-
Notifications
You must be signed in to change notification settings - Fork 59
/
manifest.go
127 lines (114 loc) · 3.07 KB
/
manifest.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
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"io"
"io/ioutil"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/utils/v3/arch"
"gopkg.in/yaml.v2"
)
// Manifest represents the recording of the building of the charm or bundle.
// The manifest file should represent the metadata.yaml, but a lot more
// information.
type Manifest struct {
Bases []Base `yaml:"bases"`
}
// Validate checks the manifest to ensure there are no empty names, nor channels,
// and that architectures are supported.
func (m *Manifest) Validate() error {
for _, b := range m.Bases {
if err := b.Validate(); err != nil {
return errors.Annotate(err, "validating manifest")
}
}
return nil
}
func (m *Manifest) UnmarshalYAML(f func(interface{}) error) error {
raw := make(map[interface{}]interface{})
err := f(&raw)
if err != nil {
return err
}
v, err := schema.List(baseSchema).Coerce(raw["bases"], nil)
if err != nil {
return errors.Annotatef(err, "coerce")
}
newV, ok := v.([]interface{})
if !ok {
return errors.Annotatef(err, "converting")
}
bases, err := parseBases(newV)
if err != nil {
return err
}
*m = Manifest{Bases: bases}
return nil
}
func parseBases(input interface{}) ([]Base, error) {
var err error
if input == nil {
return nil, nil
}
var res []Base
for _, v := range input.([]interface{}) {
var base Base
baseMap := v.(map[string]interface{})
if value, ok := baseMap["name"]; ok {
base.Name = value.(string)
}
if value, ok := baseMap["channel"]; ok {
base.Channel, err = ParseChannelNormalize(value.(string))
if err != nil {
return nil, errors.Annotatef(err, "parsing channel %q", value.(string))
}
}
base.Architectures = parseArchitectureList(baseMap["architectures"])
err = base.Validate()
if err != nil {
return nil, errors.Trace(err)
}
res = append(res, base)
}
return res, nil
}
// ReadManifest reads in a Manifest from a charm's manifest.yaml. Some of
// validation is done when unmarshalling the manifest, including
// verification that the base.Name is a supported operating system. Full
// validation done by calling Validate().
func ReadManifest(r io.Reader) (*Manifest, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var manifest *Manifest
if err := yaml.Unmarshal(data, &manifest); err != nil {
return nil, errors.Annotatef(err, "manifest")
}
if manifest == nil {
return nil, errors.Annotatef(err, "invalid base in manifest")
}
return manifest, nil
}
var baseSchema = schema.FieldMap(
schema.Fields{
"name": schema.String(),
"channel": schema.String(),
"architectures": schema.List(schema.String()),
}, schema.Defaults{
"name": schema.Omit,
"channel": schema.Omit,
"architectures": schema.Omit,
})
func parseArchitectureList(list interface{}) []string {
if list == nil {
return nil
}
slice := list.([]interface{})
result := make([]string, 0, len(slice))
for _, elem := range slice {
result = append(result, arch.NormaliseArch(elem.(string)))
}
return result
}