gopkg.in/zumata/godep.v14@v14.0.0-20151008182512-99082d62f381/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 [-v] [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  If -verbose is given, verbose output is enabled.
    20  
    21  For more about specifying packages, see 'go help packages'.
    22  `,
    23  	Run: runGet,
    24  }
    25  
    26  func init() {
    27  	cmdGet.Flag.BoolVar(&verbose, "v", false, "enable verbose output")
    28  }
    29  
    30  func runGet(cmd *Command, args []string) {
    31  	if len(args) == 0 {
    32  		args = []string{"."}
    33  	}
    34  
    35  	cmdArgs := []interface{}{"get", "-d"}
    36  	if verbose {
    37  		cmdArgs = append(cmdArgs, "-v")
    38  	}
    39  
    40  	err := command("go", append(cmdArgs, args)...).Run()
    41  	if err != nil {
    42  		log.Fatalln(err)
    43  	}
    44  
    45  	// group import paths by Godeps location
    46  	groups := make(map[string][]string)
    47  	ps, err := LoadPackages(args...)
    48  	if err != nil {
    49  		log.Fatalln(err)
    50  	}
    51  	for _, pkg := range ps {
    52  		if pkg.Error.Err != "" {
    53  			log.Fatalln(pkg.Error.Err)
    54  		}
    55  		dir, _ := findInParents(pkg.Dir, "Godeps")
    56  		groups[dir] = append(groups[dir], pkg.ImportPath)
    57  	}
    58  	for dir, packages := range groups {
    59  		var c *exec.Cmd
    60  		if dir == "" {
    61  			c = command("go", "install", packages)
    62  		} else {
    63  			c = command("godep", "go", "install", packages)
    64  			c.Dir = dir
    65  		}
    66  		if err := c.Run(); err != nil {
    67  			log.Fatalln(err)
    68  		}
    69  	}
    70  }
    71  
    72  // command is like exec.Command, but the returned
    73  // Cmd inherits stderr from the current process, and
    74  // elements of args may be either string or []string.
    75  func command(name string, args ...interface{}) *exec.Cmd {
    76  	var a []string
    77  	for _, arg := range args {
    78  		switch v := arg.(type) {
    79  		case string:
    80  			a = append(a, v)
    81  		case []string:
    82  			a = append(a, v...)
    83  		}
    84  	}
    85  	c := exec.Command(name, a...)
    86  	c.Stderr = os.Stderr
    87  	return c
    88  }