Refactoring
This commit is contained in:
parent
cb21b57cee
commit
2c89063d13
@ -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 {
|
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
44
link.go
@ -34,23 +34,9 @@ type link struct {
|
|||||||
Src string
|
Src string
|
||||||
}
|
}
|
||||||
|
|
||||||
func preparePath(loc string, flags int) error {
|
func cleanPath(loc string, flags int) error {
|
||||||
clobber := flags&optClobber == optClobber
|
|
||||||
force := flags&optForce == optForce
|
|
||||||
verbose := flags&optVerbose == optVerbose
|
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, _ := os.Lstat(loc); info != nil {
|
||||||
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||||
if verbose {
|
if verbose {
|
||||||
@ -59,7 +45,7 @@ func preparePath(loc string, flags int) error {
|
|||||||
if err := os.Remove(loc); err != nil {
|
if err := os.Remove(loc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if clobber {
|
} else if flags&optClobber == optClobber {
|
||||||
if verbose {
|
if verbose {
|
||||||
log.Print("Clobbering path: '%s'", loc)
|
log.Print("Clobbering path: '%s'", loc)
|
||||||
}
|
}
|
||||||
@ -72,6 +58,22 @@ func preparePath(loc string, flags int) error {
|
|||||||
return nil
|
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 {
|
func (this link) install(srcDir, dstDir string, flags int) error {
|
||||||
if len(this.Dst) == 0 {
|
if len(this.Dst) == 0 {
|
||||||
this.Dst = this.Src
|
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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,3 +96,11 @@ 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 len(this.Dst) == 0 {
|
||||||
|
this.Dst = this.Src
|
||||||
|
}
|
||||||
|
|
||||||
|
return cleanPath(path.Join(dstDir, this.Dst), flags)
|
||||||
|
}
|
||||||
|
32
task.go
32
task.go
@ -29,23 +29,47 @@ type task struct {
|
|||||||
Links []link
|
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 {
|
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 := depTask.install(srcDir, dstDir, conf, flags); err != nil {
|
if err := depFunc(&depTask); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, link := range this.Links {
|
for _, currLink := range this.Links {
|
||||||
if err := link.install(srcDir, dstDir, flags); err != nil {
|
if err := linkFunc(&currLink); 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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user