-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_resources.go
306 lines (227 loc) · 5.72 KB
/
cmd_resources.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
git "gopkg.in/src-d/go-git.v4"
cli "gopkg.in/urfave/cli.v1"
)
// A Resource contains data related to a local resource.
type Resource struct {
Name string
Path string
Manifest *Manifest
}
func gatherResources(root string) []*Resource {
resourceList := []*Resource{}
filepath.Walk(root, func(p string, f os.FileInfo, err error) error {
if err == nil {
if f.IsDir() {
manifest, err := openManifest(p)
if err == nil {
_, name := filepath.Split(p)
resourceList = append(resourceList, &Resource{
Name: name,
Path: p,
Manifest: manifest,
})
}
}
return nil
}
return err
})
return resourceList
}
func dirExists(dirName string) (bool, error) {
stat, err := os.Stat(dirName)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if !stat.IsDir() {
return false, os.ErrExist
}
return true, nil
}
func cloneRepo(repoURI string, outRoot string) error {
// clone the repository
_, err := git.PlainClone(outRoot, false, &git.CloneOptions{
URL: repoURI,
})
if err != nil {
fmt.Printf("error while cloning %v: %v\n", repoURI, err)
return err
}
return nil
}
func fetchRepo(outRoot string, force bool) error {
repo, err := git.PlainOpen(outRoot)
if err != nil {
fmt.Printf("error while opening git repo %v: %v\n", outRoot, err)
return err
}
worktree, err := repo.Worktree()
if err != nil {
fmt.Printf("error while opening git repo %v: %v\n", outRoot, err)
return err
}
status, err := worktree.Status()
if err != nil {
fmt.Printf("error while getting git repo %v status: %v\n", outRoot, err)
return err
}
if !status.IsClean() && !force {
fmt.Printf("can't update %v, repo changes below:\n", outRoot)
fmt.Printf("%v", status.String())
fmt.Printf("use --force flag to bypass")
return errors.New("Git repository is dirty, can't update")
}
err = worktree.Pull(&git.PullOptions{
RemoteName: "origin",
})
if err != nil && err != git.NoErrAlreadyUpToDate {
fmt.Printf("error while pulling git repo %v: %v\n", outRoot, err)
return err
}
return nil
}
func addPackage(packageURI string, force bool) ([]*Resource, error) {
repoURI := packageURI
// split the package name part from the repo URI
_, packageName := path.Split(repoURI)
// split any . from the package name
packageName = strings.Split(packageName, ".")[0]
// format a root path of the form resources/[repoName]/
packageRoot := fmt.Sprintf("[%v]", strings.ToLower(packageName))
outRoot := filepath.Join("resources", packageRoot)
// check for existence
exists, err := dirExists(outRoot)
if err != nil {
return nil, err
}
// fetch/clone depending on existence
if exists {
err = fetchRepo(outRoot, force)
} else {
err = cloneRepo(repoURI, outRoot)
}
if err != nil {
return nil, err
}
// gather resources in the repository
resourceList := gatherResources(outRoot)
// gather dependencies
dependencies := map[string]bool{}
for _, resource := range resourceList {
deps := resource.Manifest.GetAll("dependency_url")
for _, dep := range deps {
dependencies[dep] = true
}
}
// fetch dependencies
for dep := range dependencies {
localList, err := addPackage(dep, force)
if err != nil {
return nil, err
}
resourceList = append(localList, resourceList...)
}
return resourceList, nil
}
func parseResourceLines(file string) ([]string, error) {
fileData, err := ioutil.ReadFile(file)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
// if not existent, assume an empty file
if err != nil {
fileData = []byte{}
}
fileText := string(fileData)
lines := strings.Split(fileText, "\n")
// read any 'start' lines
resources := []string{}
for _, line := range lines {
if strings.HasPrefix(line, "start ") {
splitLine := strings.Split(line, " ")
if len(splitLine) >= 2 {
resources = append(resources, strings.TrimSpace(splitLine[1]))
}
}
}
return resources, nil
}
func writeResourceLines(file string, lines []string) error {
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
fmt.Fprintf(writer, "# THIS FILE IS AUTOMATICALLY-GENERATED\r\n")
fmt.Fprintf(writer, "# DO NOT ADD ANYTHING OTHER THAN `start` LINES\r\n")
for _, line := range lines {
fmt.Fprintf(writer, "start %v\r\n", line)
}
writer.Flush()
err := ioutil.WriteFile(file, buffer.Bytes(), 0644)
return err
}
func addResources(resources []*Resource, file string) error {
// add lines to resource configuration
resourceLines, err := parseResourceLines(file)
if err != nil {
return err
}
// try finding/adding each resource
for _, resource := range resources {
// loop through each line to see if we found any
found := false
for _, line := range resourceLines {
if line == resource.Name {
found = true
break
}
}
if !found {
resourceLines = append(resourceLines, resource.Name)
}
}
err = writeResourceLines(file, resourceLines)
return err
}
func cmdAdd(ctx *cli.Context) error {
// get the intended package URI
packageURI := ctx.Args().First()
if len(strings.TrimSpace(packageURI)) == 0 {
return nil
}
// fetch the package
resources, err := addPackage(packageURI, ctx.Bool("force"))
if err != nil {
return err
}
err = addResources(resources, ctx.String("config-file"))
if err != nil {
return err
}
return nil
}
func cmdGet(ctx *cli.Context) error {
// get the intended package URI
packageURI := ctx.Args().First()
if len(strings.TrimSpace(packageURI)) == 0 {
return nil
}
// fetch the package
resources, err := addPackage(packageURI, ctx.Bool("force"))
for _, resource := range resources {
fmt.Printf("- %s\n", resource.Name)
}
return err
}