-
Notifications
You must be signed in to change notification settings - Fork 6
/
manifests.go
590 lines (508 loc) · 21.7 KB
/
manifests.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package main
import (
"os"
"errors"
"slices"
"strings"
"io"
"io/fs"
"net/url"
"net/http"
"path/filepath"
"gopkg.in/yaml.v3"
"rewinged/logging"
"rewinged/models"
)
func ingestManifestsWorker(autoInternalize bool, autoInternalizePath string, autoInternalizeSkipHosts []string) error {
for path := range jobs {
files, err := os.ReadDir(path)
if err != nil {
logging.Logger.Error().Err(err).Msg("ingestManifestsWorker error")
wg.Done()
continue
}
// temporary map collecting all files belonging to a particular package
var nonSingletonsMap = make(map[models.MultiFileManifest][]models.ManifestTypeAndPath)
for _, file := range files {
if !file.IsDir() {
if caseInsensitiveHasSuffix(file.Name(), ".yml") || caseInsensitiveHasSuffix(file.Name(), ".yaml") {
var basemanifest, err = parseFileAsBaseManifest(filepath.Join(path, file.Name()))
if err != nil {
logging.Logger.Error().Err(err).Str("file", filepath.Join(path, file.Name())).Msgf("cannot unmarshal YAML file as BaseManifest")
continue
}
// There could be other, non winget-manifest YAML files in the manifestPath as well. Skip them.
// All valid manifest files must have all basemanifest fields set as they are required by the schema
if basemanifest.PackageIdentifier != "" && basemanifest.PackageVersion != "" &&
basemanifest.ManifestType != "" && basemanifest.ManifestVersion != "" {
if basemanifest.ManifestType == "singleton" {
logging.Logger.Debug().Str("package", basemanifest.PackageIdentifier).Str("packageversion", basemanifest.PackageVersion).Msgf("found singleton manifest")
manifest, err := parseFileAsSingletonManifest(basemanifest.ManifestVersion, filepath.Join(path, file.Name()))
if err != nil {
logging.Logger.Error().Err(err).Str("package", basemanifest.PackageIdentifier).Str("packageversion", basemanifest.PackageVersion).Msg("could not parse singleton manifest")
} else {
// Singleton manifests can only contain version of a package each
var version = manifest.GetVersions()[0]
// Internalization logic
if (autoInternalize) {
var installers []models.API_InstallerInterface = version.GetInstallers()
internalizeInstallers(basemanifest.PackageIdentifier, basemanifest.PackageVersion, installers, autoInternalizePath, autoInternalizeSkipHosts)
// Recreate manifest object, but with overwritten values (InstallerUrl(s))
manifest, err = newAPIManifest(
basemanifest.ManifestVersion,
basemanifest.PackageIdentifier,
basemanifest.PackageVersion,
version.GetDefaultLocale(),
version.GetLocales(),
installers,
)
if err != nil {
logging.Logger.Error().Str("package", basemanifest.PackageIdentifier).Str("packageversion", basemanifest.PackageVersion).Msgf("error reconstructing package after overwrite")
}
version = manifest.GetVersions()[0]
}
// End internalization logic
models.Manifests.Set(manifest.GetPackageIdentifier(), basemanifest.PackageVersion, version)
}
} else {
typeAndPath := models.ManifestTypeAndPath{
ManifestType: basemanifest.ManifestType,
FilePath: filepath.Join(path, file.Name()),
}
nonSingletonsMap[basemanifest.ToMultiFileManifest()] = append(nonSingletonsMap[basemanifest.ToMultiFileManifest()], typeAndPath)
}
}
}
}
}
if len(nonSingletonsMap) > 0 {
for key, value := range nonSingletonsMap {
logging.Logger.Debug().Str("package", key.PackageIdentifier).Str("packageversion", key.PackageVersion).Msgf("found multi-file manifest")
var mergedManifest, err = parseMultiFileManifest(key, value...)
if err != nil {
logging.Logger.Error().Err(err).Str("package", key.PackageIdentifier).Str("packageversion", key.PackageVersion).Msgf("could not parse all manifest files for this package")
} else {
for _, version := range mergedManifest.GetVersions() {
// Internalization logic
if (autoInternalize) {
var installers []models.API_InstallerInterface = version.GetInstallers()
internalizeInstallers(key.PackageIdentifier, key.PackageVersion, installers, autoInternalizePath, autoInternalizeSkipHosts)
// Recreate manifest object, but with overwritten values (InstallerUrl(s))
overwrittenMergedManifest, err := newAPIManifest(
key.ManifestVersion,
key.PackageIdentifier,
key.PackageVersion,
version.GetDefaultLocale(),
version.GetLocales(),
installers,
)
if err != nil {
logging.Logger.Error().Str("package", key.PackageIdentifier).Str("packageversion", key.PackageVersion).Msgf("error reconstructing package after overwrite")
}
version = overwrittenMergedManifest.GetVersions()[0]
}
// End internalization logic
// Replace the existing PkgId + PkgVersion entry with this one
models.Manifests.Set(mergedManifest.GetPackageIdentifier(), version.GetPackageVersion(), version)
}
}
}
}
wg.Done()
}
return nil
}
func internalizeInstallers(
packageIdentifier string,
packageVersion string,
installers []models.API_InstallerInterface,
autoInternalizePath string,
autoInternalizeSkipHosts []string,
) {
for _, installer := range installers {
var originalInstallerURL string = installer.GetInstallerUrl()
u, err := url.Parse(originalInstallerURL)
if err != nil {
logging.Logger.Error().Err(err).Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("cannot parse InstallerUrl %s", originalInstallerURL)
continue
}
if slices.Contains(autoInternalizeSkipHosts, u.Hostname()) {
logging.Logger.Debug().Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("not internalizing %s", originalInstallerURL)
continue
}
var destFile string = filepath.Join(autoInternalizePath, strings.ToLower(installer.GetInstallerSha()))
// Why os.OpenFile instead of os.Create:
// https://stackoverflow.com/a/22483001
out, err := os.OpenFile(destFile, os.O_RDWR | os.O_CREATE | os.O_EXCL, 0666)
if err != nil {
if errors.Is(err, fs.ErrExist) {
logging.Logger.Debug().Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("file already exists, not redownloading %s", destFile)
} else {
logging.Logger.Error().Err(err).Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("cannot create file %s", destFile)
continue
}
} else {
defer out.Close()
// No error, we could open the file for writing and it does not exist yet - so download it
logging.Logger.Debug().Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("downloading installer")
resp, err := http.Get(originalInstallerURL)
if err != nil {
logging.Logger.Error().Err(err).Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("cannot download file %s", originalInstallerURL)
continue
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
logging.Logger.Error().Err(err).Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("cannot download file %s (http status %d)", originalInstallerURL, resp.StatusCode)
continue
}
n, err := io.Copy(out, resp.Body)
if err != nil {
logging.Logger.Error().Err(err).Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("cannot save to file %s", destFile)
continue
} else {
logging.Logger.Debug().Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("downloaded installer, %d bytes written", n)
}
}
logging.Logger.Debug().Str("package", packageIdentifier).Str("packageversion", packageVersion).Msgf("prepared internaliziation")
// Remember that this installer was internalized successfully (could be or already was downloaded)
// so we know we can rewrite its InstallerUrl later.
models.InternalizedInstallers[installer.GetInstallerSha()] = true
}
}
// Finds and parses all package manifest files in a directory
// recursively and returns them as a map of PackageIdentifier
// and PackageVersions
func getManifests (path string) {
files, err := os.ReadDir(path)
if err != nil {
logging.Logger.Error().Err(err)
}
// wg.Add() before goroutine, see staticcheck check SA2000 and also
// https://stackoverflow.com/questions/65213707/where-to-put-wg-add
wg.Add(1)
go func() {
jobs <- path
}()
for _, file := range files {
if file.IsDir() {
subdirPath := filepath.Join(path, file.Name())
logging.Logger.Trace().Msgf("searching directory %s", subdirPath)
getManifests(subdirPath)
}
}
}
func unmarshalVersionManifest (manifestVersion string, yamlData []byte) (models.Manifest_VersionManifestInterface, error) {
var version models.Manifest_VersionManifestInterface
switch manifestVersion {
case "1.1.0":
version = &models.Manifest_VersionManifest_1_1_0{}
case "1.2.0":
version = &models.Manifest_VersionManifest_1_2_0{}
case "1.4.0":
version = &models.Manifest_VersionManifest_1_4_0{}
case "1.5.0":
version = &models.Manifest_VersionManifest_1_5_0{}
case "1.6.0":
version = &models.Manifest_VersionManifest_1_6_0{}
default:
return nil, errors.New("unsupported VersionManifest version " + manifestVersion)
}
err := yaml.Unmarshal(yamlData, version)
if err != nil {
return nil, err
}
return version, nil
}
func unmarshalInstallerManifest (manifestVersion string, yamlData []byte) (models.Manifest_InstallerManifestInterface, error) {
var installer models.Manifest_InstallerManifestInterface
switch manifestVersion {
case "1.1.0":
installer = &models.Manifest_InstallerManifest_1_1_0{}
case "1.2.0":
installer = &models.Manifest_InstallerManifest_1_2_0{}
case "1.4.0":
installer = &models.Manifest_InstallerManifest_1_4_0{}
case "1.5.0":
installer = &models.Manifest_InstallerManifest_1_5_0{}
case "1.6.0":
installer = &models.Manifest_InstallerManifest_1_6_0{}
default:
return nil, errors.New("unsupported InstallerManifest version " + manifestVersion)
}
err := yaml.Unmarshal(yamlData, installer)
if err != nil {
return nil, err
}
return installer, nil
}
func unmarshalLocaleManifest (manifestVersion string, yamlData []byte) (models.Manifest_LocaleManifestInterface, error) {
var locale models.Manifest_LocaleManifestInterface
switch manifestVersion {
case "1.1.0":
locale = &models.Manifest_LocaleManifest_1_1_0{}
case "1.2.0":
locale = &models.Manifest_LocaleManifest_1_2_0{}
case "1.4.0":
locale = &models.Manifest_LocaleManifest_1_4_0{}
case "1.5.0":
locale = &models.Manifest_LocaleManifest_1_5_0{}
case "1.6.0":
locale = &models.Manifest_LocaleManifest_1_6_0{}
default:
return nil, errors.New("unsupported LocaleManifest version " + manifestVersion)
}
err := yaml.Unmarshal(yamlData, locale)
if err != nil {
return nil, err
}
return locale, nil
}
func unmarshalDefaultLocaleManifest (manifestVersion string, yamlData []byte) (models.Manifest_DefaultLocaleManifestInterface, error) {
var defaultlocale models.Manifest_DefaultLocaleManifestInterface
switch manifestVersion {
case "1.1.0":
defaultlocale = &models.Manifest_DefaultLocaleManifest_1_1_0{}
case "1.2.0":
defaultlocale = &models.Manifest_DefaultLocaleManifest_1_2_0{}
case "1.4.0":
defaultlocale = &models.Manifest_DefaultLocaleManifest_1_4_0{}
case "1.5.0":
defaultlocale = &models.Manifest_DefaultLocaleManifest_1_5_0{}
case "1.6.0":
defaultlocale = &models.Manifest_DefaultLocaleManifest_1_6_0{}
default:
return nil, errors.New("unsupported DefaultLocaleManifest version " + manifestVersion)
}
err := yaml.Unmarshal(yamlData, defaultlocale)
if err != nil {
return nil, err
}
return defaultlocale, nil
}
func parseMultiFileManifest (multifilemanifest models.MultiFileManifest, files ...models.ManifestTypeAndPath) (models.API_ManifestInterface, error) {
if len(files) <= 0 {
return nil, errors.New("you must provide at least one filename for reading values")
}
versions := []models.Manifest_VersionManifestInterface{}
installers := []models.Manifest_InstallerManifestInterface{}
locales := []models.Manifest_LocaleManifestInterface{}
var defaultlocale models.Manifest_DefaultLocaleManifestInterface
for _, file := range files {
yamlFile, err := os.ReadFile(file.FilePath)
if err != nil {
logging.Logger.Error().Str("file", file.FilePath).Err(err).Msg("cannot read file")
continue
}
switch file.ManifestType {
case "version":
var version models.Manifest_VersionManifestInterface
version, err = unmarshalVersionManifest(multifilemanifest.ManifestVersion, yamlFile)
if err != nil {
logging.Logger.Error().Str("file", file.FilePath).Err(err).Msg("cannot unmarshal manifest file")
continue
}
versions = append(versions, version)
case "installer":
var installer models.Manifest_InstallerManifestInterface
installer, err = unmarshalInstallerManifest(multifilemanifest.ManifestVersion, yamlFile)
if err != nil {
logging.Logger.Error().Str("file", file.FilePath).Err(err).Msg("cannot unmarshal manifest file")
continue
}
installers = append(installers, installer)
case "locale":
var locale models.Manifest_LocaleManifestInterface
locale, err = unmarshalLocaleManifest(multifilemanifest.ManifestVersion, yamlFile)
if err != nil {
logging.Logger.Error().Str("file", file.FilePath).Err(err).Msg("cannot unmarshal manifest file")
continue
}
locales = append(locales, locale)
case "defaultLocale":
defaultlocale, err = unmarshalDefaultLocaleManifest(multifilemanifest.ManifestVersion, yamlFile)
if err != nil {
logging.Logger.Error().Str("file", file.FilePath).Err(err).Msg("cannot unmarshal manifest file")
continue
}
default:
}
}
// It's possible there were no installer or locale manifests or parsing them failed
if len(installers) == 0 {
return nil, errors.New("no (valid) installer manifest")
}
if len(versions) == 0 {
return nil, errors.New("no (valid) locale manifest")
}
// This transforms the manifest data into the format the API will return.
// This logic should probably be moved out of this function, so that it returns
// the full unaltered data from the combined manifests - and restructuring to
// API-format will happen somewhere else
var apiLocales []models.API_LocaleInterface
for _, locale := range locales {
apiLocales = append(apiLocales, locale.ToApiLocale())
}
var apiInstallers []models.API_InstallerInterface
// In case there are more than 1 InstallerManifests per package (not sure if officially allowed)
// we get all the installers defined in all the InstallerManifest files.
for _, v := range installers {
apiInstallers = append(apiInstallers, v.ToApiInstallers()...)
}
manifest, err := newAPIManifest(
multifilemanifest.ManifestVersion,
multifilemanifest.PackageIdentifier,
versions[0].GetPackageVersion(),
defaultlocale.ToApiDefaultLocale(),
apiLocales,
apiInstallers,
)
return manifest, err
}
func newAPIManifest (
ManifestVersion string,
PackageIdentifier string,
pv string,
dl models.API_DefaultLocaleInterface,
l []models.API_LocaleInterface,
inst []models.API_InstallerInterface,
) (
models.API_ManifestInterface,
error,
) {
var apiReturnManifest models.API_ManifestInterface
var apiMvi models.API_ManifestVersionInterface
if ManifestVersion == "1.1.0" {
var apiLocales []models.API_Locale_1_1_0
for _, locale := range l {
apiLocales = append(apiLocales, locale.(models.API_Locale_1_1_0))
}
var apiInstallers []models.API_Installer_1_1_0
for _, intf := range inst {
apiInstallers = append(apiInstallers, *intf.(*models.API_Installer_1_1_0))
}
apiMvi = models.API_ManifestVersion_1_1_0{
PackageVersion: pv,
DefaultLocale: dl.(models.API_DefaultLocale_1_1_0),
Channel: "",
Locales: apiLocales,
Installers: apiInstallers,
}
apiReturnManifest = &models.API_Manifest_1_1_0 {
PackageIdentifier: PackageIdentifier,
Versions: []models.API_ManifestVersionInterface{ apiMvi },
}
} else if ManifestVersion == "1.2.0" || ManifestVersion == "1.4.0" {
// There is no API schema 1.2.0, so both v1.2.0 and v1.4.0
// packages are returned to clients as v1.4.0 API responses
var apiLocales []models.API_Locale_1_4_0
for _, locale := range l {
apiLocales = append(apiLocales, locale.(models.API_Locale_1_4_0))
}
var apiInstallers []models.API_Installer_1_4_0
for _, intf := range inst {
apiInstallers = append(apiInstallers, *intf.(*models.API_Installer_1_4_0))
}
apiMvi = models.API_ManifestVersion_1_4_0{
PackageVersion: pv,
DefaultLocale: dl.(models.API_DefaultLocale_1_4_0),
Channel: "",
Locales: apiLocales,
Installers: apiInstallers,
}
apiReturnManifest = &models.API_Manifest_1_4_0 {
PackageIdentifier: PackageIdentifier,
Versions: []models.API_ManifestVersionInterface{ apiMvi },
}
} else if ManifestVersion == "1.5.0" {
var apiLocales []models.API_Locale_1_5_0
for _, locale := range l {
apiLocales = append(apiLocales, locale.(models.API_Locale_1_5_0))
}
var apiInstallers []models.API_Installer_1_5_0
for _, intf := range inst {
apiInstallers = append(apiInstallers, *intf.(*models.API_Installer_1_5_0))
}
apiMvi = models.API_ManifestVersion_1_5_0{
PackageVersion: pv,
DefaultLocale: dl.(models.API_DefaultLocale_1_5_0),
Channel: "",
Locales: apiLocales,
Installers: apiInstallers,
}
apiReturnManifest = &models.API_Manifest_1_5_0 {
PackageIdentifier: PackageIdentifier,
Versions: []models.API_ManifestVersionInterface{ apiMvi },
}
} else if ManifestVersion == "1.6.0" {
var apiLocales []models.API_Locale_1_6_0
for _, locale := range l {
apiLocales = append(apiLocales, locale.(models.API_Locale_1_6_0))
}
var apiInstallers []models.API_Installer_1_6_0
for _, intf := range inst {
apiInstallers = append(apiInstallers, *intf.(*models.API_Installer_1_6_0))
}
apiMvi = models.API_ManifestVersion_1_6_0{
PackageVersion: pv,
DefaultLocale: dl.(models.API_DefaultLocale_1_6_0),
Channel: "",
Locales: apiLocales,
Installers: apiInstallers,
}
apiReturnManifest = &models.API_Manifest_1_6_0 {
PackageIdentifier: PackageIdentifier,
Versions: []models.API_ManifestVersionInterface{ apiMvi },
}
} else {
return nil, errors.New("Converting manifest v" + ManifestVersion + " data for API responses is not yet supported.")
}
return apiReturnManifest, nil
}
func parseFileAsBaseManifest (path string) (*models.BaseManifest, error) {
manifest := &models.BaseManifest{}
yamlFile, err := os.ReadFile(path)
if err != nil {
return manifest, err
}
err = yaml.Unmarshal(yamlFile, manifest)
return manifest, err
}
func parseFileAsSingletonManifest (manifestVersion string, path string) (models.API_ManifestInterface, error) {
var manifest models.API_ManifestInterface
yamlFile, err := os.ReadFile(path)
if err != nil {
return manifest, err
}
var singleton models.Manifest_SingletonManifestInterface
singleton, err = unmarshalSingletonManifest(manifestVersion, yamlFile)
if err == nil {
manifest = singleton.ToApiManifest()
}
return manifest, err
}
func unmarshalSingletonManifest (manifestVersion string, yamlData []byte) (models.Manifest_SingletonManifestInterface, error) {
var smanifest models.Manifest_SingletonManifestInterface
switch manifestVersion {
case "1.1.0":
smanifest = &models.Manifest_SingletonManifest_1_1_0{}
case "1.2.0":
smanifest = &models.Manifest_SingletonManifest_1_2_0{}
case "1.4.0":
smanifest = &models.Manifest_SingletonManifest_1_4_0{}
case "1.5.0":
smanifest = &models.Manifest_SingletonManifest_1_5_0{}
case "1.6.0":
smanifest = &models.Manifest_SingletonManifest_1_6_0{}
default:
return nil, errors.New("unsupported SingletonManifest version " + manifestVersion)
}
err := yaml.Unmarshal(yamlData, smanifest)
if err != nil {
return nil, err
}
return smanifest, nil
}
func caseInsensitiveHasSuffix(s, substr string) bool {
s, substr = strings.ToUpper(s), strings.ToUpper(substr)
return strings.HasSuffix(s, substr)
}