-
Notifications
You must be signed in to change notification settings - Fork 308
/
types.go
326 lines (274 loc) · 7.97 KB
/
types.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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package v1manifest
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/pingcap/tiup/pkg/utils"
"golang.org/x/mod/semver"
)
// Manifest representation for ser/de.
type Manifest struct {
// Signatures value
Signatures []Signature `json:"signatures"`
// Signed value; any value here must have the SignedBase base.
Signed ValidManifest `json:"signed"`
}
// RawManifest representation for ser/de.
type RawManifest struct {
// Signatures value
Signatures []Signature `json:"signatures"`
// Signed value; raw json message
Signed json.RawMessage `json:"signed"`
}
// Signature represents a signature for a manifest
type Signature struct {
KeyID string `json:"keyid"`
Sig string `json:"sig"`
}
// SignedBase represents parts of a manifest's signed value which are shared by all manifests.
type SignedBase struct {
Ty string `json:"_type"`
SpecVersion string `json:"spec_version"`
Expires string `json:"expires"`
// 0 => no version specified
Version uint `json:"version"`
}
// SetExpiresAt set manifest expires at the specified time.
func (s *SignedBase) SetExpiresAt(t time.Time) {
s.Expires = t.Format(time.RFC3339)
}
// ValidManifest is a manifest which includes SignedBase and can be validated.
type ValidManifest interface {
isValid() error
// Base returns this manifest's SignedBase which is values common to all manifests.
Base() *SignedBase
// Filename returns the unversioned name that the manifest should be saved as based on its Go type.
Filename() string
}
// Root manifest.
type Root struct {
SignedBase
Roles map[string]*Role `json:"roles"`
}
// Role object.
type Role struct {
URL string `json:"url"`
Keys map[string]*KeyInfo `json:"keys"`
Threshold uint `json:"threshold"`
}
// KeyInfo is the manifest structure of a single key
type KeyInfo struct {
Type string `json:"keytype"`
Value map[string]string `json:"keyval"`
Scheme string `json:"scheme"`
}
// Index manifest.
type Index struct {
SignedBase
Owners map[string]Owner `json:"owners"`
Components map[string]ComponentItem `json:"components"`
DefaultComponents []string `json:"default_components"`
}
// Owner object.
type Owner struct {
Name string `json:"name"`
Keys map[string]*KeyInfo `json:"keys"`
Threshold int `json:"threshold"`
}
// VersionItem is the manifest structure of a version of a component
type VersionItem struct {
URL string `json:"url"`
Yanked bool `json:"yanked"`
Entry string `json:"entry"`
Released string `json:"released"`
Dependencies map[string]string `json:"dependencies"`
FileHash
}
// Component manifest.
type Component struct {
SignedBase
ID string `json:"id"`
Description string `json:"description"`
Nightly string `json:"nightly"` // version of the latest daily build
// platform -> version -> VersionItem
Platforms map[string]map[string]VersionItem `json:"platforms"`
}
// ComponentItem object
type ComponentItem struct {
Yanked bool `json:"yanked"`
Owner string `json:"owner"`
URL string `json:"url"`
Standalone bool `json:"standalone"`
Hidden bool `json:"hidden"`
}
// Snapshot manifest.
type Snapshot struct {
SignedBase
Meta map[string]FileVersion `json:"meta"`
}
// Timestamp manifest.
type Timestamp struct {
SignedBase
Meta map[string]FileHash `json:"meta"`
}
// FileHash is the hashes and length of a file.
type FileHash struct {
Hashes map[string]string `json:"hashes"`
Length uint `json:"length"`
}
// FileVersion is just a version number.
type FileVersion struct {
Version uint `json:"version"`
Length uint `json:"length"`
}
// Boilerplate implementations of ValidManifest.
// Base implements ValidManifest
func (manifest *Root) Base() *SignedBase {
return &manifest.SignedBase
}
// Base implements ValidManifest
func (manifest *Index) Base() *SignedBase {
return &manifest.SignedBase
}
// Base implements ValidManifest
func (manifest *Component) Base() *SignedBase {
return &manifest.SignedBase
}
// Base implements ValidManifest
func (manifest *Snapshot) Base() *SignedBase {
return &manifest.SignedBase
}
// Base implements ValidManifest
func (manifest *Timestamp) Base() *SignedBase {
return &manifest.SignedBase
}
// Filename implements ValidManifest
func (manifest *Root) Filename() string {
return ManifestFilenameRoot
}
// Filename implements ValidManifest
func (manifest *Index) Filename() string {
return ManifestFilenameIndex
}
// ComponentList returns non-yanked components
func (manifest *Index) ComponentList() map[string]ComponentItem {
components := make(map[string]ComponentItem)
for n, c := range manifest.Components {
if c.Yanked {
continue
}
components[n] = c
}
return components
}
// ComponentListWithYanked return all components
func (manifest *Index) ComponentListWithYanked() map[string]ComponentItem {
return manifest.Components
}
// Filename implements ValidManifest
func (manifest *Component) Filename() string {
return manifest.ID + ".json"
}
// Filename implements ValidManifest
func (manifest *Snapshot) Filename() string {
return ManifestFilenameSnapshot
}
// Filename implements ValidManifest
func (manifest *Timestamp) Filename() string {
return ManifestFilenameTimestamp
}
// HasNightly return true if the component has nightly version.
func (manifest *Component) HasNightly(platform string) bool {
if manifest.Nightly == "" {
return false
}
v, ok := manifest.Platforms[platform][manifest.Nightly]
if !ok {
return false
}
return !v.Yanked
}
// VersionList return all versions exclude yanked versions
func (manifest *Component) VersionList(platform string) map[string]VersionItem {
versions := make(map[string]VersionItem)
vs := manifest.VersionListWithYanked(platform)
if vs == nil {
return nil
}
for v, vi := range vs {
if vi.Yanked {
continue
}
versions[v] = vi
}
return versions
}
// LatestVersion return the latest version exclude yanked versions
func (manifest *Component) LatestVersion(platform string) string {
versions := manifest.VersionList(platform)
if versions == nil {
return ""
}
var latest string
for v := range versions {
if utils.Version(v).IsNightly() {
continue
}
if latest == "" || semver.Compare(v, latest) > 0 {
latest = v
}
}
return latest
}
// VersionListWithYanked return all versions include yanked versions
func (manifest *Component) VersionListWithYanked(platform string) map[string]VersionItem {
if manifest == nil {
return nil
}
vs, ok := manifest.Platforms[platform]
if !ok {
vs, ok = manifest.Platforms[AnyPlatform]
if !ok {
return nil
}
}
return vs
}
// ErrLoadManifest is an empty object of LoadManifestError, useful for type check
var ErrLoadManifest = &LoadManifestError{}
// LoadManifestError is the error type used when loading manifest failes
type LoadManifestError struct {
manifest string // manifest name
err error // wrapped error
}
// Error implements the error interface
func (e *LoadManifestError) Error() string {
return fmt.Sprintf(
"error loading manifest %s: %s",
e.manifest, e.err,
)
}
// Unwrap implements the error interface
func (e *LoadManifestError) Unwrap() error { return e.err }
// Is implements the error interface
func (e *LoadManifestError) Is(target error) bool {
t, ok := target.(*LoadManifestError)
if !ok {
return false
}
return (e.manifest == t.manifest || t.manifest == "") &&
(errors.Is(e.err, t.err) || t.err == nil)
}