Repository navigation
Expand file tree
/
Copy pathget_imports.go
More file actions
269 lines (232 loc) · 6.65 KB
/
Copy pathget_imports.go
File metadata and controls
269 lines (232 loc) · 6.65 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cmd
import (
"github.com/Masterminds/cookoo"
"fmt"
"os"
)
const (
NoVCS uint = iota
Git
Bzr
Hg
Svn
)
func GetImports(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
cfg := p.Get("conf", nil).(*Config)
if len(cfg.Imports) == 0 {
Info("No dependencies found. Nothing downloaded.\n")
return false, nil
}
for _, dep := range cfg.Imports {
if err := VcsGet(dep); err != nil {
Warn("Skipped getting %s: %s\n", dep.Name, err)
}
}
return true, nil
}
func UpdateImports(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
cfg := p.Get("conf", nil).(*Config)
if len(cfg.Imports) == 0 {
Info("No dependencies found. Nothing updated.\n")
return false, nil
}
for _, dep := range cfg.Imports {
if err := VcsUpdate(dep); err != nil {
Warn("Update failed for %s: %s\n", dep.Name, err)
}
}
return true, nil
}
func CowardMode(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
gopath := os.Getenv("GOPATH")
if len(gopath) == 0 {
return false, fmt.Errorf("No GOPATH is set.\n")
}
if _, err := os.Stat(gopath); err != nil {
return false, fmt.Errorf("Did you forget to 'glide install'? GOPATH=%s seems not to exist: %s\n", gopath, err)
}
ggpath := os.Getenv("GLIDE_GOPATH")
if len(ggpath) > 0 && ggpath != gopath {
Warn("Your GOPATH is set to %s, and we expected %s\n", gopath, ggpath)
}
return true, nil
}
func SetReference(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
cfg := p.Get("conf", nil).(*Config)
if len(cfg.Imports) == 0 {
Info("No dependencies found.\n")
return false, nil
}
for _, dep := range cfg.Imports {
if err := VcsVersion(dep); err != nil {
Warn("Failed to set version on %s to %s: %s\n", dep.Name, dep.Reference, err)
}
}
return true, nil
}
type VCS interface {
Get(*Dependency) error
Update(*Dependency) error
Version(*Dependency) error
LastCommit(*Dependency) (string, error)
}
var (
goGet VCS = new(GoGetVCS)
git VCS = new(GitVCS)
svn VCS = new(SvnVCS)
bzr VCS = new(BzrVCS)
hg VCS = new(HgVCS)
)
// VcsGet figures out how to fetch a dependency, and then gets it.
//
// Usually it delegates to lower-level *Get functions.
//
// See https://code.google.com/p/go/source/browse/src/cmd/go/vcs.go
func VcsGet(dep *Dependency) error {
// See note in VcsUpdate.
if dep.Repository == "" && dep.Reference == "" {
Info("Installing %s with 'go get'\n", dep.Name)
return goGet.Get(dep)
}
switch dep.VcsType {
case Git:
if dep.Repository == "" {
dep.Repository = "http://" + dep.Name
}
Info("Installing %s with Git (From %s)\n", dep.Name, dep.Repository)
return git.Get(dep)
case Bzr:
Info("Installing %s with Bzr (From %s)\n", dep.Name, dep.Repository)
return bzr.Get(dep)
case Hg:
if dep.Repository == "" {
dep.Repository = "http://" + dep.Name
}
Info("Installing %s with Hg (From %s)\n", dep.Name, dep.Repository)
return hg.Get(dep)
case Svn:
Info("Installing %s with Svn (From %s)\n", dep.Name, dep.Repository)
return svn.Get(dep)
default:
if dep.VcsType == NoVCS {
Info("Defaulting to 'go get %s'\n", dep.Name)
if len(dep.Reference) > 0 {
Warn("Ref is set to %s, but no VCS is set. This can cause inconsistencies.\n", dep.Reference)
}
} else {
Warn("No handler for %d. Falling back to 'go get %s'.\n", dep.VcsType, dep.Name)
}
return goGet.Get(dep)
}
}
// VcsUpdate updates to a particular checkout based on the VCS setting.
func VcsUpdate(dep *Dependency) error {
// If there is no Ref set, and if Repository is empty, we should just
// default to Go Get.
//
// Why do we care if Ref is blank? As of Go 1.3, go get builds a .a
// file for each library. But if we set a Ref, that will switch the source
// code, but not necessarily build a .a file. So we want to make sure not
// to default to 'go get' if we're then going to grab a specific version.
if dep.Reference == "" && dep.Repository == "" {
Info("No ref or repo. Falling back to 'go get -u %s'.\n", dep.Name)
return goGet.Update(dep)
}
if dep.VcsType == NoVCS {
guess, err := GuessVCS(dep)
if err != nil {
Warn("Tried to guess VCS type, but failed: %s", err)
} else {
dep.VcsType = guess
}
}
switch dep.VcsType {
case Git:
Info("Updating %s with Git (From %s)\n", dep.Name, dep.Repository)
return git.Update(dep)
case Bzr:
Info("Updating %s with Bzr (From %s)\n", dep.Name, dep.Repository)
return bzr.Update(dep)
case Hg:
Info("Updating %s with Hg (From %s)\n", dep.Name, dep.Repository)
return hg.Update(dep)
case Svn:
Info("Updating %s with Svn (From %s)\n", dep.Name, dep.Repository)
return svn.Update(dep)
default:
if dep.VcsType == NoVCS {
Info("No VCS set. Updating with 'go get -u %s'\n", dep.Name)
if len(dep.Reference) > 0 {
Warn("Ref is set to %s, but no VCS is set. This can cause inconsistencies.\n", dep.Reference)
}
} else {
Warn("No handler for this repo type. Falling back to 'go get -u %s'.\n", dep.Name)
}
return goGet.Update(dep)
}
}
func VcsVersion(dep *Dependency) error {
if dep.VcsType == NoVCS {
dep.VcsType, _ = GuessVCS(dep)
}
switch dep.VcsType {
case Git:
return git.Version(dep)
case Bzr:
return bzr.Version(dep)
case Hg:
return hg.Version(dep)
case Svn:
return svn.Version(dep)
default:
if len(dep.Reference) > 0 {
Warn("Cannot update %s to specific version with VCS %d.\n", dep.Name, dep.VcsType)
return goGet.Version(dep)
}
return nil
}
}
func VcsLastCommit(dep *Dependency) (string, error) {
if dep.VcsType == NoVCS {
dep.VcsType, _ = GuessVCS(dep)
}
switch dep.VcsType {
case Git:
return git.LastCommit(dep)
case Bzr:
return bzr.LastCommit(dep)
case Hg:
return hg.LastCommit(dep)
case Svn:
return svn.LastCommit(dep)
default:
if len(dep.Reference) > 0 {
Warn("Cannot update %s to specific version with VCS %d.\n", dep.Name, dep.VcsType)
return goGet.LastCommit(dep)
}
return "", nil
}
}
func VcsSetReference(dep *Dependency) error {
Warn("Cannot set reference. not implemented.\n")
return nil
}
func GuessVCS(dep *Dependency) (uint, error) {
dest := fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), dep.Name)
//Debug("Looking in %s for hints about VCS type.\n", dest)
if _, err := os.Stat(dest + "/.git"); err == nil {
Info("Looks like %s is a Git repo.\n", dest)
return Git, nil
} else if _, err := os.Stat(dest + "/.bzr"); err == nil {
Info("Looks like %s is a Bzr repo.\n", dest)
return Bzr, nil
} else if _, err := os.Stat(dest + "/.hg"); err == nil {
Info("Looks like %s is a Mercurial repo.\n", dest)
return Hg, nil
} else if _, err := os.Stat(dest + "/.svn"); err == nil {
Info("Looks like %s is a Subversion repo.\n", dest)
return Svn, nil
} else {
return NoVCS, nil
}
}