forked from Masterminds/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
668 lines (583 loc) · 16.6 KB
/
config.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package cfg
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"sort"
"strconv"
"strings"
"github.com/Masterminds/vcs"
"github.com/sdboyer/gps"
"gopkg.in/yaml.v2"
)
// Config is the top-level configuration object.
type Config struct {
// Name is the name of the package or application.
Name string `yaml:"package"`
// Description is a short description for a package, application, or library.
// This description is similar but different to a Go package description as
// it is for marketing and presentation purposes rather than technical ones.
Description string `json:"description,omitempty"`
// Home is a url to a website for the package.
Home string `yaml:"homepage,omitempty"`
// License provides either a SPDX license or a path to a file containing
// the license. For more information on SPDX see http://spdx.org/licenses/.
// When more than one license an SPDX expression can be used.
License string `yaml:"license,omitempty"`
// Owners is an array of owners for a project. See the Owner type for
// more detail. These can be one or more people, companies, or other
// organizations.
Owners Owners `yaml:"owners,omitempty"`
// Ignore contains a list of packages to ignore fetching. This is useful
// when walking the package tree (including packages of packages) to list
// those to skip.
Ignore []string `yaml:"ignore,omitempty"`
// Imports contains a list of all dependency constraints for a project. For
// more detail on how these are captured see the Dependency type.
// TODO rename
// TODO mapify
Imports Dependencies `yaml:"dependencies"`
// DevImports contains the test or other development dependency constraints
// for a project. See the Dependency type for more details on how this is
// recorded.
// TODO rename
// TODO mapify
DevImports Dependencies `yaml:"testDependencies"`
}
// A transitive representation of a dependency for importing and exporting to yaml.
type cf struct {
Name string `yaml:"package"`
Description string `yaml:"description,omitempty"`
Home string `yaml:"homepage,omitempty"`
License string `yaml:"license,omitempty"`
Owners Owners `yaml:"owners,omitempty"`
Ignore []string `yaml:"ignore,omitempty"`
Imports Dependencies `yaml:"dependencies,omitempty"`
DevImports Dependencies `yaml:"testDependencies,omitempty"`
// these fields guarantee that this struct fails to unmarshal legacy yamls
Compat int `yaml:"import,omitempty"`
Compat2 int `yaml:"testImport,omitempty"`
}
// ConfigFromYaml returns an instance of Config from YAML
func ConfigFromYaml(yml []byte) (cfg *Config, legacy bool, err error) {
cfg = &Config{}
err = yaml.Unmarshal(yml, cfg)
if err != nil {
lcfg := &lConfig1{}
err = yaml.Unmarshal(yml, &lcfg)
if err == nil {
legacy = true
cfg, err = lcfg.Convert()
}
}
return
}
// Marshal converts a Config instance to YAML
func (c *Config) Marshal() ([]byte, error) {
yml, err := yaml.Marshal(&c)
if err != nil {
return []byte{}, err
}
return yml, nil
}
// UnmarshalYAML is a hook for gopkg.in/yaml.v2 in the unmarshalling process
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
newConfig := &cf{}
if err := unmarshal(&newConfig); err != nil {
return err
}
c.Name = newConfig.Name
c.Description = newConfig.Description
c.Home = newConfig.Home
c.License = newConfig.License
c.Owners = newConfig.Owners
c.Ignore = newConfig.Ignore
c.Imports = newConfig.Imports
c.DevImports = newConfig.DevImports
// Cleanup the Config object now that we have it.
err := c.DeDupe()
return err
}
// MarshalYAML is a hook for gopkg.in/yaml.v2 in the marshaling process
func (c *Config) MarshalYAML() (interface{}, error) {
newConfig := &cf{
Name: c.Name,
Description: c.Description,
Home: c.Home,
License: c.License,
Owners: c.Owners,
Ignore: c.Ignore,
}
i, err := c.Imports.Clone().DeDupe()
if err != nil {
return newConfig, err
}
di, err := c.DevImports.Clone().DeDupe()
if err != nil {
return newConfig, err
}
newConfig.Imports = i
newConfig.DevImports = di
return newConfig, nil
}
// HasDependency returns true if the given name is listed as an import or dev import.
func (c *Config) HasDependency(name string) bool {
for _, d := range c.Imports {
if d.Name == name {
return true
}
}
for _, d := range c.DevImports {
if d.Name == name {
return true
}
}
return false
}
// DependencyConstraints lists all the non-test dependency constraints
// described in a glide manifest in a way gps will understand.
func (c *Config) DependencyConstraints() []gps.ProjectConstraint {
return gpsifyDeps(c.Imports)
}
// TestDependencyConstraints lists all the test dependency constraints described
// in a glide manifest in a way gps will understand.
func (c *Config) TestDependencyConstraints() []gps.ProjectConstraint {
return gpsifyDeps(c.DevImports)
}
func gpsifyDeps(deps Dependencies) []gps.ProjectConstraint {
cp := make([]gps.ProjectConstraint, len(deps))
for k, d := range deps {
cp[k] = gps.ProjectConstraint{
Ident: gps.ProjectIdentifier{
ProjectRoot: gps.ProjectRoot(d.Name),
NetworkName: d.Repository,
},
Constraint: d.GetConstraint(),
}
}
return cp
}
func (c *Config) IgnorePackages() map[string]bool {
m := make(map[string]bool)
for _, ig := range c.Ignore {
m[ig] = true
}
return m
}
func (c *Config) Overrides() gps.ProjectConstraints {
return nil
}
// HasIgnore returns true if the given name is listed on the ignore list.
func (c *Config) HasIgnore(name string) bool {
for _, v := range c.Ignore {
// Check for both a name and to make sure sub-packages are ignored as
// well.
if v == name || strings.HasPrefix(name, v+"/") {
return true
}
}
return false
}
// Clone performs a deep clone of the Config instance
func (c *Config) Clone() *Config {
n := &Config{}
n.Name = c.Name
n.Description = c.Description
n.Home = c.Home
n.License = c.License
n.Owners = c.Owners.Clone()
n.Ignore = c.Ignore
n.Imports = c.Imports.Clone()
n.DevImports = c.DevImports.Clone()
return n
}
// WriteFile writes a Glide YAML file.
//
// This is a convenience function that marshals the YAML and then writes it to
// the given file. If the file exists, it will be clobbered.
func (c *Config) WriteFile(glidepath string) error {
o, err := c.Marshal()
if err != nil {
return err
}
return ioutil.WriteFile(glidepath, o, 0666)
}
// DeDupe consolidates duplicate dependencies on a Config instance
func (c *Config) DeDupe() error {
// Remove duplicates in the imports
var err error
c.Imports, err = c.Imports.DeDupe()
if err != nil {
return err
}
c.DevImports, err = c.DevImports.DeDupe()
if err != nil {
return err
}
// If the name on the config object is part of the imports remove it.
found := -1
for i, dep := range c.Imports {
if dep.Name == c.Name {
found = i
}
}
if found >= 0 {
c.Imports = append(c.Imports[:found], c.Imports[found+1:]...)
}
found = -1
for i, dep := range c.DevImports {
if dep.Name == c.Name {
found = i
}
}
if found >= 0 {
c.DevImports = append(c.DevImports[:found], c.DevImports[found+1:]...)
}
// If something is on the ignore list remove it from the imports.
for _, v := range c.Ignore {
found = -1
for k, d := range c.Imports {
if v == d.Name {
found = k
}
}
if found >= 0 {
c.Imports = append(c.Imports[:found], c.Imports[found+1:]...)
}
found = -1
for k, d := range c.DevImports {
if v == d.Name {
found = k
}
}
if found >= 0 {
c.DevImports = append(c.DevImports[:found], c.DevImports[found+1:]...)
}
}
return nil
}
// AddImport appends dependencies to the import list, deduplicating as we go.
func (c *Config) AddImport(deps ...*Dependency) error {
t := c.Imports
t = append(t, deps...)
t, err := t.DeDupe()
if err != nil {
return err
}
c.Imports = t
return nil
}
// Hash generates a sha256 hash for a given Config
func (c *Config) Hash() (string, error) {
yml, err := c.Marshal()
if err != nil {
return "", err
}
hash := sha256.New()
hash.Write(yml)
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
// Dependencies is a collection of Dependency
type Dependencies []*Dependency
// Get a dependency by name
func (d Dependencies) Get(name string) *Dependency {
for _, dep := range d {
if dep.Name == name {
return dep
}
}
return nil
}
// Has checks if a dependency is on a list of dependencies such as import or testImport
func (d Dependencies) Has(name string) bool {
for _, dep := range d {
if dep.Name == name {
return true
}
}
return false
}
// Remove removes a dependency from a list of dependencies
func (d Dependencies) Remove(name string) Dependencies {
found := -1
for i, dep := range d {
if dep.Name == name {
found = i
}
}
if found >= 0 {
copy(d[found:], d[found+1:])
d[len(d)-1] = nil
return d[:len(d)-1]
}
return d
}
// Clone performs a deep clone of Dependencies
func (d Dependencies) Clone() Dependencies {
n := make(Dependencies, 0, len(d))
for _, v := range d {
n = append(n, v.Clone())
}
return n
}
// DeDupe cleans up duplicates on a list of dependencies.
func (d Dependencies) DeDupe() (Dependencies, error) {
checked := map[string]int{}
imports := make(Dependencies, 0, 1)
i := 0
for _, dep := range d {
// The first time we encounter a dependency add it to the list
if val, ok := checked[dep.Name]; !ok {
checked[dep.Name] = i
imports = append(imports, dep)
i++
} else {
// In here we've encountered a dependency for the second time.
// Make sure the details are the same or return an error.
v := imports[val]
// Have to do string-based comparison
if dep.ConstraintsEq(*v) {
return d, fmt.Errorf("Import %s repeated with different versions '%s' and '%s'", dep.Name, dep.GetConstraint(), v.GetConstraint())
}
if dep.Repository != v.Repository {
return d, fmt.Errorf("Import %s repeated with different Repository details", dep.Name)
}
}
}
return imports, nil
}
// Dependency describes a package that the present package depends upon.
type Dependency struct {
Name string
VcsType string // TODO remove
Repository string
Branch string
Version string
}
// A transitive representation of a dependency for yaml import/export.
type dep struct {
Name string `yaml:"package"`
Version string `yaml:"version,omitempty"`
Branch string `yaml:"branch,omitempty"`
Repository string `yaml:"repo,omitempty"`
}
// DependencyFromLock converts a Lock to a Dependency
func DependencyFromLock(lock *Lock) *Dependency {
d := &Dependency{
Name: lock.Name,
Repository: lock.Repository,
}
// Because it's not allowed to have both, if we see both, prefer version
// over branch
if lock.Version != "" {
d.Version = lock.Version
} else if lock.Branch != "" {
d.Branch = lock.Branch
} else {
d.Version = lock.Revision
}
return d
}
// GetConstraint constructs an appropriate gps.Constraint from the Dependency's
// string input data.
func (d Dependency) GetConstraint() gps.Constraint {
// If neither or both Version and Branch are set, accept anything
if d.IsUnconstrained() {
return gps.Any()
} else if d.Version != "" {
return DeduceConstraint(d.Version)
} else {
// only case left is a non-empty branch
return gps.NewBranch(d.Branch)
}
}
// IsUnconstrained indicates if this dependency has no constraint information,
// version or branch.
func (d Dependency) IsUnconstrained() bool {
return (d.Version != "" && d.Branch != "") || (d.Version == "" && d.Branch == "")
}
// ConstraintsEq checks if the constraints on two Dependency are exactly equal.
func (d Dependency) ConstraintsEq(d2 Dependency) bool {
// Having both branch and version set is always an error, so if either have
// it, then return false
if (d.Version != "" && d.Branch != "") || (d2.Version != "" && d2.Branch != "") {
return false
}
// Neither being set, though, is OK
if (d.Version == "" && d.Branch == "") || (d2.Version == "" && d2.Branch == "") {
return true
}
// Now, xors
if d.Version != "" && d.Version == d2.Version {
return true
}
if d.Branch == d2.Branch {
return true
}
return false
}
// UnmarshalYAML is a hook for gopkg.in/yaml.v2 in the unmarshaling process
func (d *Dependency) UnmarshalYAML(unmarshal func(interface{}) error) error {
newDep := dep{}
err := unmarshal(&newDep)
if err != nil {
return err
}
if newDep.Version != "" && newDep.Branch != "" {
return fmt.Errorf("Cannot set both a both a branch and a version constraint for %q", d.Name)
}
d.Name = newDep.Name
d.Repository = newDep.Repository
d.Version = newDep.Version
d.Branch = newDep.Branch
return 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)
}
if _, err = hex.DecodeString(s[i3+1:]); err == nil {
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.
//
// svn, you ask? lol, madame. lol.
return gps.NewVersion(s)
}
// MarshalYAML is a hook for gopkg.in/yaml.v2 in the marshaling process
func (d *Dependency) MarshalYAML() (interface{}, error) {
newDep := &dep{
Name: d.Name,
Repository: d.Repository,
Version: d.Version,
Branch: d.Branch,
}
return newDep, nil
}
// GetRepo retrieves a Masterminds/vcs repo object configured for the root
// of the package being retrieved.
// TODO remove
func (d *Dependency) GetRepo(dest string) (vcs.Repo, error) {
// The remote location is either the configured repo or the package
// name as an https url.
var remote string
if len(d.Repository) > 0 {
remote = d.Repository
} else {
remote = "https://" + d.Name
}
// If the VCS type has a value we try that first.
if len(d.VcsType) > 0 && d.VcsType != "None" {
switch vcs.Type(d.VcsType) {
case vcs.Git:
return vcs.NewGitRepo(remote, dest)
case vcs.Svn:
return vcs.NewSvnRepo(remote, dest)
case vcs.Hg:
return vcs.NewHgRepo(remote, dest)
case vcs.Bzr:
return vcs.NewBzrRepo(remote, dest)
default:
return nil, fmt.Errorf("Unknown VCS type %s set for %s", d.VcsType, d.Name)
}
}
// When no type set we try to autodetect.
return vcs.NewRepo(remote, dest)
}
// Clone creates a clone of a Dependency
func (d *Dependency) Clone() *Dependency {
var d2 Dependency
d2 = *d
return &d2
}
// Owners is a list of owners for a project.
type Owners []*Owner
// Clone performs a deep clone of Owners
func (o Owners) Clone() Owners {
n := make(Owners, 0, 1)
for _, v := range o {
n = append(n, v.Clone())
}
return n
}
// Owner describes an owner of a package. This can be a person, company, or
// other organization. This is useful if someone needs to contact the
// owner of a package to address things like a security issue.
type Owner struct {
// Name describes the name of an organization.
Name string `yaml:"name,omitempty"`
// Email is an email address to reach the owner at.
Email string `yaml:"email,omitempty"`
// Home is a url to a website for the owner.
Home string `yaml:"homepage,omitempty"`
}
// Clone creates a clone of a Dependency
func (o *Owner) Clone() *Owner {
return &Owner{
Name: o.Name,
Email: o.Email,
Home: o.Home,
}
}
func stringArrayDeDupe(s []string, items ...string) []string {
for _, item := range items {
exists := false
for _, v := range s {
if v == item {
exists = true
}
}
if !exists {
s = append(s, item)
}
}
sort.Strings(s)
return s
}
func filterVcsType(vcs string) string {
switch vcs {
case "git", "hg", "bzr", "svn":
return vcs
case "mercurial":
return "hg"
case "bazaar":
return "bzr"
case "subversion":
return "svn"
default:
return ""
}
}
func normalizeSlash(k string) string {
return strings.Replace(k, "\\", "/", -1)
}