-
Notifications
You must be signed in to change notification settings - Fork 0
/
glock.go
97 lines (88 loc) · 1.9 KB
/
glock.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
// 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 migrate
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/kardianos/govendor/context"
"github.com/kardianos/govendor/pkgspec"
)
func init() {
register("glock", sysGlock{})
}
type sysGlock struct{}
func (sys sysGlock) Check(root string) (system, error) {
if hasFiles(root, "GLOCKFILE") {
return sys, nil
}
return nil, nil
}
func (sysGlock) Migrate(root string) error {
err := os.MkdirAll(filepath.Join(root, "vendor"), 0777)
if err != nil {
return err
}
filebytes, err := ioutil.ReadFile(filepath.Join(root, "GLOCKFILE"))
if err != nil {
return err
}
lines := strings.Split(string(filebytes), "\n")
for i, l := range lines {
lines[i] = strings.TrimSpace(l)
}
/*
vf := &vendorfile.File{}
vf.Package = make([]*vendorfile.Package, 0, len(lines))
*/
ctx, err := context.NewContext(root, filepath.Join("vendor", "vendor.json"), "vendor", false)
if err != nil {
return err
}
const cmdPrefix = "cmd "
for _, l := range lines {
if len(l) == 0 {
continue
}
isCmd := strings.HasPrefix(l, cmdPrefix)
if isCmd {
continue
}
field := strings.Fields(l)
if len(field) < 2 {
continue
}
ps, err := pkgspec.Parse("", field[0]+"@"+field[1])
if err != nil {
return err
}
ps.IncludeTree = true
err = ctx.ModifyImport(ps, context.Fetch)
if err != nil {
return err
}
}
for _, l := range lines {
if len(l) == 0 {
continue
}
isCmd := strings.HasPrefix(l, cmdPrefix)
if !isCmd {
continue
}
path := strings.TrimPrefix(l, cmdPrefix)
ps, err := pkgspec.Parse("", path)
if err != nil {
return err
}
err = ctx.ModifyImport(ps, context.Fetch)
if err != nil {
return err
}
}
err = ctx.WriteVendorFile()
os.Remove(filepath.Join(root, "GLOCKFILE"))
return err
}