-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
75 lines (60 loc) · 1.74 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
package main
import (
"os"
"github.com/BurntSushi/toml"
)
const (
// CFAPIEnvVar is the name of the environment variable that contains the
// Cloudflare API token.
CFAPIEnvVar = "CF_API_TOKEN"
// DNAPIEnvVar is the name of the environment variable that contains the
// Defined Networking API token.
DNAPIEnvVar = "DN_API_TOKEN"
)
type AppConfig struct {
RequiredTags []string `toml:"required_tags"`
RequiredSuffix string `toml:"required_suffix"`
TrimSuffix bool `toml:"trim_suffix"`
AppendSuffix string `toml:"append_suffix"`
PruneRecords bool `toml:"prune_records"`
// Cloudflare is the configuration for the Cloudflare API.
Cloudflare CloudflareConfig `toml:"cloudflare"`
// Defined is the configuration for the Defined Networking API.
DefinedNet DefinedConfig `toml:"definednet"`
}
type CloudflareConfig struct {
APIToken string `toml:"api_token"`
ZoneName string `toml:"zone_name"`
}
type DefinedConfig struct {
APIToken string `toml:"api_token"`
}
func LoadConfig(path string) (*AppConfig, error) {
// Load config from file
config, err := newConfigFromFile(path)
if err != nil {
return nil, err
}
// Optionally load secrets from the environment
config.readEnv()
// Default AppendSuffix to the zone name
if config.AppendSuffix == "" {
config.AppendSuffix = config.Cloudflare.ZoneName
}
return config, nil
}
func (c *AppConfig) readEnv() {
if cfToken := os.Getenv(CFAPIEnvVar); cfToken != "" {
c.Cloudflare.APIToken = cfToken
}
if dnToken := os.Getenv(DNAPIEnvVar); dnToken != "" {
c.DefinedNet.APIToken = dnToken
}
}
func newConfigFromFile(path string) (*AppConfig, error) {
var config AppConfig
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, err
}
return &config, nil
}