This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ensure.go
394 lines (327 loc) · 11.5 KB
/
ensure.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"go/build"
"log"
"path/filepath"
"strconv"
"strings"
"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/gps/pkgtree"
"github.com/pkg/errors"
)
const ensureShortHelp = `Ensure a dependency is safely vendored in the project`
const ensureLongHelp = `
Ensure is used to fetch project dependencies into the vendor folder, as well as
to set version constraints for specific dependencies. It takes user input,
solves the updated dependency graph of the project, writes any changes to the
lock file, and places dependencies in the vendor folder.
Package spec:
<path>[:alt location][@<version specifier>]
Examples:
dep ensure Populate vendor from existing manifest and lock
dep ensure github.com/pkg/foo@^1.0.1 Update a specific dependency to a specific version
For more detailed usage examples, see dep ensure -examples.
`
const ensureExamples = `
dep ensure
Solve the project's dependency graph, and place all dependencies in the
vendor folder. If a dependency is in the lock file, use the version
specified there. Otherwise, use the most recent version that can satisfy the
constraints in the manifest file.
dep ensure -update
Update all dependencies to the latest versions allowed by the manifest,
ignoring any versions specified in the lock file. Update the lock file with
any changes.
dep ensure -update github.com/pkg/foo github.com/pkg/bar
Update a list of dependencies to the latest versions allowed by the manifest,
ignoring any versions specified in the lock file. Update the lock file with
any changes.
dep ensure github.com/pkg/foo@^1.0.1
Constrain pkg/foo to the latest release matching >= 1.0.1, < 2.0.0, and
place that release in the vendor folder. If a constraint was previously set
in the manifest, this resets it. This form of constraint strikes a good
balance of safety and flexibility, and should be preferred for libraries.
dep ensure github.com/pkg/foo@~1.0.1
Same as above, but choose any release matching 1.0.x, preferring latest.
dep ensure github.com/pkg/foo:git.internal.com/alt/foo
Fetch the dependency from a different location.
dep ensure -override github.com/pkg/foo@^1.0.1
Forcefully and transitively override any constraint for this dependency.
Overrides are powerful, but harmful in the long term. They should be used as
a last resort, especially if your project may be imported by others.
`
func (cmd *ensureCommand) Name() string { return "ensure" }
func (cmd *ensureCommand) Args() string { return "[spec...]" }
func (cmd *ensureCommand) ShortHelp() string { return ensureShortHelp }
func (cmd *ensureCommand) LongHelp() string { return ensureLongHelp }
func (cmd *ensureCommand) Hidden() bool { return false }
func (cmd *ensureCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.examples, "examples", false, "print detailed usage examples")
fs.BoolVar(&cmd.update, "update", false, "ensure dependencies are at the latest version allowed by the manifest")
fs.BoolVar(&cmd.dryRun, "n", false, "dry run, don't actually ensure anything")
fs.Var(&cmd.overrides, "override", "specify an override constraint spec (repeatable)")
}
type ensureCommand struct {
examples bool
update bool
dryRun bool
overrides stringSlice
}
func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
if cmd.examples {
ctx.Loggers.Err.Println(strings.TrimSpace(ensureExamples))
return nil
}
p, err := ctx.LoadProject("")
if err != nil {
return err
}
sm, err := ctx.SourceManager()
if err != nil {
return err
}
sm.UseDefaultSignalHandling()
defer sm.Release()
params := p.MakeParams()
if ctx.Loggers.Verbose {
params.TraceLogger = ctx.Loggers.Err
}
params.RootPackageTree, err = pkgtree.ListPackages(p.AbsRoot, string(p.ImportRoot))
if err != nil {
return errors.Wrap(err, "ensure ListPackage for project")
}
if err := checkErrors(params.RootPackageTree.Packages); err != nil {
return err
}
if cmd.update {
applyUpdateArgs(args, ¶ms)
} else {
err := applyEnsureArgs(ctx.Loggers.Err, args, cmd.overrides, p, sm, ¶ms)
if err != nil {
return err
}
}
solver, err := gps.Prepare(params, sm)
if err != nil {
return errors.Wrap(err, "ensure Prepare")
}
solution, err := solver.Solve()
if err != nil {
handleAllTheFailuresOfTheWorld(err)
return errors.Wrap(err, "ensure Solve()")
}
// check if vendor exists, because if the locks are the same but
// vendor does not exist we should write vendor
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(p.AbsRoot, "vendor"))
if err != nil {
return errors.Wrap(err, "ensure vendor is a directory")
}
writeV := dep.VendorOnChanged
if !vendorExists && solution != nil {
writeV = dep.VendorAlways
}
newLock := dep.LockFromInterface(solution)
sw, err := dep.NewSafeWriter(nil, p.Lock, newLock, writeV)
if err != nil {
return err
}
if cmd.dryRun {
return sw.PrintPreparedActions(ctx.Loggers.Out)
}
return errors.Wrap(sw.Write(p.AbsRoot, sm, true), "grouped write of manifest, lock and vendor")
}
func applyUpdateArgs(args []string, params *gps.SolveParameters) {
// When -update is specified without args, allow every project to change versions, regardless of the lock file
if len(args) == 0 {
params.ChangeAll = true
return
}
// Allow any of specified project versions to change, regardless of the lock file
for _, arg := range args {
params.ToChange = append(params.ToChange, gps.ProjectRoot(arg))
}
}
func applyEnsureArgs(logger *log.Logger, args []string, overrides stringSlice, p *dep.Project, sm gps.SourceManager, params *gps.SolveParameters) error {
var errs []error
for _, arg := range args {
pc, err := getProjectConstraint(arg, sm)
if err != nil {
errs = append(errs, err)
continue
}
if gps.IsAny(pc.Constraint) && pc.Ident.Source == "" {
// If the input specified neither a network name nor a constraint,
// then the strict thing to do would be to remove the entry
// entirely. But that would probably be quite surprising for users,
// and it's what rm is for, so just ignore the input.
//
// TODO(sdboyer): for this case - or just in general - do we want to
// add project args to the requires list temporarily for this run?
if _, has := p.Manifest.Dependencies[pc.Ident.ProjectRoot]; !has {
logger.Printf("dep: No constraint or alternate source specified for %q, omitting from manifest\n", pc.Ident.ProjectRoot)
}
// If it's already in the manifest, no need to log
continue
}
p.Manifest.Dependencies[pc.Ident.ProjectRoot] = gps.ProjectProperties{
Source: pc.Ident.Source,
Constraint: pc.Constraint,
}
}
for _, ovr := range overrides {
pc, err := getProjectConstraint(ovr, sm)
if err != nil {
errs = append(errs, err)
continue
}
// Empty overrides are fine (in contrast to deps), because they actually
// carry meaning - they force the constraints entirely open for a given
// project. Inadvisable, but meaningful.
p.Manifest.Ovr[pc.Ident.ProjectRoot] = gps.ProjectProperties{
Source: pc.Ident.Source,
Constraint: pc.Constraint,
}
}
if len(errs) > 0 {
var buf bytes.Buffer
for _, err := range errs {
fmt.Fprintln(&buf, err)
}
return errors.New(buf.String())
}
return nil
}
type stringSlice []string
func (s *stringSlice) String() string {
if len(*s) == 0 {
return "<none>"
}
return strings.Join(*s, ", ")
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstraint, error) {
constraint := gps.ProjectConstraint{
Constraint: gps.Any(), // default to any; avoids panics later
}
// try to split on '@'
var versionStr string
atIndex := strings.Index(arg, "@")
if atIndex > 0 {
parts := strings.SplitN(arg, "@", 2)
arg = parts[0]
versionStr = parts[1]
constraint.Constraint = deduceConstraint(parts[1])
}
// TODO: What if there is no @, assume default branch (which may not be master) ?
// TODO: if we decide to keep equals.....
// split on colon if there is a network location
colonIndex := strings.Index(arg, ":")
if colonIndex > 0 {
parts := strings.SplitN(arg, ":", 2)
arg = parts[0]
constraint.Ident.Source = parts[1]
}
pr, err := sm.DeduceProjectRoot(arg)
if err != nil {
return constraint, errors.Wrapf(err, "could not infer project root from dependency path: %s", arg) // this should go through to the user
}
if string(pr) != arg {
return constraint, errors.Errorf("dependency path %s is not a project root, try %s instead", arg, pr)
}
constraint.Ident.ProjectRoot = gps.ProjectRoot(arg)
// Below we are checking if the constraint we deduced was valid.
if v, ok := constraint.Constraint.(gps.Version); ok && versionStr != "" {
if v.Type() == gps.IsVersion {
// we hit the fall through case in deduce constraint, let's call out to network
// and get the package's versions
versions, err := sm.ListVersions(constraint.Ident)
if err != nil {
return constraint, errors.Wrapf(err, "list versions for %s", arg) // means repo does not exist
}
var found bool
for _, version := range versions {
if versionStr == version.String() {
found = true
constraint.Constraint = version.Unpair()
break
}
}
if !found {
return constraint, errors.Errorf("%s is not a valid version for the package %s", versionStr, arg)
}
}
}
return constraint, nil
}
// deduceConstraint tries to puzzle out what kind of version is given in a string -
// semver, a revision, or as a fallback, a plain tag
func deduceConstraint(s string) gps.Constraint {
// always semver if we can
c, err := gps.NewSemverConstraint(s)
if err == nil {
return c
}
slen := len(s)
if slen == 40 {
if _, err = hex.DecodeString(s); err == nil {
// Whether or not it's intended to be a SHA1 digest, this is a
// valid byte sequence for that, so go with Revision. This
// covers git and hg
return gps.Revision(s)
}
}
// Next, try for bzr, which has a three-component GUID separated by
// dashes. There should be two, but the email part could contain
// internal dashes
if strings.Count(s, "-") >= 2 {
// Work from the back to avoid potential confusion from the email
i3 := strings.LastIndex(s, "-")
// Skip if - is last char, otherwise this would panic on bounds err
if slen == i3+1 {
return gps.NewVersion(s)
}
i2 := strings.LastIndex(s[:i3], "-")
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
// Getting this far means it'd pretty much be nuts if it's not a
// bzr rev, so don't bother parsing the email.
return gps.Revision(s)
}
}
// If not a plain SHA1 or bzr custom GUID, assume a plain version.
// TODO: if there is amgibuity here, then prompt the user?
return gps.NewVersion(s)
}
func checkErrors(m map[string]pkgtree.PackageOrErr) error {
noGoErrors, pkgErrors := 0, 0
for _, poe := range m {
if poe.Err != nil {
switch poe.Err.(type) {
case *build.NoGoError:
noGoErrors++
default:
pkgErrors++
}
}
}
if len(m) == 0 || len(m) == noGoErrors {
return errors.New("all dirs lacked any go code")
}
if len(m) == pkgErrors {
return errors.New("all dirs had go code with errors")
}
if len(m) == pkgErrors+noGoErrors {
return errors.Errorf("%d dirs had errors and %d had no go code", pkgErrors, noGoErrors)
}
return nil
}