forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
78 lines (66 loc) · 1.56 KB
/
restore.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
78
package main
import (
"log"
)
var cmdRestore = &Command{
Usage: "restore [-v]",
Short: "check out listed dependency versions in GOPATH",
Long: `
Restore checks out the Godeps-specified version of each package in GOPATH.
If -v is given, verbose output is enabled.
`,
Run: runRestore,
}
func init() {
cmdRestore.Flag.BoolVar(&verbose, "v", false, "enable verbose output")
}
func runRestore(cmd *Command, args []string) {
g, err := loadDefaultGodepsFile()
if err != nil {
log.Fatalln(err)
}
hadError := map[Dependency]bool{}
for _, dep := range g.Deps {
err := download(dep)
if err != nil {
hadError[dep] = true
}
}
for _, dep := range g.Deps {
err := restore(dep)
if err != nil && hadError[dep] {
log.Printf("download or restore: %v err: %v", dep.ImportPath, err)
hadError[dep] = true
} else {
log.Printf("restore: %v OK", dep.ImportPath)
}
}
}
// download downloads the given dependency.
func download(dep Dependency) error {
// make sure pkg exists somewhere in GOPATH
args := []string{"get", "-d"}
if verbose {
args = append(args, "-v")
}
return runIn(".", "go", append(args, dep.ImportPath)...)
}
// restore checks out the given revision.
func restore(dep Dependency) error {
ps, err := LoadPackages(dep.ImportPath)
if err != nil {
return err
}
pkg := ps[0]
dep.vcs, err = VCSForImportPath(dep.ImportPath)
if err != nil {
dep.vcs, _, err = VCSFromDir(pkg.Dir, pkg.Root)
if err != nil {
return err
}
}
if !dep.vcs.exists(pkg.Dir, dep.Rev) {
dep.vcs.vcs.Download(pkg.Dir)
}
return dep.vcs.RevSync(pkg.Dir, dep.Rev)
}