gopkg.in/tools/godep.v56@v56.0.0-20160226230103-b32db8cfcaad/get.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"os/exec"
     7  )
     8  
     9  var cmdGet = &Command{
    10  	Name:  "get",
    11  	Args:  "[-t] [packages]",
    12  	Short: "download and install packages with specified dependencies",
    13  	Long: `
    14  Get downloads to GOPATH the packages named by the import paths, and installs
    15  them with the dependencies specified in their Godeps files.
    16  
    17  If any of the packages do not have Godeps files, those are installed
    18  as if by go get.
    19  
    20  If -t is given, dependencies of test files are also downloaded and installed.
    21  
    22  For more about specifying packages, see 'go help packages'.
    23  `,
    24  	Run: runGet,
    25  }
    26  
    27  var getT bool
    28  
    29  func init() {
    30  	cmdGet.Flag.BoolVar(&getT, "t", false, "get test dependencies")
    31  }
    32  
    33  func runGet(cmd *Command, args []string) {
    34  	if len(args) == 0 {
    35  		args = []string{"."}
    36  	}
    37  
    38  	cmdArgs := []interface{}{"get", "-d"}
    39  	if verbose {
    40  		cmdArgs = append(cmdArgs, "-v")
    41  	}
    42  
    43  	if getT {
    44  		cmdArgs = append(cmdArgs, "-t")
    45  	}
    46  
    47  	err := command("go", append(cmdArgs, args)...).Run()
    48  	if err != nil {
    49  		log.Fatalln(err)
    50  	}
    51  
    52  	// group import paths by Godeps location
    53  	groups := make(map[string][]string)
    54  	ps, err := LoadPackages(args...)
    55  	if err != nil {
    56  		log.Fatalln(err)
    57  	}
    58  	for _, pkg := range ps {
    59  		if pkg.Error.Err != "" {
    60  			log.Fatalln(pkg.Error.Err)
    61  		}
    62  		dir, _ := findInParents(pkg.Dir, "Godeps")
    63  		groups[dir] = append(groups[dir], pkg.ImportPath)
    64  	}
    65  	for dir, packages := range groups {
    66  		var c *exec.Cmd
    67  		if dir == "" {
    68  			c = command("go", "install", packages)
    69  		} else {
    70  			c = command("godep", "go", "install", packages)
    71  			c.Dir = dir
    72  		}
    73  		if err := c.Run(); err != nil {
    74  			log.Fatalln(err)
    75  		}
    76  	}
    77  }
    78  
    79  // command is like exec.Command, but the returned
    80  // Cmd inherits stderr from the current process, and
    81  // elements of args may be either string or []string.
    82  func command(name string, args ...interface{}) *exec.Cmd {
    83  	var a []string
    84  	for _, arg := range args {
    85  		switch v := arg.(type) {
    86  		case string:
    87  			a = append(a, v)
    88  		case []string:
    89  			a = append(a, v...)
    90  		}
    91  	}
    92  	c := exec.Command(name, a...)
    93  	c.Stderr = os.Stderr
    94  	return c
    95  }