gopkg.in/zumata/godep.v14@v14.0.0-20151008182512-99082d62f381/dep.go (about)

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