diff --git a/config.go b/config.go index 0eb6679..01b3ac9 100644 --- a/config.go +++ b/config.go @@ -38,5 +38,10 @@ func (this *config) install(srcDir, dstDir, taskName string, flags int) error { } func (this *config) uninstall(dstDir, taskName string, flags int) error { - return nil + task, ok := this.Tasks[taskName] + if !ok { + return fmt.Errorf("Task not found: '%s'", taskName) + } + + return task.uninstall(dstDir, this, flags) } diff --git a/link.go b/link.go index 7548a16..afa63c6 100644 --- a/link.go +++ b/link.go @@ -34,23 +34,9 @@ type link struct { Src string } -func preparePath(loc string, flags int) error { - clobber := flags&optClobber == optClobber - force := flags&optForce == optForce +func cleanPath(loc string, flags int) error { verbose := flags&optVerbose == optVerbose - if force { - parentDir, _ := path.Split(loc) - if _, err := os.Stat(parentDir); os.IsNotExist(err) { - if verbose { - log.Printf("Force creating path: '%s'", parentDir) - } - if err := os.MkdirAll(parentDir, 0777); err != nil { - return err - } - } - } - if info, _ := os.Lstat(loc); info != nil { if info.Mode()&os.ModeSymlink == os.ModeSymlink { if verbose { @@ -59,7 +45,7 @@ func preparePath(loc string, flags int) error { if err := os.Remove(loc); err != nil { return err } - } else if clobber { + } else if flags&optClobber == optClobber { if verbose { log.Print("Clobbering path: '%s'", loc) } @@ -72,6 +58,22 @@ func preparePath(loc string, flags int) error { return nil } +func prepInstallPath(loc string, flags int) error { + if flags&optForce == optForce { + parentDir, _ := path.Split(loc) + if _, err := os.Stat(parentDir); os.IsNotExist(err) { + if flags&optVerbose == optVerbose { + log.Printf("Force creating path: '%s'", parentDir) + } + if err := os.MkdirAll(parentDir, 0777); err != nil { + return err + } + } + } + + return cleanPath(loc, flags) +} + func (this link) install(srcDir, dstDir string, flags int) error { if len(this.Dst) == 0 { this.Dst = this.Src @@ -84,7 +86,7 @@ func (this link) install(srcDir, dstDir string, flags int) error { return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath) } - if err := preparePath(dstPath, flags); err != nil { + if err := prepInstallPath(dstPath, flags); err != nil { return err } @@ -94,3 +96,11 @@ func (this link) install(srcDir, dstDir string, flags int) error { return os.Symlink(srcPath, dstPath) } + +func (this *link) uninstall(dstDir string, flags int) error { + if len(this.Dst) == 0 { + this.Dst = this.Src + } + + return cleanPath(path.Join(dstDir, this.Dst), flags) +} diff --git a/task.go b/task.go index b251a8e..770d252 100644 --- a/task.go +++ b/task.go @@ -29,23 +29,47 @@ type task struct { Links []link } -func (this task) install(srcDir, dstDir string, conf *config, flags int) error { +func (this *task) walk(conf *config, depFunc func(depTask *task) error, linkFunc func(currLink *link) error) error { for _, depName := range this.Deps { depTask, ok := conf.Tasks[depName] if !ok { return fmt.Errorf("Task dependency not found: '%s'", depName) } - if err := depTask.install(srcDir, dstDir, conf, flags); err != nil { + if err := depFunc(&depTask); err != nil { return err } } - for _, link := range this.Links { - if err := link.install(srcDir, dstDir, flags); err != nil { + for _, currLink := range this.Links { + if err := linkFunc(&currLink); err != nil { return err } } 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) +}