forked from harness/gitness
/
node_volume.go
69 lines (57 loc) · 1.26 KB
/
node_volume.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
package parse
import (
"fmt"
"gopkg.in/yaml.v2"
)
// VolumeNode represents a Docker volume.
type VolumeNode struct {
NodeType
root *RootNode
Name string
Driver string
DriverOpts map[string]string
External bool
}
// Root returns the root node.
func (n *VolumeNode) Root() *RootNode { return n.root }
//
// intermediate types for yaml decoding.
//
// volume is an intermediate type used for decoding a volumes in a format
// compatible with docker-compose.yml
type volume struct {
Name string
Driver string
DriverOpts map[string]string `yaml:"driver_opts"`
}
// volumeList is an intermediate type used for decoding a slice of volumes
// in a format compatible with docker-compose.yml
type volumeList struct {
volumes []*volume
}
func (v *volumeList) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}
err := unmarshal(&slice)
if err != nil {
return err
}
for _, s := range slice {
vv := volume{}
out, err := yaml.Marshal(s.Value)
if err != nil {
return err
}
err = yaml.Unmarshal(out, &vv)
if err != nil {
return err
}
if vv.Name == "" {
vv.Name = fmt.Sprintf("%v", s.Key)
}
if vv.Driver == "" {
vv.Driver = "local"
}
v.volumes = append(v.volumes, &vv)
}
return err
}