forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_network.go
68 lines (56 loc) · 1.26 KB
/
node_network.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
package parse
import (
"fmt"
"gopkg.in/yaml.v2"
)
// NetworkNode represents a Docker network.
type NetworkNode struct {
NodeType
root *RootNode
Name string
Driver string
DriverOpts map[string]string
}
// Root returns the root node.
func (n *NetworkNode) Root() *RootNode { return n.root }
//
// intermediate types for yaml decoding.
//
// network is an intermediate type used for decoding a networks in a format
// compatible with docker-compose.yml
type network struct {
Name string
Driver string
DriverOpts map[string]string `yaml:"driver_opts"`
}
// networkList is an intermediate type used for decoding a slice of networks
// in a format compatible with docker-compose.yml
type networkList struct {
networks []*network
}
func (n *networkList) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}
err := unmarshal(&slice)
if err != nil {
return err
}
for _, s := range slice {
nn := network{}
out, err := yaml.Marshal(s.Value)
if err != nil {
return err
}
err = yaml.Unmarshal(out, &nn)
if err != nil {
return err
}
if nn.Name == "" {
nn.Name = fmt.Sprintf("%v", s.Key)
}
if nn.Driver == "" {
nn.Driver = "bridge"
}
n.networks = append(n.networks, &nn)
}
return err
}