forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest.go
293 lines (252 loc) · 7.92 KB
/
manifest.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
// 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 dep
import (
"bytes"
"fmt"
"io"
"reflect"
"sort"
"github.com/golang/dep/internal/gps"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
const ManifestName = "Gopkg.toml"
type Manifest struct {
Dependencies gps.ProjectConstraints
Ovr gps.ProjectConstraints
Ignored []string
Required []string
}
type rawManifest struct {
Dependencies []rawProject `toml:"dependencies,omitempty"`
Overrides []rawProject `toml:"overrides,omitempty"`
Ignored []string `toml:"ignored,omitempty"`
Required []string `toml:"required,omitempty"`
}
type rawProject struct {
Name string `toml:"name"`
Branch string `toml:"branch,omitempty"`
Revision string `toml:"revision,omitempty"`
Version string `toml:"version,omitempty"`
Source string `toml:"source,omitempty"`
}
func validateManifest(s string) ([]error, error) {
var errs []error
// Load the TomlTree from string
tree, err := toml.Load(s)
if err != nil {
return errs, errors.Wrap(err, "Unable to load TomlTree from string")
}
// Convert tree to a map
manifest := tree.ToMap()
// Look for unknown fields and collect errors
for prop, val := range manifest {
switch prop {
case "metadata":
// Check if metadata is of Map type
if reflect.TypeOf(val).Kind() != reflect.Map {
errs = append(errs, errors.New("metadata should be a TOML table"))
}
case "dependencies", "overrides":
// Invalid if type assertion fails. Not a TOML array of tables.
if rawProj, ok := val.([]interface{}); ok {
// Iterate through each array of tables
for _, v := range rawProj {
// Check the individual field's key to be valid
for key, value := range v.(map[string]interface{}) {
// Check if the key is valid
switch key {
case "name", "branch", "revision", "version", "source":
// valid key
case "metadata":
// Check if metadata is of Map type
if reflect.TypeOf(value).Kind() != reflect.Map {
errs = append(errs, fmt.Errorf("metadata in %q should be a TOML table", prop))
}
default:
// unknown/invalid key
errs = append(errs, fmt.Errorf("Invalid key %q in %q", key, prop))
}
}
}
} else {
errs = append(errs, fmt.Errorf("%v should be a TOML array of tables", prop))
}
case "ignored", "required":
default:
errs = append(errs, fmt.Errorf("Unknown field in manifest: %v", prop))
}
}
return errs, nil
}
// readManifest returns a Manifest read from r and a slice of validation warnings.
func readManifest(r io.Reader) (*Manifest, []error, error) {
buf := &bytes.Buffer{}
_, err := buf.ReadFrom(r)
if err != nil {
return nil, nil, errors.Wrap(err, "Unable to read byte stream")
}
warns, err := validateManifest(buf.String())
if err != nil {
return nil, warns, errors.Wrap(err, "Manifest validation failed")
}
raw := rawManifest{}
err = toml.Unmarshal(buf.Bytes(), &raw)
if err != nil {
return nil, warns, errors.Wrap(err, "Unable to parse the manifest as TOML")
}
m, err := fromRawManifest(raw)
return m, warns, err
}
func fromRawManifest(raw rawManifest) (*Manifest, error) {
m := &Manifest{
Dependencies: make(gps.ProjectConstraints, len(raw.Dependencies)),
Ovr: make(gps.ProjectConstraints, len(raw.Overrides)),
Ignored: raw.Ignored,
Required: raw.Required,
}
for i := 0; i < len(raw.Dependencies); i++ {
name, prj, err := toProject(raw.Dependencies[i])
if err != nil {
return nil, err
}
if _, exists := m.Dependencies[name]; exists {
return nil, errors.Errorf("multiple dependencies specified for %s, can only specify one", name)
}
m.Dependencies[name] = prj
}
for i := 0; i < len(raw.Overrides); i++ {
name, prj, err := toProject(raw.Overrides[i])
if err != nil {
return nil, err
}
m.Ovr[name] = prj
}
return m, nil
}
// toProject interprets the string representations of project information held in
// a rawProject, converting them into a proper gps.ProjectProperties. An
// error is returned if the rawProject contains some invalid combination -
// for example, if both a branch and version constraint are specified.
func toProject(raw rawProject) (n gps.ProjectRoot, pp gps.ProjectProperties, err error) {
n = gps.ProjectRoot(raw.Name)
if raw.Branch != "" {
if raw.Version != "" || raw.Revision != "" {
return n, pp, errors.Errorf("multiple constraints specified for %s, can only specify one", n)
}
pp.Constraint = gps.NewBranch(raw.Branch)
} else if raw.Version != "" {
if raw.Revision != "" {
return n, pp, errors.Errorf("multiple constraints specified for %s, can only specify one", n)
}
// always semver if we can
pp.Constraint, err = gps.NewSemverConstraint(raw.Version)
if err != nil {
// but if not, fall back on plain versions
pp.Constraint = gps.NewVersion(raw.Version)
}
} else if raw.Revision != "" {
pp.Constraint = gps.Revision(raw.Revision)
} else {
// If the user specifies nothing, it means an open constraint (accept
// anything).
pp.Constraint = gps.Any()
}
pp.Source = raw.Source
return n, pp, nil
}
// toRaw converts the manifest into a representation suitable to write to the manifest file
func (m *Manifest) toRaw() rawManifest {
raw := rawManifest{
Dependencies: make([]rawProject, 0, len(m.Dependencies)),
Overrides: make([]rawProject, 0, len(m.Ovr)),
Ignored: m.Ignored,
Required: m.Required,
}
for n, prj := range m.Dependencies {
raw.Dependencies = append(raw.Dependencies, toRawProject(n, prj))
}
sort.Sort(sortedRawProjects(raw.Dependencies))
for n, prj := range m.Ovr {
raw.Overrides = append(raw.Overrides, toRawProject(n, prj))
}
sort.Sort(sortedRawProjects(raw.Overrides))
return raw
}
type sortedRawProjects []rawProject
func (s sortedRawProjects) Len() int { return len(s) }
func (s sortedRawProjects) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortedRawProjects) Less(i, j int) bool {
l, r := s[i], s[j]
if l.Name < r.Name {
return true
}
if r.Name < l.Name {
return false
}
return l.Source < r.Source
}
func (m *Manifest) MarshalTOML() ([]byte, error) {
raw := m.toRaw()
result, err := toml.Marshal(raw)
return result, errors.Wrap(err, "Unable to marshal the lock to a TOML string")
}
func toRawProject(name gps.ProjectRoot, project gps.ProjectProperties) rawProject {
raw := rawProject{
Name: string(name),
Source: project.Source,
}
if v, ok := project.Constraint.(gps.Version); ok {
switch v.Type() {
case gps.IsRevision:
raw.Revision = v.String()
case gps.IsBranch:
raw.Branch = v.String()
case gps.IsSemver, gps.IsVersion:
raw.Version = v.String()
}
return raw
}
// We simply don't allow for a case where the user could directly
// express a 'none' constraint, so we can ignore it here. We also ignore
// the 'any' case, because that's the other possibility, and it's what
// we interpret not having any constraint expressions at all to mean.
// if !gps.IsAny(pp.Constraint) && !gps.IsNone(pp.Constraint) {
if !gps.IsAny(project.Constraint) && project.Constraint != nil {
// Has to be a semver range.
raw.Version = project.Constraint.String()
}
return raw
}
func (m *Manifest) DependencyConstraints() gps.ProjectConstraints {
return m.Dependencies
}
func (m *Manifest) TestDependencyConstraints() gps.ProjectConstraints {
// TODO decide whether we're going to incorporate this or not
return nil
}
func (m *Manifest) Overrides() gps.ProjectConstraints {
return m.Ovr
}
func (m *Manifest) IgnoredPackages() map[string]bool {
if len(m.Ignored) == 0 {
return nil
}
mp := make(map[string]bool, len(m.Ignored))
for _, i := range m.Ignored {
mp[i] = true
}
return mp
}
func (m *Manifest) RequiredPackages() map[string]bool {
if len(m.Required) == 0 {
return nil
}
mp := make(map[string]bool, len(m.Required))
for _, i := range m.Required {
mp[i] = true
}
return mp
}