-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyperglide.go
210 lines (172 loc) · 4.25 KB
/
hyperglide.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/Masterminds/glide/action"
"github.com/Masterminds/glide/cache"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/repo"
"github.com/urfave/cli"
)
const (
BackupSuffix = ".hyperglide.hgbak"
)
var ErrAlreadyReportedError = fmt.Errorf("An error occurred")
func LoadGlideYaml(path string) (*cfg.Config, error) {
yml, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return cfg.ConfigFromYaml(yml)
}
func LoadGlideLock(path string) (*cfg.Lockfile, error) {
return cfg.ReadLockFile(path)
}
type LockMapper func(*cfg.Lock) *cfg.Dependency
func mergeImports(pins cfg.Dependencies, locks cfg.Locks, mapper LockMapper) cfg.Dependencies {
imports := map[string]*cfg.Dependency{}
merged := cfg.Dependencies{}
for _, lock := range locks {
dep := mapper(lock)
imports[dep.Name] = dep
merged = append(merged, dep)
}
for _, pin := range pins {
existing, ok := imports[pin.Name]
if !ok {
imports[pin.Name] = pin
merged = append(merged, pin)
continue
}
existing.Reference = pin.Reference
existing.VcsType = pin.VcsType
existing.Repository = pin.Repository
}
return merged
}
func doUpdate(gypath, glpath, home string, mapper LockMapper) error {
gy, err := LoadGlideYaml(gypath)
if err != nil {
return err
}
gl, err := LoadGlideLock(glpath)
if err != nil {
return err
}
hgy := &cfg.Config{
Name: gy.Name,
Imports: mergeImports(gy.Imports, gl.Imports, mapper),
DevImports: mergeImports(gy.DevImports, gl.DevImports, mapper),
}
err = os.Rename(gypath, gypath+BackupSuffix)
if err != nil {
return err
}
defer func() {
err = os.Rename(gypath+BackupSuffix, gypath)
if err != nil {
msg.Err("error restoring %s: %s", gypath, err.Error())
}
}()
hgyData, err := hgy.Marshal()
if err != nil {
return err
}
err = ioutil.WriteFile(gypath, hgyData, 0644)
if err != nil {
return err
}
installer := repo.NewInstaller()
installer.ResolveAllFiles = false
installer.ResolveTest = true
installer.Home = home
action.Update(installer, false, true)
if msg.HasErrored() {
return ErrAlreadyReportedError
}
return nil
}
func Update(c *cli.Context) error {
gypath := c.GlobalString("yaml")
glpath := c.GlobalString("lock")
home := c.GlobalString("home")
targets := map[string]struct{}{}
for _, name := range c.Args() {
targets[name] = struct{}{}
}
return doUpdate(gypath, glpath, home, func(lock *cfg.Lock) *cfg.Dependency {
dep := cfg.DependencyFromLock(lock)
_, ok := targets[dep.Name]
if len(c.Args()) == 0 || ok {
dep.Reference = "origin/master"
}
return dep
})
}
func NewDep(c *cli.Context) error {
gypath := c.GlobalString("yaml")
glpath := c.GlobalString("lock")
home := c.GlobalString("home")
return doUpdate(gypath, glpath, home, cfg.DependencyFromLock)
}
const usage = `A wrapper around glide to enable a workflow where:
- every dependency is frequently updated
- individual dependencies can be easily added`
func main() {
app := cli.NewApp()
app.Name = "hyperglide"
app.Usage = usage
app.Version = "master"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "yaml, y",
Value: "glide.yaml",
Usage: "Path to a glide.yaml configuration",
},
cli.StringFlag{
Name: "lock, l",
Value: "glide.lock",
Usage: "Path to the Glide lock file",
},
cli.StringFlag{
Name: "home",
Value: gpath.Home(),
Usage: "The location of Glide files",
EnvVar: "GLIDE_HOME",
},
}
app.Before = startup
app.After = shutdown
app.Commands = []cli.Command{
{
Name: "update",
ShortName: "up",
Usage: "Update everything - arguments are treated as an exclusive list of packages to update",
Action: Update,
},
{
Name: "newdep",
Usage: "Vendor new dependencies, don't update the others",
Action: NewDep,
},
}
err := app.Run(os.Args)
if err != nil {
if err != ErrAlreadyReportedError {
fmt.Fprintf(os.Stderr, "error: %s", err.Error())
}
os.Exit(1)
}
}
func startup(c *cli.Context) error {
action.Init(c.GlobalString("yaml"), c.GlobalString("home"))
action.EnsureGoVendor()
return nil
}
func shutdown(c *cli.Context) error {
cache.SystemUnlock()
return nil
}