forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glide.go
100 lines (90 loc) · 2.41 KB
/
glide.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
// Copyright 2015 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 migrate
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/kardianos/govendor/context"
"github.com/kardianos/govendor/vendorfile"
"gopkg.in/yaml.v2"
)
func init() {
register("glide", sysGlide{})
}
type sysGlide struct{}
func (sys sysGlide) Check(root string) (system, error) {
// Glide has two config files: glide.yaml and glide.lock. The
// first file is for manual configuration. The second file is
// autogenerated from the first one. Migration procedure uses
// autogenerated glide.lock because it has resolved recursive
// dependencies that glide make automatically from glide.yaml.
if hasFiles(root, "glide.lock") {
return sys, nil
}
return nil, nil
}
func (sys sysGlide) Migrate(root string) error {
// Create a new empty config.
ctx, err := context.NewContext(root, filepath.Join("vendor", "vendor.json"), "vendor", false)
if err != nil {
return err
}
ctx.VendorDiscoverFolder = "vendor"
ctx.VendorFile.Ignore = "test"
// Get&parse glide' config.
rawConfigData, err := ioutil.ReadFile(filepath.Join(root, "glide.lock"))
if err != nil {
return err
}
type (
imports struct {
Name string `json:"name"`
Version string `json:"version"`
Repo string `json:"repo,omitempty"`
Subpackages []string `json:"subpackages,omitempty"`
}
glideLock struct {
Imports []imports `json:"imports"`
}
)
parsedConfig := glideLock{}
err = yaml.Unmarshal(rawConfigData, &parsedConfig)
if err != nil {
return err
}
// Build a new config.
for _, i := range parsedConfig.Imports {
pkg := vendorfile.Package{
Add: true,
Path: i.Name,
Revision: i.Version,
}
if len(i.Subpackages) > 0 {
for _, p := range i.Subpackages {
subpkg := vendorfile.Package{
Add: true,
Path: path.Join(i.Name, p),
Revision: i.Version,
}
if i.Repo != "" {
subpkg.Origin = path.Join(i.Repo, p)
}
ctx.VendorFile.Package = append(ctx.VendorFile.Package, &subpkg)
}
}
if i.Repo != "" {
pkg.Origin = i.Repo
}
ctx.VendorFile.Package = append(ctx.VendorFile.Package, &pkg)
}
err = ctx.WriteVendorFile()
if err != nil {
return err
}
// Cleanup.
os.RemoveAll(filepath.Join(root, "glide.yaml"))
return os.RemoveAll(filepath.Join(root, "glide.lock"))
}