github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/project/govendor/utils.go (about) 1 package govendor 2 3 import ( 4 "fmt" 5 "go/build" 6 "os" 7 "os/exec" 8 "path" 9 "sort" 10 "strings" 11 12 "github.com/johnnyeven/libtools/executil" 13 ) 14 15 var pkgsNoGoGet = []string{ 16 "g7pay/", 17 "devops/", 18 "golib/", 19 } 20 21 func isLocalPkg(pkgName string) bool { 22 for _, pkgPrefix := range pkgsNoGoGet { 23 if strings.HasPrefix(pkgName, pkgPrefix) { 24 return true 25 } 26 } 27 return false 28 } 29 30 func UpdatePkgs(importPaths ...string) { 31 sort.Strings(importPaths) 32 needToUpdates := map[string]bool{} 33 34 for _, importPath := range importPaths { 35 isSubPkg := false 36 for p := range needToUpdates { 37 if strings.HasPrefix(importPath, p) { 38 isSubPkg = true 39 } 40 } 41 if !isSubPkg { 42 needToUpdates[importPath] = true 43 } 44 } 45 46 for importPath := range needToUpdates { 47 UpdatePkg(importPath) 48 } 49 } 50 51 func UpdatePkg(importPath string) { 52 pkg, err := build.Import(importPath, "", build.FindOnly) 53 if err != nil { 54 if !isLocalPkg(importPath) { 55 executil.StdRun(exec.Command("go", "get", "-v", importPath)) 56 return 57 } 58 goPath := os.Getenv("GOPATH") 59 os.Chdir(path.Join(strings.Split(goPath, ":")[0], "src")) 60 61 gitRepo := fmt.Sprintf(`git@git.chinawayltd.com:%s.git`, importPath) 62 executil.StdRun(exec.Command("git", "clone", gitRepo, importPath)) 63 } else { 64 os.Chdir(pkg.Dir) 65 executil.StdRun(exec.Command("git", "pull", "--rebase")) 66 } 67 }