-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
72 lines (63 loc) · 2.06 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
package main
import (
"os"
"strings"
"github.com/adrg/xdg"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
var configName = "tailscale-custom-domain-dns.toml"
func loadConfig() {
// Find the config file
configPath, err := xdg.SearchConfigFile(configName)
if err != nil {
log.Error().Msg("Could not find config file")
log.Error().Msgf(`No config file "%s" found in directories:`, configName)
log.Error().Msgf(" %s", xdg.ConfigHome)
for _, configDir := range xdg.ConfigDirs {
log.Error().Msgf(" %s", configDir)
}
log.Error().Msg("You can generate config with `tailscale-custom-domain-dns --generate-config`")
log.Fatal().Err(err).Msg("Exiting")
}
// Setup viper
viper.SetConfigName("tailscale-custom-domain-dns")
viper.SetConfigType("toml")
// Read configs from environment variables
viper.SetEnvPrefix("TSDNS")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", ""))
viper.AutomaticEnv()
// Set some default values
viper.SetDefault("log-level", "info")
viper.SetDefault("fetcher.interval", "1h")
viper.SetDefault("dns-server.port", 53)
viper.SetDefault("tailscale.hostname", "tailscale-custom-domain-dns")
viper.SetDefault("tailscale.ephemeral", false)
viper.SetDefault("aliases.subdomains", map[string]string{})
// Read the config
configFile, err := os.Open(configPath)
if err != nil {
log.Fatal().Err(err).Str("path", configPath).Msg("could not open conf file")
}
err = viper.ReadConfig(configFile)
if err != nil {
log.Fatal().Err(err).Str("path", configPath).Msg("could not parse conf file")
}
// Check for required config options
requiredConfigs := []string{
"domain",
"tailscale.organization-name",
"tailscale.auth-key",
"tailscale.oauth-client-id",
"tailscale.oauth-client-secret",
}
var missingConfigs []string
for _, requiredConfigName := range requiredConfigs {
if !viper.IsSet(requiredConfigName) {
missingConfigs = append(missingConfigs, requiredConfigName)
}
}
if len(missingConfigs) != 0 {
log.Fatal().Msgf("Missing required config values:\n - %s\n", strings.Join(missingConfigs, "\n - "))
}
}