forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
76 lines (67 loc) · 1.48 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
package main
import (
"log"
"os"
"path/filepath"
)
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 := ReadAndLoadGodeps(findGodepsJSON())
if err != nil {
log.Fatalln(err)
}
hadError := false
for _, dep := range g.Deps {
err := restore(dep)
if err != nil {
log.Println("restore:", err)
hadError = true
}
}
if hadError {
os.Exit(1)
}
}
// restore downloads the given dependency and checks out
// the given revision.
func restore(dep Dependency) error {
// make sure pkg exists somewhere in GOPATH
args := []string{"get", "-d"}
if verbose {
args = append(args, "-v")
}
err := runIn(".", "go", append(args, dep.ImportPath)...)
if err != nil {
return err
}
ps, err := LoadPackages(dep.ImportPath)
if err != nil {
return err
}
pkg := ps[0]
if !dep.vcs.exists(pkg.Dir, dep.Rev) {
dep.vcs.vcs.Download(pkg.Dir)
}
return dep.vcs.RevSync(pkg.Dir, dep.Rev)
}
func findGodepsJSON() (path string) {
dir, isDir := findGodeps()
if dir == "" {
log.Fatalln("No Godeps found (or in any parent directory)")
}
if isDir {
return filepath.Join(dir, "Godeps", "Godeps.json")
}
return filepath.Join(dir, "Godeps")
}