This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
88 lines (72 loc) · 2.03 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
// RunMode specifies the mode in which the program is run as
type RunMode int
// String method implements the stringer interface
func (r RunMode) String() string {
if r == SERVER {
return "server"
}
return "client"
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for this enum type.
func (r *RunMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
x := ""
if err := unmarshal(&x); err != nil {
return err
}
fmt.Println("Debug::YAMLUnmarshal: runMode : ", x)
switch strings.ToLower(x) {
case "server":
*r = SERVER
case "client":
*r = CLIENT
default:
return errors.New("invalid runMode passed: expects either 'client' or 'server'")
}
return nil
}
// All the available RunModes
const (
SERVER RunMode = iota
CLIENT
)
// Config struct is used to store the values parsed from the config file
type Config struct {
Mode RunMode `yaml:"mode"`
Address string `yaml:"address"`
MTU int `yaml:"mtu"`
InterfaceName string `yaml:"interfaceName"`
DHCP string `yaml:"dhcp"`
DNS []string `yaml:"dns"`
Secret string `yaml:"secret"`
PostInitScript map[string]string `yaml:"postServerInit"`
PostConnectScript map[string]string `yaml:"postConnect"`
PostDisconnectScript map[string]string `yaml:"postDisconnect"`
}
// NewConfigFromFile is used to read configuration data from provided filename and return a Config struct
// after parsing the contents
func NewConfigFromFile(filename string) (*Config, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
config := &Config{}
err = yaml.Unmarshal(contents, config)
if err != nil {
return nil, err
}
if config.Address == "" {
return nil, fmt.Errorf("Config parse error : no address specified")
}
if config.MTU <= 0 {
config.MTU = 1500
}
return config, nil
}