-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_loader.go
46 lines (40 loc) · 947 Bytes
/
env_loader.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
package config
import (
"os"
"strings"
)
// envLoader loads and processes
type envLoader struct {
watchForChanges bool
prefix string
}
// NewEnvLoader returns a env vars parser
func NewEnvLoader(watchForChanges bool, prefix string) Loader {
loader := &envLoader{
watchForChanges: watchForChanges,
prefix: prefix,
}
return loader
}
func (f *envLoader) IsWatchable() bool {
return f.watchForChanges
}
func (f envLoader) Parse() (map[string]string, error) {
// Collect the environment variable
// If prefix is provided returning only variables with prefix
var res = map[string]string{}
if f.prefix == "" {
for _, v := range os.Environ() {
kv := strings.SplitN(v, "=", 2)
res[kv[0]] = kv[1]
}
} else {
for _, v := range os.Environ() {
if strings.HasPrefix(strings.ToLower(v), strings.ToLower(f.prefix)) {
kv := strings.SplitN(v, "=", 2)
res[kv[0]] = kv[1]
}
}
}
return res, nil
}