forked from dougbw/coredns_omada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
122 lines (98 loc) · 2.7 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package coredns_omada
import (
"strconv"
"github.com/coredns/caddy"
"github.com/go-playground/validator/v10"
)
type config struct {
Controller_url string `validate:"required,url"`
Site string `validate:"required"`
Username string `validate:"required"`
Password string `validate:"required"`
refresh_minutes int // update dns zones every x minutes
refresh_login_hours int // login and get a new session token every x hours
resolve_clients bool // resolve 'client' addresses
resolve_devices bool // resolve 'device' addresses
resolve_dhcp_reservations bool // resolve static 'dhcp reservations'
// update_client_names bool
// update_device_names bool
// update_dhcp_reservations bool
}
func parse(c *caddy.Controller) (config config, err error) {
// defaults
config.refresh_minutes = 1
config.refresh_login_hours = 24
config.resolve_clients = true
config.resolve_devices = true
config.resolve_dhcp_reservations = false
// not yet implemented...
// config.overwrite_client_names = false
// config.overwrite_device_names = false
// config.overwrite_dhcp_reservations = false
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "controller_url":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Controller_url = c.Val()
case "site":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Site = c.Val()
case "username":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Username = c.Val()
case "password":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Password = c.Val()
case "refresh_minutes":
if !c.NextArg() {
return config, c.ArgErr()
}
config.refresh_minutes, err = strconv.Atoi(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "refresh_login_hours":
if !c.NextArg() {
return config, c.ArgErr()
}
config.refresh_login_hours, err = strconv.Atoi(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "resolve_clients":
if !c.NextArg() {
return config, c.ArgErr()
}
config.resolve_clients, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "resolve_devices":
if !c.NextArg() {
return config, c.ArgErr()
}
config.resolve_devices, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
default:
return config, c.Errf("unknown property: %q", c.Val())
}
}
}
validate := validator.New()
if err := validate.Struct(config); err != nil {
log.Info("There is a Corefile configuration error:")
return config, err
}
return config, nil
}