-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
150 lines (124 loc) · 3.88 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package backend
import (
"strings"
"github.com/spf13/viper"
)
// ConfigInit instantiates and validates the configuration options
// optionally it can print out a configuration summary
func ConfigInit(cfgFile string, printConfig bool) {
// init viper
initViper(cfgFile)
// Print config if required
if printConfig {
printConfigSummary()
}
// Sanity checks
sanityChecks()
// assign variable values to config values
awsRegions = viper.GetStringSlice("aws.regions")
envNameIgnore = viper.GetStringSlice("aws.ignore_environments")
instanceTypeIgnore = viper.GetStringSlice("aws.ignore_instance_types")
maxInstancesToShutdown = viper.GetInt("aws.max_instances_to_shutdown")
requiredTagKey = viper.GetString("aws.required_tag_key")
requiredTagValue = viper.GetString("aws.required_tag_value")
environmentTagKey = viper.GetString("aws.environment_tag_key")
slackEnabled = viper.GetBool("slack.enabled")
slackWebHooks = viper.GetStringSlice("slack.webhook_urls")
mockEnabled = viper.GetBool("mock.enabled")
experimentalEnabled = viper.GetBool("experimental.enabled")
asgEnabled = viper.GetBool("aws.enable_asg_support")
return
}
// setup viper
func initViper(cfgFile string) {
// Set some defaults
viper.SetDefault("log_level", "DEBUG")
viper.SetDefault("server.bind_address", "127.0.0.1")
viper.SetDefault("server.bind_port", "8080")
viper.SetDefault("server.access_log", true)
// Configuring and pulling overrides from environmental variables
viper.SetEnvPrefix("POWER_TOGGLE")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// set default config name and paths to look for it
viper.SetConfigType("yaml")
viper.SetConfigName("power-toggle-config")
viper.AddConfigPath("./")
// if the user provides a config file in a flag, lets use it
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
}
// If a config file is found, read it in.
err := viper.ReadInConfig()
// Kick-off the logging module
loggingInit(viper.GetString("log_level"))
if err == nil {
log.Infof("using config file: %s", viper.ConfigFileUsed())
} else {
log.Warningf("no config file found: using environment variables and hard-coded defaults: %v", err)
}
}
// prints the config options
func printConfigSummary() {
log.Debugf("Configuration:\n")
for _, c := range []string{
"log_level",
"server.bind_address",
"server.bind_port",
"server.tls.enabled",
"server.access_log",
"server.compression",
"aws.polling_interval",
"aws.required_tag_key",
"aws.required_tag_value",
"aws.environment_tag_key",
"aws.max_instances_to_shutdown",
"aws.enable_asg_support",
"slack.enabled",
"mock.enabled",
"mock.delay",
"mock.errors",
"experimental.enabled",
} {
log.Debugf("%s: %s\n", c, viper.GetString(c))
}
for _, c := range []string{
"aws.regions",
"aws.ignore_instance_types",
"aws.ignore_environments",
} {
log.Debugf("%s: %v\n", c, viper.GetStringSlice(c))
}
}
// checks that the config is correctly defined
func sanityChecks() {
for _, k := range []string{
"aws.required_tag_key",
"aws.required_tag_value",
"aws.environment_tag_key",
} {
if viper.GetString(k) == "" {
log.Fatalf("%s MUST be defined and not empty", k)
}
}
if len(viper.GetStringSlice("aws.regions")) == 0 {
log.Fatal("aws.regions MUST be defined and not empty")
}
for _, k := range []string{
"aws.max_instances_to_shutdown",
"aws.polling_interval",
} {
if !(viper.GetInt(k) > 0) {
log.Fatal("polling_interval MUST be defined and greater than 0")
}
}
if !(viper.GetInt("aws.max_instances_to_shutdown") > 0) {
log.Fatal("max_instances_to_shutdown MUST be defined and greater than 0")
}
if !(viper.GetInt("aws.polling_interval") > 0) {
log.Fatal("polling_interval MUST be defined and greater than 0")
}
if viper.GetBool("slack.enabled") && len(viper.GetStringSlice("slack.webhook_urls")) == 0 {
log.Warning("slack is ENABLED but slack.webhook_urls is empty")
}
}