github.com/penguinn/godep@v0.0.0-20170205210856-a9cd0561f946/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 OnlyInGOPATH: true, 26 } 27 28 var getT bool 29 30 func init() { 31 cmdGet.Flag.BoolVar(&getT, "t", false, "get test dependencies") 32 } 33 34 func runGet(cmd *Command, args []string) { 35 if len(args) == 0 { 36 args = []string{"."} 37 } 38 39 cmdArgs := []interface{}{"get", "-d"} 40 if verbose { 41 cmdArgs = append(cmdArgs, "-v") 42 } 43 44 if getT { 45 cmdArgs = append(cmdArgs, "-t") 46 } 47 48 err := command("go", append(cmdArgs, args)...).Run() 49 if err != nil { 50 log.Fatalln(err) 51 } 52 53 // group import paths by Godeps location 54 groups := make(map[string][]string) 55 ps, err := LoadPackages(args...) 56 if err != nil { 57 log.Fatalln(err) 58 } 59 for _, pkg := range ps { 60 if pkg.Error.Err != "" { 61 log.Fatalln(pkg.Error.Err) 62 } 63 dir, _ := findInParents(pkg.Dir, "Godeps") 64 groups[dir] = append(groups[dir], pkg.ImportPath) 65 } 66 for dir, packages := range groups { 67 var c *exec.Cmd 68 if dir == "" { 69 c = command("go", "install", packages) 70 } else { 71 c = command("godep", "go", "install", packages) 72 c.Dir = dir 73 } 74 if err := c.Run(); err != nil { 75 log.Fatalln(err) 76 } 77 } 78 } 79 80 // command is like exec.Command, but the returned 81 // Cmd inherits stderr from the current process, and 82 // elements of args may be either string or []string. 83 func command(name string, args ...interface{}) *exec.Cmd { 84 var a []string 85 for _, arg := range args { 86 switch v := arg.(type) { 87 case string: 88 a = append(a, v) 89 case []string: 90 a = append(a, v...) 91 } 92 } 93 c := exec.Command(name, a...) 94 c.Stderr = os.Stderr 95 return c 96 }