-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
197 lines (177 loc) · 4.78 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
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
package config
import (
"fmt"
"os"
"strconv"
"strings"
)
// Load configuration.
func Load(namespace string) *Config {
cfg := &Config{}
cfg.SetNamespace(namespace)
cfg.loadNamespaceEnvVars()
return cfg
}
// SetNamespace for configuration.
func (c *Config) SetNamespace(namespace string) {
c.namespace = strings.ToUpper(namespace)
}
func (c *Config) namespacePrefix() string {
return fmt.Sprintf("%s_", c.namespace)
}
func (c *Config) SetValues(values map[string]string) {
c.values = values
}
// Get reads all visible environment variables
// that belongs to the namespace.
// An optional reload parameter lets re-read
// the values from environment.
func (c *Config) Get(reload ...bool) map[string]string {
if len(reload) > 1 && reload[0] {
return c.get(true)
}
return c.get(false)
}
func (c *Config) get(reload bool) map[string]string {
if reload || len(c.values) == 0 {
return c.readNamespaceEnvVars()
}
return c.values
}
// Val read a specific namespaced environment variable
// And return its value.
// An optional reload parameter lets re-read
// the values from environment.
func (c *Config) Val(key string, reload ...bool) (value string, ok bool) {
vals := c.get(false)
if len(reload) > 1 && reload[0] {
vals = c.get(true)
}
val, ok := vals[key]
return val, ok
}
// ValOrDef read a specific namespaced environment variables
// And return its value.
// A default value is returned if key value is not found.
// An optional reload parameter lets re-read
// the value from environment.
func (c *Config) ValOrDef(key string, defVal string, reload ...bool) (value string) {
vals := c.get(false)
if len(reload) > 1 && reload[0] {
vals = c.get(true)
}
val, ok := vals[key]
if !ok {
val = defVal
}
return val
}
// ValAsInt read a specific namespaced environment variables
// and return its value as an int if it can be parsed as such type.
// A default value is returned if key value is not found.
// An optional reload parameter lets re-read
// the values from environment before.
func (c *Config) ValAsInt(key string, defVal int64, reload ...bool) (value int64) {
vals := c.get(false)
if len(reload) > 1 && reload[0] {
vals = c.get(true)
}
val, ok := vals[key]
if !ok {
return defVal
}
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return defVal
}
return i
}
// ValAsFloat read a specific namespaced environment variables
// and return its value as a float if it can be parsed as such type.
// A default value is returned if key value is not found.
// An optional reload parameter lets re-read
// the values from environment before.
func (c *Config) ValAsFloat(key string, defVal float64, reload ...bool) (value float64) {
vals := c.get(false)
if len(reload) > 1 && reload[0] {
vals = c.get(true)
}
val, ok := vals[key]
if !ok {
return defVal
}
f, err := strconv.ParseFloat(val, 64)
if err != nil {
return defVal
}
return f
}
// ValAsBool read a specific namespaced environment variables
// and return its value as a bool if it can be parsed as such type.
// A default value is returned if key value is not found.
// An optional reload parameter lets re-read
// the values from environment before.
func (c *Config) ValAsBool(key string, defVal bool, reload ...bool) (value bool) {
vals := c.get(false)
if len(reload) > 1 && reload[0] {
vals = c.get(true)
}
val, ok := vals[key]
if !ok {
return defVal
}
b, err := strconv.ParseBool(val)
if err != nil {
return defVal
}
return b
}
// loadNamespaceEnvVars load all visible environment variables
// that belongs to the namespace.
func (c *Config) loadNamespaceEnvVars() {
c.values = c.readNamespaceEnvVars()
}
// readNamespaceEnvVars reads all visible environment variables
// that belongs to the namespace.
func (c *Config) readNamespaceEnvVars() map[string]string {
nevs := make(map[string]string)
np := c.namespacePrefix()
for _, ev := range os.Environ() {
if strings.HasPrefix(ev, np) {
varval := strings.SplitN(ev, "=", 2)
if len(varval) < 2 {
continue
}
key := c.keyify(varval[0])
nevs[key] = varval[1]
}
}
return nevs
}
// keyify enviroment variable names
// i.e.: NAMESPACE_CONFIG_VALUE becomes config.value
func (c *Config) keyify(name string) string {
split := strings.Split(name, "_")
if len(split) < 1 {
return ""
}
// Without namespace prefix
wnsp := strings.Join(split[1:], ".")
// Dot separated lowercased
dots := strings.ToLower(strings.Replace(wnsp, "_", ".", -1))
return dots
}
// getEnvOrDef returns the value of environment variable or a default value.
// if this value is empty or an empty string.
// An empty string is returned if environment variable is inexistent
// and a default was not provided.
func getEnvOrDef(envar string, def ...string) string {
val := os.Getenv(envar)
if val != "" {
return val
}
if len(def) > 0 {
return def[0]
}
return ""
}