-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (55 loc) · 1.49 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
package main
import (
"fmt"
"strings"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)
type ConfigPrefix struct {
Id int `koanf:"id"`
Prefix string `koanf:"prefix"`
ASN int `koanf:"asn"`
}
type ConfigSite struct {
Id int `koanf:"id"`
Name string `koanf:"name"`
Neighbor string `koanf:"neighbor"`
ASN int `konaf:"asn"`
}
var (
k = koanf.New(".")
parser = toml.Parser()
Config struct {
ASN uint32 `koanf:"asn"`
RouterID string `koanf:"router_id"`
ListenPort int32 `koanf:"listen_port"`
ListenAddr []string `koanf:"listen_addr"`
Prefixes []*ConfigPrefix `koanf:"prefixes"`
Sites []*ConfigSite `koanf:"sites"`
}
)
func loadConfig() error {
if err := k.Load(file.Provider(configPath), toml.Parser()); err != nil {
return err
}
if err := k.Unmarshal("", &Config); err != nil {
return err
}
return validateConfig()
}
func validateConfig() error {
for _, configPrefix := range Config.Prefixes {
prefix := configPrefix.Prefix
prefixSplit := strings.Split(prefix, "/")
if len(prefixSplit) != 2 {
return fmt.Errorf("Config(Prefix4): Prefix not valid: %v", prefix)
}
if err := validation.Validate(prefixSplit[0], is.IPv4); err != nil {
return fmt.Errorf("Config(prefix4): Prefix not valid %w, %v", err, prefix)
}
}
return nil
}