From 0ba821eb7ee58c7895c99b7c7a0900edc324b96e Mon Sep 17 00:00:00 2001 From: Bert Jacobs Date: Mon, 13 Sep 2021 18:23:39 +0200 Subject: [PATCH 1/2] 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. --- link.go | 30 ++++++++++++++++++++---------- template.go | 6 +++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/link.go b/link.go index 35f4979..aae10e6 100644 --- a/link.go +++ b/link.go @@ -30,28 +30,33 @@ import ( "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.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 { log.Printf("clobbering path: %s", loc) } - if err := os.RemoveAll(loc); err != nil { - return err + if err := try(func() error { return os.RemoveAll(loc) }) ; err != nil { + return false, err } + } else { + return false, nil } } else { if flags&flagVerbose != 0 { log.Printf("removing symlink: %s", loc) } - if err := os.Remove(loc); err != nil { - return err + if err := try(func() error { return os.Remove(loc) }); err != nil { + return false, err } } } - - return nil + return true, nil } func createPath(loc string, flags int, mode os.FileMode) error { @@ -124,9 +129,13 @@ func processLink(params []string, conf *config) error { 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 } + if !pathCleaned { + return nil + } if conf.flags&flagVerbose != 0 { log.Printf("linking %s to %s", srcPathAbs, dstPathAbs) @@ -141,6 +150,7 @@ func processLink(params []string, conf *config) error { return nil } - return try(func() error { return cleanPath(dstPathAbs, conf.flags) }) + _, err = cleanPath(dstPathAbs, conf.flags) + return err } } diff --git a/template.go b/template.go index 002386f..a0d0b84 100644 --- a/template.go +++ b/template.go @@ -96,9 +96,13 @@ func processTemplate(params []string, conf *config) (err error) { 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 } + if !pathCleaned { + return nil + } if conf.flags&flagVerbose != 0 { log.Printf("process template %s to %s", srcPathAbs, dstPathAbs) From d4087be780c9fe4eb01446f7e677d890953d8ed3 Mon Sep 17 00:00:00 2001 From: Bert Jacobs Date: Mon, 13 Sep 2021 18:26:01 +0200 Subject: [PATCH 2/2] Move shared *Path functions to util.go --- link.go | 46 ---------------------------------------------- util.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/link.go b/link.go index aae10e6..c583ed3 100644 --- a/link.go +++ b/link.go @@ -30,52 +30,6 @@ import ( "strconv" ) -func cleanPath(loc string, flags int) (bool, error) { - if info, _ := os.Lstat(loc); info != nil { - if info.Mode()&os.ModeSymlink == 0 { - shouldContinue := false - if flags&flagClobber == 0 { - shouldContinue = prompt("clobber path", loc) - } - if flags&flagClobber != 0 || shouldContinue { - if flags&flagVerbose != 0 { - log.Printf("clobbering path: %s", loc) - } - if err := try(func() error { return os.RemoveAll(loc) }) ; err != nil { - return false, err - } - } else { - return false, nil - } - } else { - if flags&flagVerbose != 0 { - log.Printf("removing symlink: %s", loc) - } - if err := try(func() error { return os.Remove(loc) }); err != nil { - return false, err - } - } - } - return true, nil -} - -func createPath(loc string, flags int, mode os.FileMode) error { - parentDir := path.Dir(loc) - - if _, err := os.Stat(parentDir); os.IsNotExist(err) { - if flags&flagForce != 0 || prompt("force create path", parentDir) { - if flags&flagVerbose != 0 { - log.Printf("force creating path: %s", parentDir) - } - if err := os.MkdirAll(parentDir, mode); err != nil { - return err - } - } - } - - return nil -} - func parseLink(params []string) (srcPath, dstPath string, mode os.FileMode, err error) { length := len(params) if length < 1 || length > 3 { diff --git a/util.go b/util.go index 58a4374..3d16e6f 100644 --- a/util.go +++ b/util.go @@ -26,6 +26,7 @@ import ( "fmt" "log" "os" + "path" "path/filepath" "strings" ) @@ -47,6 +48,52 @@ func makeAbsPath(path string) string { return path } +func cleanPath(loc string, flags int) (bool, error) { + if info, _ := os.Lstat(loc); info != nil { + if info.Mode()&os.ModeSymlink == 0 { + shouldContinue := false + if flags&flagClobber == 0 { + shouldContinue = prompt("clobber path", loc) + } + if flags&flagClobber != 0 || shouldContinue { + if flags&flagVerbose != 0 { + log.Printf("clobbering path: %s", loc) + } + if err := try(func() error { return os.RemoveAll(loc) }) ; err != nil { + return false, err + } + } else { + return false, nil + } + } else { + if flags&flagVerbose != 0 { + log.Printf("removing symlink: %s", loc) + } + if err := try(func() error { return os.Remove(loc) }); err != nil { + return false, err + } + } + } + return true, nil +} + +func createPath(loc string, flags int, mode os.FileMode) error { + parentDir := path.Dir(loc) + + if _, err := os.Stat(parentDir); os.IsNotExist(err) { + if flags&flagForce != 0 || prompt("force create path", parentDir) { + if flags&flagVerbose != 0 { + log.Printf("force creating path: %s", parentDir) + } + if err := os.MkdirAll(parentDir, mode); err != nil { + return err + } + } + } + + return nil +} + func makeVariantNames(name, variant string) []string { if nameParts := strings.Split(name, "__"); len(nameParts) > 1 { variant = nameParts[len(nameParts)-1]