Refactoring

This commit is contained in:
Alex Yatskov 2015-04-02 19:18:58 +09:00
parent cb21b57cee
commit 2c89063d13
3 changed files with 61 additions and 22 deletions

View File

@ -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)
}

44
link.go
View File

@ -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)
}

32
task.go
View File

@ -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)
}