-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
50 lines (40 loc) · 943 Bytes
/
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
package main
import (
"bytes"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
FailFast bool `yaml:"fail_fast,omitempty"`
Route []Route `yaml:"route"`
}
type Route struct {
Name string `yaml:"name"`
IP string `yaml:"ip"`
Expect []RouteExpect `yaml:"expect"`
}
type RouteExpect struct {
When RouteExpectWhen `yaml:"when"`
}
type RouteExpectWhen struct {
Device string `yaml:"device,omitempty"`
Gateway string `yaml:"gateway,omitempty"`
Source string `yaml:"source,omitempty"`
}
func ReadConfig(file string) (Config, error) {
var config Config
buf, err := os.ReadFile(file)
if err != nil {
return config, fmt.Errorf("cannot read file")
}
decoder := yaml.NewDecoder(bytes.NewReader(buf))
decoder.KnownFields(true)
err = decoder.Decode(&config)
if err != nil {
return config, fmt.Errorf(
"could not parse config file=\"%s\" %s", file, err.Error(),
)
}
return config, nil
}