Pass result of "clobber path" prompt to caller, stop task

The end user would be prompted if a file is going to be overwritten,
but homemaker would still attempt to do so no matter the answer.

Move the retry-functionality of cleanPath inside this function so that
the function itself can return a boolean to indicate if the calling task
is free to continue. If the path is not clean then homemaker can just
move on to the next file or next task.
This commit is contained in:
Bert Jacobs 2021-09-13 18:23:39 +02:00
parent 0066db5487
commit 0ba821eb7e
2 changed files with 25 additions and 11 deletions

30
link.go
View File

@ -30,28 +30,33 @@ import (
"strconv" "strconv"
) )
func cleanPath(loc string, flags int) error { func cleanPath(loc string, flags int) (bool, error) {
if info, _ := os.Lstat(loc); info != nil { if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == 0 { if info.Mode()&os.ModeSymlink == 0 {
if flags&flagClobber != 0 || prompt("clobber path", loc) { shouldContinue := false
if flags&flagClobber == 0 {
shouldContinue = prompt("clobber path", loc)
}
if flags&flagClobber != 0 || shouldContinue {
if flags&flagVerbose != 0 { if flags&flagVerbose != 0 {
log.Printf("clobbering path: %s", loc) log.Printf("clobbering path: %s", loc)
} }
if err := os.RemoveAll(loc); err != nil { if err := try(func() error { return os.RemoveAll(loc) }) ; err != nil {
return err return false, err
} }
} else {
return false, nil
} }
} else { } else {
if flags&flagVerbose != 0 { if flags&flagVerbose != 0 {
log.Printf("removing symlink: %s", loc) log.Printf("removing symlink: %s", loc)
} }
if err := os.Remove(loc); err != nil { if err := try(func() error { return os.Remove(loc) }); err != nil {
return err return false, err
} }
} }
} }
return true, nil
return nil
} }
func createPath(loc string, flags int, mode os.FileMode) error { func createPath(loc string, flags int, mode os.FileMode) error {
@ -124,9 +129,13 @@ func processLink(params []string, conf *config) error {
return err return err
} }
if err := try(func() error { return cleanPath(dstPathAbs, conf.flags) }); err != nil { pathCleaned, err := cleanPath(dstPathAbs, conf.flags)
if err != nil {
return err return err
} }
if !pathCleaned {
return nil
}
if conf.flags&flagVerbose != 0 { if conf.flags&flagVerbose != 0 {
log.Printf("linking %s to %s", srcPathAbs, dstPathAbs) log.Printf("linking %s to %s", srcPathAbs, dstPathAbs)
@ -141,6 +150,7 @@ func processLink(params []string, conf *config) error {
return nil return nil
} }
return try(func() error { return cleanPath(dstPathAbs, conf.flags) }) _, err = cleanPath(dstPathAbs, conf.flags)
return err
} }
} }

View File

@ -96,9 +96,13 @@ func processTemplate(params []string, conf *config) (err error) {
return err return err
} }
if err = try(func() error { return cleanPath(dstPathAbs, conf.flags) }); err != nil { pathCleaned, err := cleanPath(dstPathAbs, conf.flags)
if err != nil {
return err return err
} }
if !pathCleaned {
return nil
}
if conf.flags&flagVerbose != 0 { if conf.flags&flagVerbose != 0 {
log.Printf("process template %s to %s", srcPathAbs, dstPathAbs) log.Printf("process template %s to %s", srcPathAbs, dstPathAbs)