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