/
config.go
118 lines (106 loc) · 3.81 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
package sf
import (
"io/ioutil"
"os"
"strings"
"github.com/cloudflare/cloudflare-go"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var config Configuration
// Configuration is a struct to store the script's configuration
type Configuration struct {
RootDomain string `yaml:"rootDomain,omitempty"`
IPProviders []string `yaml:"ipProviders"`
Notifications struct {
Slack struct {
WebhookURL string `yaml:"webhookURL"`
Username string `yaml:"username"`
Channel string `yaml:"channel"`
IconURL string `yaml:"iconURL"`
} `yaml:"slack"`
} `yaml:"notifications"`
Traefik struct {
URL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
IgnoredRules []string `yaml:"ignoredRules"`
} `yaml:"traefik"`
AdditionalRecords []cloudflare.DNSRecord `yaml:"additionalRecords"`
Cloudflare struct {
Email string `yaml:"email"`
APIKey string `yaml:"apiKey"`
DeleteGrace int `yaml:"deleteGrace"`
ZoneName string `yaml:"zoneName"`
Defaults struct {
Type string `yaml:"type"`
Proxied bool `yaml:"proxied"`
TTL int `yaml:"ttl"`
} `yaml:"defaults"`
} `yaml:"cloudflare"`
}
// GetConfig creates a global var holding the configuration
func GetConfig(configFilePath string) Configuration {
log.Debugf("Loading config file %s", configFilePath)
configFile, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Fatalf("Unable to load config file %s from disk: %s", configFilePath, err)
}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
log.Fatalf("Unable to read config file: %s", err)
}
// Check if env vars are set
if os.Getenv("SLACK_WEBHOOK") != "" {
config.Notifications.Slack.WebhookURL = os.Getenv("SLACK_WEBHOOK")
}
if os.Getenv("TRAEFIK_PASSWORD") != "" {
config.Traefik.Password = os.Getenv("TRAEFIK_PASSWORD")
}
if os.Getenv("CLOUDFLARE_APIKEY") != "" {
config.Cloudflare.APIKey = os.Getenv("CLOUDFLARE_APIKEY")
}
// Backwards compatibility
if config.RootDomain != "" {
config.Cloudflare.ZoneName = config.RootDomain
log.Warn("rootDomain is deprecated and will be removed in a future release, use cloudflare.zoneName instead")
}
// Set default values
if config.Cloudflare.Defaults.Type == "" {
config.Cloudflare.Defaults.Type = "CNAME"
log.Debug("Cloudflare default type is empty, defaulting to ", config.Cloudflare.Defaults.Type)
}
if config.Cloudflare.Defaults.TTL == 0 || config.Cloudflare.Defaults.Proxied {
config.Cloudflare.Defaults.TTL = 1
log.Debug("Cloudflare default TTL is empty, defaulting to ", config.Cloudflare.Defaults.TTL)
}
if config.IPProviders == nil {
config.IPProviders = append(config.IPProviders, "https://ifconfig.me/ip", "https://ipecho.net/plain", "https://myip.is/ip/")
log.Debug("IP providers is empty, defaulting to ", strings.Join(config.IPProviders, ", "))
}
if config.Notifications.Slack.Username == "" {
config.Notifications.Slack.Username = "SyncFlaer"
log.Debug("Slack username is empty, defaulting to ", config.Notifications.Slack.Username)
}
if config.Notifications.Slack.IconURL == "" {
config.Notifications.Slack.IconURL = "https://www.cloudflare.com/img/cf-facebook-card.png"
log.Debug("Slack icon URL is empty, defaulting to ", config.Notifications.Slack.IconURL)
}
// Validate config
if config.Traefik.URL == "" {
log.Fatal("Traefik URL cannot be empty")
}
if config.Cloudflare.Email == "" {
log.Fatal("Cloudflare email cannot be empty")
}
if config.Cloudflare.APIKey == "" {
log.Fatal("Cloudflare api key cannot be empty")
}
if config.Cloudflare.ZoneName == "" {
log.Fatal("Cloudflare zone name cannot be empty")
}
if config.Cloudflare.Defaults.Type != "A" && config.Cloudflare.Defaults.Type != "CNAME" {
log.Fatalf("Supported Cloudflare default types are A or CNAME")
}
return config
}