gopkg.in/tools/godep.v56@v56.0.0-20160226230103-b32db8cfcaad/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  	if version == "devel" {
    74  		return "devel", nil
    75  	}
    76  	p := strings.Split(version, ".")
    77  	if len(p) < 2 {
    78  		return "", fmt.Errorf("Error determing major go version from: %q", version)
    79  	}
    80  	var split string
    81  	switch {
    82  	case strings.Contains(p[1], "beta"):
    83  		split = "beta"
    84  	case strings.Contains(p[1], "rc"):
    85  		split = "rc"
    86  	}
    87  	if split != "" {
    88  		p[1] = strings.Split(p[1], split)[0]
    89  	}
    90  	return p[0] + "." + p[1], nil
    91  }
    92  
    93  // goVersion returns the major version string of the Go compiler
    94  // currently installed, e.g. "go1.5".
    95  func goVersion() (string, error) {
    96  	// Godep might have been compiled with a different
    97  	// version, so we can't just use runtime.Version here.
    98  	cmd := exec.Command("go", "version")
    99  	cmd.Stderr = os.Stderr
   100  	out, err := cmd.Output()
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  	gv := strings.Split(string(out), " ")
   105  	if len(gv) < 3 {
   106  		return "", fmt.Errorf("Error splitting output of `go version`: Expected 3 or more elements, but there are < 3: %q", string(out))
   107  	}
   108  	return trimGoVersion(gv[2])
   109  }