forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.go
131 lines (118 loc) · 2.99 KB
/
install.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
var cmdInstall = &Command{
Usage: "install",
Short: "Install go dependencies",
Long: `
Install makes sure that all the dependencies are
checked (with their right revisions) out locally
under $GOPATH/src folder.
`,
Run: runInstall,
}
func runInstall(cmd *Command, args []string) {
downloadDependencies()
}
// prepareGopath reads dependency information from the filesystem
// entry name, fetches any necessary code, and returns a gopath
// causing the specified dependencies to be used.
func downloadDependencies() {
dir, isDir := findGodeps()
if dir == "" {
log.Fatalln("No Godeps found (or in any parent directory)")
}
if isDir {
return
}
g, err := ReadGodeps(filepath.Join(dir, "Godeps"))
if err != nil {
log.Fatalln(err)
}
err = downloadAll(g.Deps)
if err != nil {
log.Fatalln(err)
}
}
// findGodeps looks for a directory entry "Godeps" in the
// current directory or any parent, and returns the containing
// directory and whether the entry itself is a directory.
// If Godeps can't be found, findGodeps returns "".
// For any other error, it exits the program.
func findGodeps() (dir string, isDir bool) {
wd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
return findInParents(wd, "Godeps")
}
// findInParents returns the path to the directory containing name
// in dir or any ancestor, and whether name itself is a directory.
// If name cannot be found, findInParents returns the empty string.
func findInParents(dir, name string) (container string, isDir bool) {
for {
fi, err := os.Stat(filepath.Join(dir, name))
if os.IsNotExist(err) && dir == "/" {
return "", false
}
if os.IsNotExist(err) {
dir = filepath.Dir(dir)
continue
}
if err != nil {
log.Fatalln(err)
}
return dir, fi.IsDir()
}
}
func envNoGopath() (a []string) {
for _, s := range os.Environ() {
if !strings.HasPrefix(s, "GOPATH=") {
a = append(a, s)
}
}
return a
}
// sandboxAll ensures that the commits in deps are available
// on disk, and returns a GOPATH string that will cause them
// to be used.
func downloadAll(a []Dependency) (err error) {
fmt.Println("")
for _, dep := range a {
err := download(dep)
if err != nil {
return err
}
}
fmt.Println("")
return
}
func format(data string, maxNoOfChars int) string {
noOfCharsToUse := len(data)
if noOfCharsToUse > maxNoOfChars {
noOfCharsToUse = maxNoOfChars
}
return data[:noOfCharsToUse]
}
// sandbox ensures that commit d is available on disk,
// and returns a GOPATH string that will cause it to be used.
func download(d Dependency) (err error) {
if !exists(d.RepoPath()) {
fmt.Printf("\nInstalling %s ...",d.ImportPath)
if err = d.CreateRepo("main"); err != nil {
return fmt.Errorf("create repo: %s", err)
}
d.fetchAndCheckout("main")
}
fmt.Printf("\nUsing %s (%s)",d.ImportPath, format(d.Rev, 8))
err = d.checkout()
if err != nil {
err = d.fetchAndCheckout("main")
}
return
}