Move shared *Path functions to util.go

This commit is contained in:
Bert Jacobs 2021-09-13 18:26:01 +02:00
parent 0ba821eb7e
commit d4087be780
2 changed files with 47 additions and 46 deletions

46
link.go
View File

@ -30,52 +30,6 @@ import (
"strconv" "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) { func parseLink(params []string) (srcPath, dstPath string, mode os.FileMode, err error) {
length := len(params) length := len(params)
if length < 1 || length > 3 { if length < 1 || length > 3 {

47
util.go
View File

@ -26,6 +26,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -47,6 +48,52 @@ func makeAbsPath(path string) string {
return path 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 { func makeVariantNames(name, variant string) []string {
if nameParts := strings.Split(name, "__"); len(nameParts) > 1 { if nameParts := strings.Split(name, "__"); len(nameParts) > 1 {
variant = nameParts[len(nameParts)-1] variant = nameParts[len(nameParts)-1]