This commit is contained in:
Alex Yatskov 2015-04-05 16:53:32 +09:00
parent eac74e82cb
commit 1f8da243cf
4 changed files with 32 additions and 82 deletions

View File

@ -36,12 +36,3 @@ func (this *config) install(srcDir, dstDir, taskName string, flags int) error {
return task.install(srcDir, dstDir, this, flags) return task.install(srcDir, dstDir, this, flags)
} }
func (this *config) uninstall(dstDir, taskName string, flags int) error {
task, ok := this.Tasks[taskName]
if !ok {
return fmt.Errorf("Task not found: '%s'", taskName)
}
return task.uninstall(dstDir, this, flags)
}

View File

@ -75,7 +75,7 @@ func usage() {
flag.PrintDefaults() flag.PrintDefaults()
} }
func absPath(path string) string { func makeAbsPath(path string) string {
path, err := filepath.Abs(path) path, err := filepath.Abs(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -91,8 +91,7 @@ func main() {
} }
taskName := flag.String("task", "default", "name of task to execute") taskName := flag.String("task", "default", "name of task to execute")
action := flag.String("action", "install", "install or uninstall symlinks") dstDir := flag.String("dest", currUsr.HomeDir, "target directory for tasks")
dstDir := flag.String("dest", currUsr.HomeDir, "target directory for symlinks")
force := flag.Bool("force", true, "create parent directories to target") force := flag.Bool("force", true, "create parent directories to target")
clobber := flag.Bool("clobber", false, "delete files and directories at target") clobber := flag.Bool("clobber", false, "delete files and directories at target")
verbose := flag.Bool("verbose", false, "verbose output") verbose := flag.Bool("verbose", false, "verbose output")
@ -111,32 +110,17 @@ func main() {
flags |= optVerbose flags |= optVerbose
} }
if flag.NArg() == 0 { if flag.NArg() == 2 {
usage()
os.Exit(2)
}
conf, err := parse(flag.Arg(0)) conf, err := parse(flag.Arg(0))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
switch *action { if err := conf.install(makeAbsPath(flag.Arg(1)), makeAbsPath(*dstDir), *taskName, flags); err != nil {
case "install":
if flag.NArg() >= 2 {
if err := conf.install(absPath(flag.Arg(1)), absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {
usage() usage()
os.Exit(2) os.Exit(2)
} }
case "uninstall":
if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err)
}
default:
usage()
os.Exit(2)
}
} }

25
link.go
View File

@ -57,9 +57,13 @@ func cleanPath(loc string, flags int) error {
return nil return nil
} }
func prepInstallPath(loc string, flags int) error { func createPath(loc string, flags int) error {
if flags&optForce == optForce { if flags&optForce == 0 {
return nil
}
parentDir, _ := path.Split(loc) parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) { if _, err := os.Stat(parentDir); os.IsNotExist(err) {
if flags&optVerbose == optVerbose { if flags&optVerbose == optVerbose {
log.Printf("Force creating path: '%s'", parentDir) log.Printf("Force creating path: '%s'", parentDir)
@ -68,9 +72,8 @@ func prepInstallPath(loc string, flags int) error {
return err return err
} }
} }
}
return cleanPath(loc, flags) return nil
} }
func (this *link) source() string { func (this *link) source() string {
@ -106,7 +109,11 @@ func (this *link) install(srcDir, dstDir string, flags int) error {
return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath) return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath)
} }
if err := prepInstallPath(dstPath, flags); err != nil { if err := createPath(dstPath, flags); err != nil {
return err
}
if err := cleanPath(dstPath, flags); err != nil {
return err return err
} }
@ -116,11 +123,3 @@ func (this *link) install(srcDir, dstDir string, flags int) error {
return os.Symlink(srcPath, dstPath) return os.Symlink(srcPath, dstPath)
} }
func (this *link) uninstall(dstDir string, flags int) error {
if !this.valid() {
return fmt.Errorf("Link element is invalid")
}
return cleanPath(path.Join(dstDir, this.destination()), flags)
}

30
task.go
View File

@ -31,47 +31,23 @@ type task struct {
Links []link Links []link
} }
func (this *task) walk(conf *config, depFunc func(depTask *task) error, linkFunc func(currLink *link) error) error { func (this *task) install(srcDir, dstDir string, conf *config, flags int) error {
for _, depName := range this.Deps { for _, depName := range this.Deps {
depTask, ok := conf.Tasks[depName] depTask, ok := conf.Tasks[depName]
if !ok { if !ok {
return fmt.Errorf("Task dependency not found: '%s'", depName) return fmt.Errorf("Task dependency not found: '%s'", depName)
} }
if err := depFunc(&depTask); err != nil { if err := depTask.install(srcDir, dstDir, conf, flags); err != nil {
return err return err
} }
} }
for _, currLink := range this.Links { for _, currLink := range this.Links {
if err := linkFunc(&currLink); err != nil { if err := currLink.install(srcDir, dstDir, flags); err != nil {
return err return err
} }
} }
return nil return nil
} }
func (this *task) install(srcDir, dstDir string, conf *config, flags int) error {
depWalker := func(depTask *task) error {
return depTask.install(srcDir, dstDir, conf, flags)
}
linkWalker := func(currLink *link) error {
return currLink.install(srcDir, dstDir, flags)
}
return this.walk(conf, depWalker, linkWalker)
}
func (this *task) uninstall(dstDir string, conf *config, flags int) error {
depWalker := func(depTask *task) error {
return depTask.uninstall(dstDir, conf, flags)
}
linkWalker := func(currLink *link) error {
return currLink.uninstall(dstDir, flags)
}
return this.walk(conf, depWalker, linkWalker)
}