/
config.go
72 lines (58 loc) · 1.41 KB
/
config.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
package config
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Config struct {
PolicyOptions *PolicyOptions `yaml:"policy_options"`
RoutingInstances []*RoutingInstance `yaml:"routing_instances"`
RoutingOptions *RoutingOptions `yaml:"routing_options"`
Protocols *Protocols `yaml:"protocols"`
}
func (c *Config) load() error {
if c.RoutingOptions == nil {
return fmt.Errorf("config is lacking routing_options")
}
if c.PolicyOptions != nil {
err := c.PolicyOptions.load()
if err != nil {
return fmt.Errorf("unable to load policy_options: %w", err)
}
}
err := c.RoutingOptions.load()
if err != nil {
return fmt.Errorf("error in routing_options: %w", err)
}
for _, ri := range c.RoutingInstances {
err := ri.load()
if ri != nil {
return err
}
}
if c.Protocols != nil {
localAS := c.RoutingOptions.AutonomousSystem
err := c.Protocols.load(localAS, c.PolicyOptions)
if err != nil {
return fmt.Errorf("Failed to load protocols: %w", err)
}
}
return nil
}
// GetConfig gets the configuration
func GetConfig(filePath string) (*Config, error) {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("unable to read file: %w", err)
}
c := &Config{}
err = yaml.Unmarshal(file, c)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal: %w", err)
}
err = c.load()
if err != nil {
return nil, err
}
return c, nil
}