Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #32 from albertdev/bugfix/rework-clobber-prompt
Browse files Browse the repository at this point in the history
Continue to next file / task when pressing 'n' at clobber prompt
  • Loading branch information
FooSoft authored Dec 14, 2021
2 parents 0066db5 + d4087be commit ddbd7c4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 44 deletions.
50 changes: 7 additions & 43 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,6 @@ import (
"strconv"
)

func cleanPath(loc string, flags int) error {
if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == 0 {
if flags&flagClobber != 0 || prompt("clobber path", loc) {
if flags&flagVerbose != 0 {
log.Printf("clobbering path: %s", loc)
}
if err := os.RemoveAll(loc); err != nil {
return err
}
}
} else {
if flags&flagVerbose != 0 {
log.Printf("removing symlink: %s", loc)
}
if err := os.Remove(loc); err != nil {
return err
}
}
}

return 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 {
Expand Down Expand Up @@ -124,9 +83,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)
Expand All @@ -141,6 +104,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
}
}
6 changes: 5 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
)
Expand All @@ -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]
Expand Down

0 comments on commit ddbd7c4

Please sign in to comment.