forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails_v2.go
350 lines (321 loc) · 9.71 KB
/
details_v2.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package store
import (
"fmt"
"strconv"
"time"
"github.com/snapcore/snapd/jsonutil/safejson"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/strutil"
)
// storeSnap holds the information sent as JSON by the store for a snap.
type storeSnap struct {
Architectures []string `json:"architectures"`
Base string `json:"base"`
Confinement string `json:"confinement"`
Contact string `json:"contact"`
CreatedAt string `json:"created-at"` // revision timestamp
Description safejson.Paragraph `json:"description"`
Download storeSnapDownload `json:"download"`
Epoch snap.Epoch `json:"epoch"`
License string `json:"license"`
Name string `json:"name"`
Prices map[string]string `json:"prices"` // currency->price, free: {"USD": "0"}
Private bool `json:"private"`
Publisher snap.StoreAccount `json:"publisher"`
Revision int `json:"revision"` // store revisions are ints starting at 1
SnapID string `json:"snap-id"`
SnapYAML string `json:"snap-yaml"` // optional
Summary safejson.String `json:"summary"`
Title safejson.String `json:"title"`
Type snap.Type `json:"type"`
Version string `json:"version"`
Website string `json:"website"`
StoreURL string `json:"store-url"`
// TODO: not yet defined: channel map
// media
Media []storeSnapMedia `json:"media"`
CommonIDs []string `json:"common-ids"`
}
type storeSnapDownload struct {
Sha3_384 string `json:"sha3-384"`
Size int64 `json:"size"`
URL string `json:"url"`
Deltas []storeSnapDelta `json:"deltas"`
}
type storeSnapDelta struct {
Format string `json:"format"`
Sha3_384 string `json:"sha3-384"`
Size int64 `json:"size"`
Source int `json:"source"`
Target int `json:"target"`
URL string `json:"url"`
}
type storeSnapMedia struct {
Type string `json:"type"` // icon/screenshot
URL string `json:"url"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
// storeInfoChannel is the channel description included in info results
type storeInfoChannel struct {
Architecture string `json:"architecture"`
Name string `json:"name"`
Risk string `json:"risk"`
Track string `json:"track"`
ReleasedAt time.Time `json:"released-at"`
}
// storeInfoChannelSnap is the snap-in-a-channel of which the channel map is made
type storeInfoChannelSnap struct {
storeSnap
Channel storeInfoChannel `json:"channel"`
}
// storeInfo is the result of v2/info calls
type storeInfo struct {
ChannelMap []*storeInfoChannelSnap `json:"channel-map"`
Snap storeSnap `json:"snap"`
Name string `json:"name"`
SnapID string `json:"snap-id"`
}
func infoFromStoreInfo(si *storeInfo) (*snap.Info, error) {
if len(si.ChannelMap) == 0 {
// if a snap has no released revisions, it _could_ be returned
// (currently no, but spec is purposely ambiguous)
// we treat it as a 'not found' for now at least
return nil, ErrSnapNotFound
}
thisOne := si.ChannelMap[0]
thisSnap := thisOne.storeSnap // copy it as we're about to modify it
// here we assume that the ChannelSnapInfo can be populated with data
// that's in the channel map and not the outer snap. This is a
// reasonable assumption today, but copyNonZeroFrom can easily be
// changed to copy to a list if needed.
copyNonZeroFrom(&si.Snap, &thisSnap)
info, err := infoFromStoreSnap(&thisSnap)
if err != nil {
return nil, err
}
info.Channel = thisOne.Channel.Name
info.Channels = make(map[string]*snap.ChannelSnapInfo, len(si.ChannelMap))
seen := make(map[string]bool, len(si.ChannelMap))
for _, s := range si.ChannelMap {
ch := s.Channel
chName := ch.Track + "/" + ch.Risk
info.Channels[chName] = &snap.ChannelSnapInfo{
Revision: snap.R(s.Revision),
Confinement: snap.ConfinementType(s.Confinement),
Version: s.Version,
Channel: chName,
Epoch: s.Epoch,
Size: s.Download.Size,
ReleasedAt: ch.ReleasedAt.UTC(),
}
if !seen[ch.Track] {
seen[ch.Track] = true
info.Tracks = append(info.Tracks, ch.Track)
}
}
return info, nil
}
func minimalFromStoreInfo(si *storeInfo) (naming.SnapRef, *channel.Channel, error) {
if len(si.ChannelMap) == 0 {
// if a snap has no released revisions, it _could_ be returned
// (currently no, but spec is purposely ambiguous)
// we treat it as a 'not found' for now at least
return nil, nil, ErrSnapNotFound
}
snapRef := naming.NewSnapRef(si.Name, si.SnapID)
first := si.ChannelMap[0].Channel
ch := channel.Channel{
Architecture: first.Architecture,
Name: first.Name,
Track: first.Track,
Risk: first.Risk,
}
ch = ch.Clean()
return snapRef, &ch, nil
}
// copy non-zero fields from src to dst
func copyNonZeroFrom(src, dst *storeSnap) {
if len(src.Architectures) > 0 {
dst.Architectures = src.Architectures
}
if src.Base != "" {
dst.Base = src.Base
}
if src.Confinement != "" {
dst.Confinement = src.Confinement
}
if src.Contact != "" {
dst.Contact = src.Contact
}
if src.CreatedAt != "" {
dst.CreatedAt = src.CreatedAt
}
if src.Description.Clean() != "" {
dst.Description = src.Description
}
if src.Download.URL != "" {
dst.Download = src.Download
} else if src.Download.Size != 0 {
// search v2 results do not contain download url, only size
dst.Download.Size = src.Download.Size
}
if src.Epoch.String() != "0" {
dst.Epoch = src.Epoch
}
if src.License != "" {
dst.License = src.License
}
if src.Name != "" {
dst.Name = src.Name
}
if len(src.Prices) > 0 {
dst.Prices = src.Prices
}
if src.Private {
dst.Private = src.Private
}
if src.Publisher.ID != "" {
dst.Publisher = src.Publisher
}
if src.Revision > 0 {
dst.Revision = src.Revision
}
if src.SnapID != "" {
dst.SnapID = src.SnapID
}
if src.SnapYAML != "" {
dst.SnapYAML = src.SnapYAML
}
if src.StoreURL != "" {
dst.StoreURL = src.StoreURL
}
if src.Summary.Clean() != "" {
dst.Summary = src.Summary
}
if src.Title.Clean() != "" {
dst.Title = src.Title
}
if src.Type != "" {
dst.Type = src.Type
}
if src.Version != "" {
dst.Version = src.Version
}
if len(src.Media) > 0 {
dst.Media = src.Media
}
if len(src.CommonIDs) > 0 {
dst.CommonIDs = src.CommonIDs
}
if len(src.Website) > 0 {
dst.Website = src.Website
}
}
func infoFromStoreSnap(d *storeSnap) (*snap.Info, error) {
info := &snap.Info{}
info.RealName = d.Name
info.Revision = snap.R(d.Revision)
info.SnapID = d.SnapID
// https://forum.snapcraft.io/t/title-length-in-snapcraft-yaml-snap-yaml/8625/10
info.EditedTitle = strutil.ElliptRight(d.Title.Clean(), 40)
info.EditedSummary = d.Summary.Clean()
info.EditedDescription = d.Description.Clean()
info.Private = d.Private
info.EditedContact = d.Contact
info.Architectures = d.Architectures
info.SnapType = d.Type
info.Version = d.Version
info.Epoch = d.Epoch
info.Confinement = snap.ConfinementType(d.Confinement)
info.Base = d.Base
info.License = d.License
info.Publisher = d.Publisher
info.DownloadURL = d.Download.URL
info.Size = d.Download.Size
info.Sha3_384 = d.Download.Sha3_384
if len(d.Download.Deltas) > 0 {
deltas := make([]snap.DeltaInfo, len(d.Download.Deltas))
for i, d := range d.Download.Deltas {
deltas[i] = snap.DeltaInfo{
FromRevision: d.Source,
ToRevision: d.Target,
Format: d.Format,
DownloadURL: d.URL,
Size: d.Size,
Sha3_384: d.Sha3_384,
}
}
info.Deltas = deltas
}
info.CommonIDs = d.CommonIDs
info.Website = d.Website
info.StoreURL = d.StoreURL
// fill in the plug/slot data
if rawYamlInfo, err := snap.InfoFromSnapYaml([]byte(d.SnapYAML)); err == nil {
if info.Plugs == nil {
info.Plugs = make(map[string]*snap.PlugInfo)
}
for k, v := range rawYamlInfo.Plugs {
info.Plugs[k] = v
info.Plugs[k].Snap = info
}
if info.Slots == nil {
info.Slots = make(map[string]*snap.SlotInfo)
}
for k, v := range rawYamlInfo.Slots {
info.Slots[k] = v
info.Slots[k].Snap = info
}
for _, s := range rawYamlInfo.Assumes {
info.Assumes = append(info.Assumes, s)
}
}
// convert prices
if len(d.Prices) > 0 {
prices := make(map[string]float64, len(d.Prices))
for currency, priceStr := range d.Prices {
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
return nil, fmt.Errorf("cannot parse snap price: %v", err)
}
prices[currency] = price
}
info.Paid = true
info.Prices = prices
}
// media
addMedia(info, d.Media)
return info, nil
}
func addMedia(info *snap.Info, media []storeSnapMedia) {
if len(media) == 0 {
return
}
info.Media = make(snap.MediaInfos, len(media))
for i, mediaObj := range media {
info.Media[i].Type = mediaObj.Type
info.Media[i].URL = mediaObj.URL
info.Media[i].Width = mediaObj.Width
info.Media[i].Height = mediaObj.Height
}
}