-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefinition.go
87 lines (76 loc) · 2.1 KB
/
definition.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
package machina
import "github.com/gentlemanautomaton/machina/summary"
// Definition holds the definition of a machine tag.
type Definition struct {
Vars Vars `json:"vars,omitempty"`
Privileges Privileges `json:"privileges,omitempty"`
Attributes Attributes `json:"attrs,omitempty"`
Volumes []Volume `json:"volumes,omitempty"`
Connections []Connection `json:"connections,omitempty"`
Devices []Device `json:"devices,omitempty"`
}
// Config adds the attributes configuration to the summary.
func (d *Definition) Config(info MachineInfo, out summary.Interface) {
if len(d.Vars) > 0 {
out.Add("Vars:")
out.Descend()
for key, value := range d.Vars {
out.Add("%s: %s", key, value)
}
out.Ascend()
}
d.Privileges.Config(info, d.Vars, out)
d.Attributes.Config(info, d.Vars, out)
if len(d.Volumes) > 0 {
out.Add("Volumes:")
out.Descend()
for i := range d.Volumes {
d.Volumes[i].Config(out)
}
out.Ascend()
}
if len(d.Connections) > 0 {
out.Add("Connections:")
out.Descend()
for i := range d.Connections {
d.Connections[i].Config(out)
}
out.Ascend()
}
if len(d.Devices) > 0 {
out.Add("Devices:")
out.Descend()
for i := range d.Devices {
d.Devices[i].Config(out)
}
out.Ascend()
}
}
// MergeDefinitions merges a set of definitions in order. If more than one
// volume exists with the same name, only the first is included.
func MergeDefinitions(defs ...Definition) Definition {
var (
vars []Vars
privs []Privileges
attrs []Attributes
vols []Volume
conns []Connection
devs []Device
)
for i := range defs {
vars = append(vars, defs[i].Vars)
privs = append(privs, defs[i].Privileges)
attrs = append(attrs, defs[i].Attributes)
vols = append(vols, defs[i].Volumes...)
conns = append(conns, defs[i].Connections...)
devs = append(devs, defs[i].Devices...)
}
return Definition{
Vars: MergeVars(vars...),
Privileges: MergePrivileges(privs...),
Attributes: MergeAttributes(attrs...),
Volumes: MergeVolumes(vols...),
Connections: MergeConnections(conns...),
Devices: MergeDevices(devs...),
}
}