-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
150 lines (124 loc) · 3.74 KB
/
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
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
package env
import (
"os"
"strconv"
"time"
)
var (
errVariableNotSet = "Variable %s not set"
errParseVariable = "Error trying to parse %s: %s"
)
// String will return the environment variable when it is not empty. If the
// value is empty (or not set), the notifier will be notified and the default
// value will be returned.
func String(env, def string) string {
ev := os.Getenv(env)
if ev == "" {
notifier.Notify(errVariableNotSet, env)
return def
}
return ev
}
// Bool will return true when the environment applies to the strconv true rules,
// it returns false if it applies to the strconv false rules. It will return the
// default value if none of these apply and the notifier will be notified.
func Bool(env string, def bool) bool {
ev := os.Getenv(env)
if ev == "" {
notifier.Notify(errVariableNotSet, env)
return def
}
b, err := strconv.ParseBool(ev)
if err != nil {
notifier.Notify(errParseVariable, env, err)
return def
}
return b
}
// Duration will try and parse the duration given in the environment variable.
// If the variable is not set or not a valid duration, the notifier will be
// notified and the default value will be returned.
func Duration(env string, def time.Duration) time.Duration {
ev := os.Getenv(env)
if ev == "" {
notifier.Notify(errVariableNotSet, env)
return def
}
dur, err := time.ParseDuration(ev)
if err != nil {
notifier.Notify(errParseVariable, env, err.Error())
return def
}
return dur
}
func parseInt(env string, base, bitSize int) (int64, bool) {
ev := os.Getenv(env)
if ev == "" {
notifier.Notify(errVariableNotSet, env)
return 0, false
}
i, err := strconv.ParseInt(ev, base, bitSize)
if err != nil {
notifier.Notify(errParseVariable, env, err.Error())
return 0, false
}
return i, true
}
// Int64 will try and parse the value given in the environment variable as an
// int64. If the variable is not set or the value is not a valid int64, the
// notifier will be notified and the default value will be returned.
func Int64(env string, def int64) int64 {
if v, ok := parseInt(env, 10, 64); ok {
return v
}
return def
}
// Int32 will try and parse the value given in the environment variable as an
// int32. If the variable is not set or the value is not a valid int32, the
// notifier will be notified and the default value will be returned.
func Int32(env string, def int32) int32 {
if v, ok := parseInt(env, 10, 32); ok {
return int32(v)
}
return def
}
// Int will try and parse the value given in the environment variable as an int.
// If the variable is not set or the value is not a valid int, the notifier will
// be notified and the default value will be returned.
func Int(env string, def int) int {
if v, ok := parseInt(env, 10, 0); ok {
return int(v)
}
return def
}
func parseFloat(env string, bitSize int) (float64, bool) {
ev := os.Getenv(env)
if ev == "" {
notifier.Notify(errVariableNotSet, env)
return 0, false
}
i, err := strconv.ParseFloat(ev, bitSize)
if err != nil {
notifier.Notify(errParseVariable, env, err.Error())
return 0, false
}
return i, true
}
// Float64 will try and parse the value given in the environment variable as an
// float64. If the variable is not set or the value is not a valid float64, the
// notifier will be notified and the default value will be returned.
func Float64(env string, def float64) float64 {
if v, ok := parseFloat(env, 64); ok {
return v
}
return def
}
// Float32 will try and parse the value given in the environment variable as an
// float32. If the variable is not set or the value is not a valid float32, the
// notifier will be notified and the default value will be returned.
func Float32(env string, def float32) float32 {
if v, ok := parseFloat(env, 32); ok {
return float32(v)
}
return def
}