-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenvconf.go
88 lines (81 loc) · 1.75 KB
/
envconf.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
package envconf
import (
"errors"
"os"
"strconv"
"strings"
)
var (
// ErrUnsupportedType is returned if the type passed in is unsupported
ErrUnsupportedType = errors.New("Unsupported type")
)
// FromEnvP is the same as FromEnv, but panics on error
func FromEnvP(env string, value interface{}) interface{} {
ev, err := FromEnv(env, value)
if err != nil {
panic(err)
}
return ev
}
// FromEnv returns the environment variable specified by env
// using the type of value
func FromEnv(env string, value interface{}) (interface{}, error) {
envs := os.Environ()
found := false
for _, e := range envs {
if strings.HasPrefix(e, env+"=") {
found = true
break
}
}
if !found {
return value, nil
}
ev := os.Getenv(env)
switch value.(type) {
case string:
vt := interface{}(ev)
return vt, nil
case int:
i, e := strconv.ParseInt(ev, 10, 64)
return int(i), e
case int8:
i, e := strconv.ParseInt(ev, 10, 8)
return int8(i), e
case int16:
i, e := strconv.ParseInt(ev, 10, 16)
return int16(i), e
case int32:
i, e := strconv.ParseInt(ev, 10, 32)
return int32(i), e
case int64:
i, e := strconv.ParseInt(ev, 10, 64)
return i, e
case uint:
i, e := strconv.ParseUint(ev, 10, 64)
return uint(i), e
case uint8:
i, e := strconv.ParseUint(ev, 10, 8)
return uint8(i), e
case uint16:
i, e := strconv.ParseUint(ev, 10, 16)
return uint16(i), e
case uint32:
i, e := strconv.ParseUint(ev, 10, 32)
return uint32(i), e
case uint64:
i, e := strconv.ParseUint(ev, 10, 64)
return i, e
case float32:
i, e := strconv.ParseFloat(ev, 32)
return float32(i), e
case float64:
i, e := strconv.ParseFloat(ev, 64)
return float64(i), e
case bool:
i, e := strconv.ParseBool(ev)
return i, e
default:
return value, ErrUnsupportedType
}
}