-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (38 loc) · 1005 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
package main
import (
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Config struct {
APIKey string `mapstructure:"api_key"`
Records []Record `mapstructure:"dns_records"`
CheckInterval int `mapstructure:"check_interval"`
}
type Record struct {
Domain string `mapstructure:"domain"`
ZoneID string `mapstructure:"zone_id"`
Proxied bool `mapstructure:"proxied"`
IPs []string `mapstructure:"ips"`
}
func parseConfig() (Config, error) {
var configPath string
pflag.StringVarP(&configPath, "config", "c", "", "Path to config file")
pflag.Parse()
if configPath != "" {
viper.SetConfigFile(configPath)
} else {
viper.SetConfigName("config")
viper.AddConfigPath(".")
}
var cfg Config
err := viper.ReadInConfig()
if err != nil {
return cfg, fmt.Errorf("Error reading config: %v", err)
}
err = viper.Unmarshal(&cfg)
if err != nil {
return cfg, fmt.Errorf("Unable to decode config into struct, %v", err)
}
return cfg, nil
}