github.com/elubow/godep@v0.0.0-20140525002653-983ff9241cea/get.go (about)

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