-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
75 lines (60 loc) · 1.92 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 (
"log"
"os"
toml "github.com/pelletier/go-toml"
)
type koniConfig struct {
debug bool
listenHTTP string
listenHTTPS string
url string
certsDir string
email string
provider string
imapServer string
popServer string
smtpServer string
}
func loadConfigFile(configFile string) koniConfig {
if _, err := os.Stat(configFile); err != nil {
log.Fatalf("Failed to open config file: %s", err)
}
tomlConfig, err := toml.LoadFile(configFile)
if err != nil {
log.Fatalf("Config file %s is malformed: %s", configFile, err)
}
return koniConfig{
debug: getBoolConfigValueDefault(tomlConfig, "debug", defaultDebug),
listenHTTP: getConfigValueDefault(tomlConfig, "listen_http", defaultListenHTTP),
listenHTTPS: getConfigValueDefault(tomlConfig, "listen_https", defaultListenHTTPS),
url: getConfigValueDefault(tomlConfig, "letsencrypt.url", defaultURL),
certsDir: getConfigValueDefault(tomlConfig, "letsencrypt.certs_dir", defaultCertsDir),
email: getConfigValue(tomlConfig, "letsencrypt.email"),
provider: getConfigValue(tomlConfig, "mail.provider_id"),
imapServer: getConfigValue(tomlConfig, "mail.imap_server"),
popServer: getConfigValue(tomlConfig, "mail.pop3_server"),
smtpServer: getConfigValue(tomlConfig, "mail.smtp_server"),
}
}
func getConfigValueDefault(config *toml.Tree, key string, defaultVal string) string {
val := config.Get(key)
if val == nil {
return defaultVal
}
return val.(string)
}
func getConfigValue(config *toml.Tree, key string) string {
val := config.Get(key)
if val == nil {
log.Fatalf("Invalid configuration file: Mandatory setting '%s' is missing", key)
}
return val.(string)
}
func getBoolConfigValueDefault(config *toml.Tree, key string, defaultVal bool) bool {
defaultStringValue := "no"
if defaultVal {
defaultStringValue = "yes"
}
return getConfigValueDefault(config, key, defaultStringValue) == "yes"
}