-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenv.go
35 lines (31 loc) · 903 Bytes
/
env.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
package gonfig
import (
"os"
"strings"
)
// EnvConfig can be used to read values from the environment
// into the underlaying Configurable
type EnvConfig struct {
Configurable
Prefix string
}
// Creates a new Env config backed by a memory config
func NewEnvConfig(prefix string) ReadableConfig {
cfg := &EnvConfig{NewMemoryConfig(), prefix}
cfg.Load()
return cfg
}
// Loads the data from os.Environ() to the underlaying Configurable.
// if a Prefix is set then variables are imported with self.Prefix removed from the name
// so MYAPP_test=1 exported in env and read from ENV by EnvConfig{Prefix:"MYAPP_"} can be found from
// EnvConfig.Get("test")
func (self *EnvConfig) Load() (err error) {
env := os.Environ()
for _, pair := range env {
kv := strings.Split(pair, "=")
if kv != nil && len(kv) >= 2 {
self.Set(strings.Replace(kv[0], self.Prefix, "", 1), kv[1])
}
}
return nil
}