-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
60 lines (50 loc) · 994 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
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/json"
"log"
"net"
"os"
)
type Config struct {
Interface string `json:"interface"`
net *net.IPNet `json:"-"`
LeaseTime uint32 `json:"lease_time"`
}
func (c *Config) Init() {
itf, err := net.InterfaceByName(c.Interface)
if err != nil {
log.Fatalf("Error Getting Interface: %s\n", err.Error())
}
addrs, err := itf.Addrs()
if err != nil {
log.Fatalln(err.Error())
}
c.net = addrs[0].(*net.IPNet)
}
func LoadConfig(path string) Config {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var config Config
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
return config
}
func SaveConfig(path string, config Config) {
file, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", "\t")
err = encoder.Encode(config)
if err != nil {
log.Fatal(err)
}
}