github.com/octohelm/cuemod@v0.9.4/pkg/cuemod/util.go (about) 1 package cuemod 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "golang.org/x/mod/semver" 10 ) 11 12 func versionGreaterThan(v string, w string) bool { 13 if w == "" { 14 return false 15 } 16 return semver.Compare(v, w) > 0 17 } 18 19 func isSubDirFor(targetpath string, root string) bool { 20 targetpath = targetpath + "/" 21 root = root + "/" 22 return strings.HasPrefix(targetpath, root) 23 } 24 25 func subDir(pkg string, importPath string) (string, error) { 26 if isSubDirFor(importPath, pkg) { 27 if len(importPath) > len(pkg)+1 { 28 return importPath[len(pkg)+1:], nil 29 } 30 return "", nil 31 } 32 return "", fmt.Errorf("%s is not sub CompletePath of %s", importPath, pkg) 33 } 34 35 func replaceImportPath(to string, from string, importPath string) string { 36 if from == importPath { 37 return to 38 } 39 s, _ := subDir(from, importPath) 40 return filepath.Join(to, s) 41 } 42 43 func paths(path string) []string { 44 paths := make([]string, 0) 45 d := path 46 for { 47 paths = append(paths, d) 48 if !strings.Contains(d, "/") { 49 break 50 } 51 d = filepath.Join(d, "../") 52 } 53 return paths 54 } 55 56 func writeFile(filename string, data []byte) error { 57 if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil { 58 return err 59 } 60 return os.WriteFile(filename, data, os.ModePerm) 61 }