/
environment.go
56 lines (46 loc) · 1021 Bytes
/
environment.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
package cfgur
import (
"github.com/spf13/cast"
"os"
)
type envCfg struct {
}
func (cfg *envCfg) Init() error {
return nil
}
func NewEnvMarshaller() ConfigUnmarshaler {
return &commonMarshaller{ConfigProvider: &envCfg{}}
}
func (cfg *envCfg) GetStringE(key string) (string, bool) {
key = toUnderscoreKey(key)
if v, ok := os.LookupEnv(key); ok {
return v, true
}
return "", false
}
func (cfg *envCfg) GetIntE(key string) (int64, bool) {
key = toUnderscoreKey(key)
if v, ok := os.LookupEnv(key); ok {
return cast.ToInt64(v), true
}
return 0, false
}
func (cfg *envCfg) GetBoolE(key string) (bool, bool) {
key = toUnderscoreKey(key)
if v, ok := os.LookupEnv(key); ok {
return cast.ToBool(v), true
}
return false, false
}
func (cfg *envCfg) GetString(key string) string {
val, _ := cfg.GetStringE(key)
return val
}
func (cfg *envCfg) GetInt(key string) int64 {
val, _ := cfg.GetIntE(key)
return val
}
func (cfg *envCfg) GetBool(key string) bool {
val, _ := cfg.GetBoolE(key)
return val
}