gopkg.in/tools/godep.v41@v41.0.0-20151217180337-fe5ce707f879/dep.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "sort" 8 "strings" 9 ) 10 11 // A Dependency is a specific revision of a package. 12 type Dependency struct { 13 ImportPath string 14 Comment string `json:",omitempty"` // Description of commit, if present. 15 Rev string // VCS-specific commit ID. 16 17 // used by command save & update 18 ws string // workspace 19 root string // import path to repo root 20 dir string // full path to package 21 22 // used by command update 23 matched bool // selected for update by command line 24 pkg *Package 25 26 // used by command go 27 vcs *VCS 28 } 29 30 func eqDeps(a, b []Dependency) bool { 31 ok := true 32 for _, da := range a { 33 for _, db := range b { 34 if da.ImportPath == db.ImportPath && da.Rev != db.Rev { 35 ok = false 36 } 37 } 38 } 39 return ok 40 } 41 42 // containsPathPrefix returns whether any string in a 43 // is s or a directory containing s. 44 // For example, pattern ["a"] matches "a" and "a/b" 45 // (but not "ab"). 46 func containsPathPrefix(pats []string, s string) bool { 47 for _, pat := range pats { 48 if pat == s || strings.HasPrefix(s, pat+"/") { 49 return true 50 } 51 } 52 return false 53 } 54 55 func uniq(a []string) []string { 56 var s string 57 var i int 58 if !sort.StringsAreSorted(a) { 59 sort.Strings(a) 60 } 61 for _, t := range a { 62 if t != s { 63 a[i] = t 64 i++ 65 s = t 66 } 67 } 68 return a[:i] 69 } 70 71 // trimGoVersion and return the major version 72 func trimGoVersion(version string) (string, error) { 73 p := strings.Split(version, ".") 74 if len(p) < 2 { 75 return "", fmt.Errorf("Error determing major go version from: %q", version) 76 } 77 return p[0] + "." + p[1], nil 78 } 79 80 // goVersion returns the major version string of the Go compiler 81 // currently installed, e.g. "go1.5". 82 func goVersion() (string, error) { 83 // Godep might have been compiled with a different 84 // version, so we can't just use runtime.Version here. 85 cmd := exec.Command("go", "version") 86 cmd.Stderr = os.Stderr 87 out, err := cmd.Output() 88 if err != nil { 89 return "", err 90 } 91 gv := strings.Split(string(out), " ") 92 if len(gv) < 3 { 93 return "", fmt.Errorf("Error splitting output of `go version`: Expected 3 or more elements, but there are < 3: %q", string(out)) 94 } 95 return trimGoVersion(gv[2]) 96 }