-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
metadata.go
77 lines (62 loc) · 2.08 KB
/
metadata.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
package registry
import (
"fmt"
"io"
"os"
"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v3"
)
type PolicySpec struct {
// Authors is a list of authors of the policy
Authors []string `yaml:",omitempty"`
// URL is the URL of the policy source code
URL string `yaml:",omitempty"`
// Documentation is the URL of the policy documentation
Documentation string `yaml:",omitempty"`
// Source is the URL of the policy source code
Source string `yaml:",omitempty"`
// Version is the policy version, it must be semantic versioning compliant without the leading v
Version string `yaml:",omitempty"`
// Vendor is the policy vendor
Vendor string `yaml:",omitempty"`
// Licenses is the policy license
Licenses []string `yaml:",omitempty"`
// I don't understand why if set, it creates a file locally named with the value of the title
// I'll need to investigate but I am disabling it for now.
// Title string `yaml:",omitempty"`
// Description is the policy description
Description string `yaml:",omitempty"`
}
// LoadPolicyFile loads an Updatecli compose file into a compose Spec
func LoadPolicyFile(filename string) (*PolicySpec, error) {
var policySpec PolicySpec
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("opening Updatecli policy file %q: %s", filename, err)
}
defer f.Close()
policyFileByte, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("reading Updatecli policy file %q: %s", filename, err)
}
if err = yaml.Unmarshal(policyFileByte, &policySpec); err != nil {
return nil, fmt.Errorf("parsing Updatecli policy file %q: %s", filename, err)
}
if err = policySpec.Sanitize(); err != nil {
return nil, fmt.Errorf("validating Updatecli policy file %q: %s", filename, err)
}
return &policySpec, nil
}
// Sanitize validates the policy spec and set default values accordingly
func (s *PolicySpec) Sanitize() error {
if s.Version == "" {
s.Version = "0.0.1"
}
v, err := semver.NewVersion(s.Version)
if err != nil {
return fmt.Errorf("invalid policy version %q: %s", s.Version, err)
}
// Trim leading v
s.Version = v.String()
return nil
}