-
Notifications
You must be signed in to change notification settings - Fork 23
/
charmmanifest.go
161 lines (136 loc) · 4.21 KB
/
charmmanifest.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
// Copyright 2024 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// CharmManifestArgs is an argument struct used to create a new
// CharmManifest.
type CharmManifestArgs struct {
Bases []CharmManifestBase
}
func newCharmManifest(args CharmManifestArgs) *charmManifest {
var bases []charmManifestBase
if args.Bases != nil {
bases = make([]charmManifestBase, len(args.Bases))
for i, b := range args.Bases {
bases[i] = charmManifestBase{
Name_: b.Name(),
Channel_: b.Channel(),
Architectures_: b.Architectures(),
}
}
}
return &charmManifest{
Version_: 1,
Bases_: bases,
}
}
// charmManifest represents the metadata of a charm.
type charmManifest struct {
Version_ int `yaml:"version"`
Bases_ []charmManifestBase `yaml:"bases"`
}
// Bases returns the list of the base the charm supports.
func (m *charmManifest) Bases() []CharmManifestBase {
bases := make([]CharmManifestBase, len(m.Bases_))
for i, b := range m.Bases_ {
bases[i] = b
}
return bases
}
func importCharmManifest(source map[string]interface{}) (*charmManifest, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "charmManifest version schema check failed")
}
importFunc, ok := charmManifestDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type charmManifestDeserializationFunc func(map[string]interface{}) (*charmManifest, error)
var charmManifestDeserializationFuncs = map[int]charmManifestDeserializationFunc{
1: importCharmManifestV1,
}
func importCharmManifestV1(source map[string]interface{}) (*charmManifest, error) {
return importCharmManifestVersion(source, 1)
}
func importCharmManifestVersion(source map[string]interface{}, importVersion int) (*charmManifest, error) {
fields := schema.Fields{
"bases": schema.List(schema.Any()),
}
defaults := schema.Defaults{
"bases": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "charmManifest v1 schema check failed")
}
valid := coerced.(map[string]interface{})
var bases []charmManifestBase
if valid["bases"] != nil {
bases = make([]charmManifestBase, len(valid["bases"].([]interface{})))
for k, v := range valid["bases"].([]interface{}) {
var err error
bases[k], err = importCharmManifestBase(v, importVersion)
if err != nil {
return nil, errors.Annotate(err, "charmManifest bases schema check failed")
}
}
}
return &charmManifest{
Version_: 1,
Bases_: bases,
}, nil
}
func importCharmManifestBase(source interface{}, importVersion int) (charmManifestBase, error) {
fields := schema.Fields{
"name": schema.String(),
"channel": schema.String(),
"architectures": schema.List(schema.String()),
}
defaults := schema.Defaults{
"name": schema.Omit,
"channel": schema.Omit,
"architectures": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmManifestBase{}, errors.Annotate(err, "charmManifestBase schema check failed")
}
valid := coerced.(map[string]interface{})
var architectures []string
if valid["architectures"] != nil {
architectures = make([]string, len(valid["architectures"].([]interface{})))
for i, a := range valid["architectures"].([]interface{}) {
architectures[i] = a.(string)
}
}
return charmManifestBase{
Name_: valid["name"].(string),
Channel_: valid["channel"].(string),
Architectures_: architectures,
}, nil
}
type charmManifestBase struct {
Name_ string `yaml:"name"`
Channel_ string `yaml:"channel"`
Architectures_ []string `yaml:"architectures"`
}
// Name returns the name of the base.
func (r charmManifestBase) Name() string {
return r.Name_
}
// Channel returns the channel of the base.
func (r charmManifestBase) Channel() string {
return r.Channel_
}
// Architectures returns the architectures of the base.
func (r charmManifestBase) Architectures() []string {
return r.Architectures_
}