-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathconfig.go
More file actions
57 lines (50 loc) · 1.6 KB
/
config.go
File metadata and controls
57 lines (50 loc) · 1.6 KB
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
package main
import (
"encoding/json"
"flag"
"os"
)
type Config struct {
Server string
Zone string
NsupdateBinary string
RecordTTL int
Port int
}
type ConfigFlags struct {
Config
ConfigFile string
DoNotLoadConfig bool
LogLevel int
}
func (conf *Config) loadConfigFromFile(path string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&conf)
if err != nil {
panic(err)
}
}
func (flagsConf *ConfigFlags) setupFlags() {
flag.BoolVar(&flagsConf.DoNotLoadConfig, "noConfig", false, "Do not load the config file")
flag.StringVar(&flagsConf.ConfigFile, "c", "/etc/dyndns.json", "The configuration file")
flag.StringVar(&flagsConf.Server, "server", "localhost", "The address of the bind server")
flag.StringVar(&flagsConf.Zone, "zone", "", "Configuring a default zone will allow to send request with the hostname only as the domain")
flag.StringVar(&flagsConf.NsupdateBinary, "nsupdateBinary", "nsupdate", "Path to nsupdate program")
flag.IntVar(&flagsConf.RecordTTL, "recordTTL", 300, "RecordTTL")
flag.IntVar(&flagsConf.Port, "p", 8080, "Port")
flag.IntVar(&flagsConf.LogLevel, "log", 0, "Set the log level")
}
// LoadConfig loads config values from the config file and from the passed arguments.
// Gives command line arguments precedence.
func (flagsConf *ConfigFlags) LoadConfig() {
flagsConf.setupFlags()
flag.Parse()
if !flagsConf.DoNotLoadConfig {
flagsConf.loadConfigFromFile(flagsConf.ConfigFile)
flag.Parse() // Parse a second time to overwrite settings from the loaded file
}
}