-
Notifications
You must be signed in to change notification settings - Fork 1
/
snek.go
49 lines (40 loc) · 1.45 KB
/
snek.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
// Package snek is the primary package for the snek application configuration manager
package snek
import (
"fmt"
"os"
)
/* Snek - a very simple environment variable-based config management system, focused on
container/kubernetes use cases. Very small fangs. */
var defaults = make(map[string]string)
// Default registers a default value for a particular config environment variable
func Default(envvar string, defaultValue string) {
defaults[envvar] = defaultValue
}
// Get retrieves the value for a config key. If the env var is set, it is retrieved.
// Otherwise, the default value is returned. If neither exist, an empty string "" is returned.
func Get(envvar string) string {
// return env var if it exists
envval := os.Getenv(envvar)
if envval != "" {
return envval
}
// otherwise return the default or "" if no default was registered
val, _ := defaults[envvar]
return val
}
// GetOrError retrieves the value for a config key. If the env var is set, it is retrieved.
// Otherwise, the default value is returned. If neither exist, an error is thrown.
func GetOrError(envvar string) (string, error) {
// return env var if it exists
envVal := os.Getenv(envvar)
if envVal != "" {
return envVal, nil
}
// otherwise return the default or "" if no default was registered
defaultVal, present := defaults[envvar]
if present {
return defaultVal, nil
}
return "", fmt.Errorf("Config key %s not set, either as environment variable or default value", envvar)
}