-
Notifications
You must be signed in to change notification settings - Fork 4
/
configEnvironment.go
275 lines (237 loc) · 8.96 KB
/
configEnvironment.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package caretakerd
import (
"github.com/echocat/caretakerd/errors"
"github.com/echocat/caretakerd/panics"
"github.com/echocat/caretakerd/service"
"github.com/echocat/caretakerd/values"
"os"
"strings"
)
const (
envPrefix = "CTD."
)
var globalEnvKeyToHandler = map[string]func(*Config, string) error{
// logger.config
"LOG_LEVEL": handleGlobalLogLevelEnv,
"LOG_STDOUT_LEVEL": handleGlobalLogStdoutLevelEnv,
"LOG_STDERR_LEVEL": handleGlobalLogStderrLevelEnv,
"LOG_FILE": handleGlobalLogFilenameEnv,
"LOG_FILE_NAME": handleGlobalLogFilenameEnv,
"LOG_MAX_SIZE": handleGlobalLogMaxSizeInMbEnv,
"LOG_MAX_SIZE_IN_MB": handleGlobalLogMaxSizeInMbEnv,
"LOG_MAX_BACKUPS": handleGlobalLogMaxBackupsEnv,
"LOG_MAX_AGE": handleGlobalLogMaxAgeInDaysEnv,
"LOG_MAX_AGE_IN_DAYS": handleGlobalLogMaxAgeInDaysEnv,
}
var serviceEnvKeyToFunction = map[string]func(*service.Config, string) error{
// service.config
"CMD": handleServiceCommandEnv,
"COMMAND": handleServiceCommandEnv,
"TYPE": handleServiceTypeEnv,
"START_DELAY": handleServiceStartDelayInSecondsEnv,
"START_DELAY_IN_SECONDS": handleServiceStartDelayInSecondsEnv,
"RESTART_DELAY": handleServiceRestartDelayInSecondsEnv,
"RESTART_DELAY_IN_SECONDS": handleServiceRestartDelayInSecondsEnv,
"EXIT_CODE": handleServiceSuccessExitCodesEnv,
"EXIT_CODES": handleServiceSuccessExitCodesEnv,
"SUCCESS_EXIT_CODE": handleServiceSuccessExitCodesEnv,
"SUCCESS_EXIT_CODES": handleServiceSuccessExitCodesEnv,
"STOP_WAIT": handleServiceStopWaitInSecondsEnv,
"STOP_WAIT_IN_SECONDS": handleServiceStopWaitInSecondsEnv,
"USER": handleServiceUserEnv,
"DIR": handleServiceDirectoryEnv,
"DIRECORY": handleServiceDirectoryEnv,
"RESTART": handleServiceAutoRestartEnv,
"AUTO_RESTART": handleServiceAutoRestartEnv,
"INHERIT_ENV": handleServiceInheritEnvironmentEnv,
"INHERIT_ENVIRONMENT": handleServiceInheritEnvironmentEnv,
// logger.config
"LOG_LEVEL": handleServiceLogLevelEnv,
"LOG_STDOUT_LEVEL": handleServiceLogStdoutLevelEnv,
"LOG_STDERR_LEVEL": handleServiceLogStderrLevelEnv,
"LOG_FILE": handleServiceLogFilenameEnv,
"LOG_FILE_NAME": handleServiceLogFilenameEnv,
"LOG_MAX_SIZE": handleServiceLogMaxSizeInMbEnv,
"LOG_MAX_SIZE_IN_MB": handleServiceLogMaxSizeInMbEnv,
"LOG_MAX_BACKUPS": handleServiceLogMaxBackupsEnv,
"LOG_MAX_AGE": handleServiceLogMaxAgeInDaysEnv,
"LOG_MAX_AGE_IN_DAYS": handleServiceLogMaxAgeInDaysEnv,
}
var serviceSubEnvKeyToFunction = map[string]func(*service.Config, string, string) error{
// service.config
"ENV": handleEnvironmentEnv,
"ENVIRONMENT": handleEnvironmentEnv,
}
// Appendable indicates an instance where a string can be appended.
type Appendable interface {
Append(value string) error
}
// Putable indicates an instance of a kind of map where a value with a key could be putted to.
type Putable interface {
Put(key string, value string) error
}
// EnrichFromEnvironment enriches the current Config instance with configuration from the environment
// variables.
func (instance Config) EnrichFromEnvironment() Config {
result := &instance
for _, plainEnviron := range os.Environ() {
environ := strings.SplitN(plainEnviron, "=", 2)
var err error
if len(environ) > 1 {
err = instance.handleUnknownEnv(plainEnviron, environ[0], environ[1])
} else {
err = instance.handleUnknownEnv(plainEnviron, environ[0], "")
}
if err != nil {
panics.New("Could not handle environment variable '%s'. Got: %s", plainEnviron, err.Error()).CausedBy(err).Throw()
}
}
return *result
}
func (instance *Config) handleUnknownEnv(full string, key string, value string) error {
prefixLength := len(envPrefix)
var err error
if strings.HasPrefix(strings.ToUpper(key), envPrefix) && len(key) > prefixLength {
err = instance.handleEnv(full, key[prefixLength:], value)
} else {
err = nil
}
return err
}
func (instance *Config) handleEnv(full string, key string, value string) error {
if handler, ok := globalEnvKeyToHandler[key]; ok {
return handler(instance, value)
}
parts := strings.SplitN(key, ".", 3)
var err error
if len(parts) == 2 {
err = instance.handleServiceEnv(full, parts[0], parts[1], value)
} else if len(parts) == 3 {
err = instance.handleServiceMapEnv(full, parts[0], parts[1], parts[2], value)
} else {
err = errors.New("Illegal environment variable found: '%s'. Unexpected number of '.' chracters.", full)
}
return err
}
func handleGlobalLogLevelEnv(conf *Config, value string) error {
return conf.Logger.Level.Set(value)
}
func handleGlobalLogStdoutLevelEnv(conf *Config, value string) error {
return conf.Logger.StdoutLevel.Set(value)
}
func handleGlobalLogStderrLevelEnv(conf *Config, value string) error {
return conf.Logger.StderrLevel.Set(value)
}
func handleGlobalLogFilenameEnv(conf *Config, value string) error {
return conf.Logger.Filename.Set(value)
}
func handleGlobalLogMaxSizeInMbEnv(conf *Config, value string) error {
return conf.Logger.MaxSizeInMb.Set(value)
}
func handleGlobalLogMaxBackupsEnv(conf *Config, value string) error {
return conf.Logger.MaxBackups.Set(value)
}
func handleGlobalLogMaxAgeInDaysEnv(conf *Config, value string) error {
return conf.Logger.MaxAgeInDays.Set(value)
}
func handleServiceCommandEnv(conf *service.Config, value string) error {
conf.Command = append(conf.Command, parseCmd(value)...)
return nil
}
func handleServiceTypeEnv(conf *service.Config, value string) error {
return conf.Type.Set(value)
}
func handleServiceStartDelayInSecondsEnv(conf *service.Config, value string) error {
return conf.StartDelayInSeconds.Set(value)
}
func handleServiceRestartDelayInSecondsEnv(conf *service.Config, value string) error {
return conf.RestartDelayInSeconds.Set(value)
}
func handleServiceSuccessExitCodesEnv(conf *service.Config, value string) error {
return conf.SuccessExitCodes.Set(value)
}
func handleServiceStopWaitInSecondsEnv(conf *service.Config, value string) error {
return conf.StopWaitInSeconds.Set(value)
}
func handleServiceUserEnv(conf *service.Config, value string) error {
return conf.User.Set(value)
}
func handleServiceDirectoryEnv(conf *service.Config, value string) error {
return conf.Directory.Set(value)
}
func handleServiceAutoRestartEnv(conf *service.Config, value string) error {
return conf.AutoRestart.Set(value)
}
func handleServiceLogLevelEnv(conf *service.Config, value string) error {
return conf.Logger.Level.Set(value)
}
func handleServiceLogStdoutLevelEnv(conf *service.Config, value string) error {
return conf.Logger.StdoutLevel.Set(value)
}
func handleServiceLogStderrLevelEnv(conf *service.Config, value string) error {
return conf.Logger.StderrLevel.Set(value)
}
func handleServiceLogFilenameEnv(conf *service.Config, value string) error {
return conf.Logger.Filename.Set(value)
}
func handleServiceLogMaxSizeInMbEnv(conf *service.Config, value string) error {
return conf.Logger.MaxSizeInMb.Set(value)
}
func handleServiceLogMaxBackupsEnv(conf *service.Config, value string) error {
return conf.Logger.MaxBackups.Set(value)
}
func handleServiceLogMaxAgeInDaysEnv(conf *service.Config, value string) error {
return conf.Logger.MaxAgeInDays.Set(value)
}
func handleServiceInheritEnvironmentEnv(conf *service.Config, value string) error {
return conf.InheritEnvironment.Set(value)
}
func (instance *Config) handleServiceEnv(full string, serviceName string, key string, value string) error {
targetKey := strings.ToUpper(key)
if handler, ok := serviceEnvKeyToFunction[targetKey]; ok {
return instance.Services.Configure(serviceName, value, handler)
}
return errors.New("Unknown configuration type '%s' for service '%s'.", key, serviceName)
}
func handleEnvironmentEnv(conf *service.Config, key string, value string) error {
return conf.Environment.Put(key, value)
}
func (instance *Config) handleServiceMapEnv(full string, serviceName string, key string, subKey string, value string) error {
targetKey := strings.ToUpper(key)
if handler, ok := serviceSubEnvKeyToFunction[targetKey]; ok {
return instance.Services.ConfigureSub(serviceName, subKey, value, handler)
}
return errors.New("Unknown configuration type '%s' for service '%s'.", key, serviceName)
}
func parseCmd(in string) []values.String {
result := []values.String{}
inEscape := false
inQuotes := false
buf := ""
for i := 0; i < len(in); i++ {
c := in[i]
if inEscape {
buf += string(c)
inEscape = false
} else if c == '\\' {
inEscape = true
} else if inQuotes {
if c == '"' {
inQuotes = false
} else {
buf += string(c)
}
} else if c == ' ' || c == '\t' || c == '\r' || c == '\n' {
result = append(result, values.String(buf))
buf = ""
} else if c == '"' {
inQuotes = true
} else {
buf += string(c)
}
}
if len(buf) > 0 {
result = append(result, values.String(buf))
}
return result
}