forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
335 lines (297 loc) · 7.97 KB
/
file.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
// 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 vendorfile is the meta-data file for vendoring.
// Round-trips unknown fields.
// It will also allow moving the vendor file to new locations.
package vendorfile
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"sort"
)
// Name of the vendor file.
const Name = "vendor.json"
// File is the structure of the vendor file.
type File struct {
RootPath string // Import path of vendor folder
Comment string
Ignore string
Package []*Package
// all preserves unknown values.
all map[string]interface{}
}
// Package represents each package.
type Package struct {
field map[string]interface{}
// If delete is set to true the package will not be written to the vendor file.
Remove bool
// If new is set to true the package will be treated as a new package to the file.
Add bool
// See the vendor spec for definitions.
Origin string
Path string
Tree bool
Revision string
RevisionTime string
Version string
VersionExact string
ChecksumSHA1 string
Comment string
}
func (pkg *Package) PathOrigin() string {
if len(pkg.Origin) > 0 {
return pkg.Origin
}
return pkg.Path
}
// The following stringer functions are useful for debugging.
type packageList []*Package
func (list packageList) String() string {
buf := &bytes.Buffer{}
for _, item := range list {
buf.WriteString("\t")
buf.WriteString(fmt.Sprintf("(%v) ", item.field))
if item.Remove {
buf.WriteString(" X ")
}
buf.WriteString(item.Path)
buf.WriteRune('\n')
}
buf.WriteRune('\n')
return buf.String()
}
var (
rootPathNames = []string{"rootPath"}
packageNames = []string{"package", "Package"}
ignoreNames = []string{"ignore"}
originNames = []string{"origin"}
pathNames = []string{"path", "canonical", "Canonical", "vendor", "Vendor"}
treeNames = []string{"tree"}
revisionNames = []string{"revision", "Revision", "version", "Version"}
revisionTimeNames = []string{"revisionTime", "RevisionTime", "versionTime", "VersionTime"}
versionNames = []string{"version"}
versionExactNames = []string{"versionExact"}
checksumSHA1Names = []string{"checksumSHA1"}
commentNames = []string{"comment", "Comment"}
)
type vendorPackageSort []interface{}
func (vp vendorPackageSort) Len() int { return len(vp) }
func (vp vendorPackageSort) Swap(i, j int) { vp[i], vp[j] = vp[j], vp[i] }
func (vp vendorPackageSort) Less(i, j int) bool {
a := vp[i].(map[string]interface{})
b := vp[j].(map[string]interface{})
aPath, _ := a[pathNames[0]].(string)
bPath, _ := b[pathNames[0]].(string)
if aPath == bPath {
aOrigin, _ := a[originNames[0]].(string)
bOrigin, _ := b[originNames[0]].(string)
return len(aOrigin) > len(bOrigin)
}
return aPath < bPath
}
func setField(fieldObj interface{}, object map[string]interface{}, names []string) {
loop:
for _, name := range names {
raw, found := object[name]
if !found {
continue
}
switch field := fieldObj.(type) {
default:
panic("unknown type")
case *string:
value, is := raw.(string)
if !is {
continue loop
}
*field = value
if len(value) != 0 {
break loop
}
case *bool:
value, is := raw.(bool)
if !is {
continue loop
}
*field = value
if value {
break loop
}
}
}
}
func setObject(fieldObj interface{}, object map[string]interface{}, names []string, hideEmpty bool) {
switch field := fieldObj.(type) {
default:
panic("unknown type")
case string:
for i, name := range names {
if i != 0 || (hideEmpty && len(field) == 0) {
delete(object, name)
continue
}
object[name] = field
}
case bool:
for i, name := range names {
if i != 0 || (hideEmpty && !field) {
delete(object, name)
continue
}
object[name] = field
}
}
}
// getRawPackageList gets the array of items from all object.
func (vf *File) getRawPackageList() []interface{} {
var rawPackageList []interface{}
for index, name := range packageNames {
rawPackageListObject, found := vf.all[name]
if !found {
continue
}
if index != 0 {
vf.all[packageNames[0]] = rawPackageListObject
delete(vf.all, name)
}
var is bool
rawPackageList, is = rawPackageListObject.([]interface{})
if is {
break
}
}
return rawPackageList
}
// toFields moves values from "all" to the field values.
func (vf *File) toFields() {
setField(&vf.RootPath, vf.all, rootPathNames)
setField(&vf.Comment, vf.all, commentNames)
setField(&vf.Ignore, vf.all, ignoreNames)
rawPackageList := vf.getRawPackageList()
vf.Package = make([]*Package, len(rawPackageList))
for index, rawPackage := range rawPackageList {
object, is := rawPackage.(map[string]interface{})
if !is {
continue
}
pkg := &Package{}
vf.Package[index] = pkg
pkg.field = object
setField(&pkg.Origin, object, originNames)
setField(&pkg.Path, object, pathNames)
setField(&pkg.Tree, object, treeNames)
setField(&pkg.Revision, object, revisionNames)
setField(&pkg.RevisionTime, object, revisionTimeNames)
setField(&pkg.Version, object, versionNames)
setField(&pkg.VersionExact, object, versionExactNames)
setField(&pkg.ChecksumSHA1, object, checksumSHA1Names)
setField(&pkg.Comment, object, commentNames)
}
}
// toAll moves values from field values to "all".
func (vf *File) toAll() {
delete(vf.all, "Tool")
setObject(vf.RootPath, vf.all, rootPathNames, true)
setObject(vf.Comment, vf.all, commentNames, false)
setObject(vf.Ignore, vf.all, ignoreNames, false)
rawPackageList := vf.getRawPackageList()
setPkgFields := func(pkg *Package) {
if pkg.Origin == pkg.Path {
pkg.Origin = ""
}
if pkg.field == nil {
pkg.field = make(map[string]interface{}, 10)
}
setObject(pkg.Origin, pkg.field, originNames, true)
setObject(pkg.Path, pkg.field, pathNames, false)
setObject(pkg.Tree, pkg.field, treeNames, true)
setObject(pkg.Revision, pkg.field, revisionNames, false)
setObject(pkg.RevisionTime, pkg.field, revisionTimeNames, true)
setObject(pkg.Version, pkg.field, versionNames, true)
setObject(pkg.VersionExact, pkg.field, versionExactNames, true)
setObject(pkg.ChecksumSHA1, pkg.field, checksumSHA1Names, true)
setObject(pkg.Comment, pkg.field, commentNames, true)
}
for i := len(vf.Package) - 1; i >= 0; i-- {
pkg := vf.Package[i]
switch {
case pkg.Remove:
for index, rawObj := range rawPackageList {
raw, is := rawObj.(map[string]interface{})
if !is {
continue
}
same := true
for key, value := range pkg.field {
if raw[key] != value {
same = false
break
}
}
if same {
rawPackageList[index] = nil
}
}
case pkg.Add:
setPkgFields(pkg)
rawPackageList = append(rawPackageList, pkg.field)
default:
if pkg.field == nil {
pkg.field = make(map[string]interface{}, 10)
}
delete(pkg.field, "local")
delete(pkg.field, "Local")
setPkgFields(pkg)
}
}
nextRawPackageList := make([]interface{}, 0, len(rawPackageList))
for _, raw := range rawPackageList {
if raw == nil {
continue
}
nextRawPackageList = append(nextRawPackageList, raw)
}
vf.all[packageNames[0]] = nextRawPackageList
}
// Marshal the vendor file to the specified writer.
// Retains read fields.
func (vf *File) Marshal(w io.Writer) error {
if vf.all == nil {
vf.all = map[string]interface{}{}
}
vf.toAll()
rawList := vf.getRawPackageList()
sort.Sort(vendorPackageSort(rawList))
jb, err := json.Marshal(vf.all)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = json.Indent(buf, jb, "", "\t")
if err != nil {
return err
}
_, err = io.Copy(w, buf)
return err
}
// Unmarshal the vendor file from the specified reader.
// Stores internally all fields.
func (vf *File) Unmarshal(r io.Reader) error {
bb, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if vf.all == nil {
vf.all = make(map[string]interface{}, 3)
}
err = json.Unmarshal(bb, &vf.all)
if err != nil {
return err
}
vf.toFields()
return nil
}