/
getValidYConfig.go
82 lines (67 loc) · 1.94 KB
/
getValidYConfig.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
package pkg
import (
"context"
"errors"
"strings"
"golang.org/x/mod/semver"
)
type dlInfo struct {
Name string
Version string
Source string
URL string
URLSHA string
osArch string
}
func (k Kindly) getValidYConfig(ctx context.Context, n string, f bool, u bool) (dlInfo, KindlyStruct, error) {
var err error
var yc KindlyStruct
// Pull out package version if provided
nVer := strings.SplitN(n, "@", 2)
dl := dlInfo{nVer[0], "", "", "", "", ""}
if len(nVer) > 1 {
dl.Version = semver.Canonical(nVer[1])
if !semver.IsValid(dl.Version) {
return dl, yc, errors.New("Invalid package version: " + n)
}
}
if f {
dl.Source = dl.Name
// Read package yaml spec and initialize KindlyStruct struct
if yc, err = getYamlFile(dl.Source); err != nil {
return dl, yc, err
}
} else {
sourceURL := k.cfg.Source + dl.Name + ".yaml"
if u {
sourceURL = dl.Name
}
dl.Source = sourceURL
// Download package yaml spec and initialize KindlyStruct struct
if yc, err = getYamlURL(ctx, sourceURL); err != nil {
return dl, yc, err
}
}
// Check if package is available
if !(len(yc.Spec.Name) > 0) {
return dl, yc, errors.New("Unavailable Package: " + dl.Name)
}
dl.Name = yc.Spec.Name
// Check if requested version is higher value than the available version in the package
if len(dl.Version) > 0 {
if semver.Compare(dl.Version, yc.Spec.Version) == 1 {
return dl, yc, errors.New("Version requested: " + n + "\tLatest version: " + dl.Name + "@" + yc.Spec.Version)
}
}
// If version was not provided in the argument, set it to version in spec file
if !(len(dl.Version) > 0) {
dl.Version = yc.Spec.Version
}
// processFile Downloads file from url, checks SHA value, and saves it to tmpDir
dl.osArch = k.cfg.OS + "_" + k.cfg.Arch
// Check if OS architecture is available
if _, ok := yc.Spec.Assets[dl.osArch]; !ok {
return dl, yc, errors.New("Unavailable OS Architecture: " + dl.osArch)
}
return dl, yc, nil
}