-
Notifications
You must be signed in to change notification settings - Fork 0
/
appconfig.go
152 lines (133 loc) · 4.4 KB
/
appconfig.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"strings"
"github.com/wiggin77/cfg"
)
type configKey struct {
name string
def string
req bool
inc incRule
}
type incRule int
const (
// ALWAYS means the key is always added to API query string
ALWAYS incRule = iota
// NEVER means the key is never added to API query string
NEVER
// NOTEMPTY means the key is added to API query string only when a non-empty string
NOTEMPTY
// NOTFALSE means the key is added to API query string only when not "NO" or "OFF"
NOTFALSE
)
// Configuration keys
var (
keyProtocolVersion = configKey{name: "protocol_ver", def: "1.3", req: false, inc: NEVER}
keyURL = configKey{name: "url", def: "api.cp.easydns.com/dyn/generic.php", req: false, inc: NEVER}
keyUsername = configKey{name: "username", def: "", req: true, inc: NEVER}
keyToken = configKey{name: "token", def: "", req: true, inc: NEVER}
keyHostname = configKey{name: "hostname", def: "", req: true, inc: ALWAYS}
keyTld = configKey{name: "tld", def: "", req: false, inc: NOTEMPTY}
keyMyIP = configKey{name: "myip", def: "1.1.1.1", req: false, inc: ALWAYS}
keyMx = configKey{name: "mx", def: "", req: false, inc: NOTEMPTY}
keyBackMx = configKey{name: "backmx", def: "NO", req: false, inc: NOTFALSE}
keyWildcard = configKey{name: "wildcard", def: "OFF", req: false, inc: NOTFALSE}
keyInterval = configKey{name: "interval", def: "11 minutes", req: false, inc: NEVER}
keyLogFile = configKey{name: "log", def: "", req: false, inc: NEVER}
keySyslog = configKey{name: "syslog", def: "NO", req: false, inc: NEVER}
keyVerbose = configKey{name: "verbose", def: "NO", req: false, inc: NEVER}
keyProto = configKey{name: "proto", def: "https", req: false, inc: NEVER}
keysAll = []configKey{keyProtocolVersion, keyURL, keyUsername, keyToken, keyHostname, keyTld,
keyMyIP, keyMx, keyBackMx, keyWildcard, keyInterval}
)
// AppConfig provides convenience methods for fetching ShadowCrypt
// specific properties.
type AppConfig struct {
cfg.Config
verified bool // ensures factory method must be used
}
// NewAppConfig creates an instance of AppConfig and verifies the
// contents of the specified config file.
func NewAppConfig(file string) (*AppConfig, error) {
config := &AppConfig{verified: false}
// create file Source using file spec and append
// to Config
src, err := cfg.NewSrcFileFromFilespec(file)
if err != nil {
return config, err
}
config.AppendSource(src)
// Verify all the required properties exist.
err = config.verify()
if err == nil {
config.verified = true
}
return config, err
}
// NewAppConfigFromMap creates an instance of AppConfig containing
// a copy of the specified map elements.
func NewAppConfigFromMap(m map[string]string) (*AppConfig, error) {
config := &AppConfig{verified: false}
src := cfg.NewSrcMapFromMap(m)
config.AppendSource(src)
err := config.verify()
if err == nil {
config.verified = true
}
return config, err
}
// getKeyVal returns the value of the specified key.
func (config *AppConfig) getKeyVal(key configKey) string {
val, _ := config.String(key.name, key.def)
return val
}
// Verify all the required properties exist
func (config *AppConfig) verify() error {
// Check all required keys are present with non-empty values
for _, k := range keysAll {
if k.req {
val, err := config.String(k.name, "")
if err != nil || val == "" {
return fmt.Errorf("key %s missing", k.name)
}
}
}
// Append another Source containing the defaults for all keys.
m := make(map[string]string)
for _, k := range keysAll {
m[k.name] = k.def
}
config.AppendSource(cfg.NewSrcMapFromMap(m))
return nil
}
// getKeys returns a slice containing all config keys.
func (config *AppConfig) getKeys() []configKey {
return keysAll
}
func isFalse(s string) bool {
s = strings.ToUpper(s)
return s == "NO" || s == "OFF" || s == "FALSE"
}
func isTrue(s string) bool {
s = strings.ToUpper(s)
return s == "YES" || s == "ON" || s == "TRUE"
}
// Dump returns a string containing all application config properties.
func (config *AppConfig) Dump() string {
var sb strings.Builder
sep := ""
for _, k := range keysAll {
sb.WriteString(sep)
sb.WriteString(k.name)
sb.WriteString("=")
val, _ := config.String(k.name, "<missing>")
if val == "" {
sb.WriteString("\"\"")
} else {
sb.WriteString(val)
}
sep = ", "
}
return sb.String()
}