-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.go
186 lines (147 loc) · 4.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package envh provides convenient helpers to manage easily your environment variables.
package envh
import (
"regexp"
)
// Env manages environment variables
// by giving a convenient helper
// to interact with them
type Env struct {
envs *map[string]string
}
// NewEnv creates a new Env instance
func NewEnv() Env {
return Env{parseVars()}
}
// GetAllValues retrieves a slice of all environment variables values
func (e Env) GetAllValues() []string {
results := []string{}
for _, v := range *e.envs {
results = append(results, v)
}
return results
}
// GetAllKeys retrieves a slice of all environment variables keys
func (e Env) GetAllKeys() []string {
results := []string{}
for k := range *e.envs {
results = append(results, k)
}
return results
}
// GetString returns a string if variable exists
// or an error otherwise
func (e Env) GetString(key string) (string, error) {
return getString(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
})
}
// GetStringUnsecured is insecured version of GetString to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing, it returns default zero string value.
// This function has to be used carefully
func (e Env) GetStringUnsecured(key string) string {
if val, err := getString(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
}); err == nil {
return val
}
return ""
}
// GetInt returns an integer if variable exists
// or an error if value is not an integer or doesn't exist
func (e Env) GetInt(key string) (int, error) {
return getInt(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
})
}
// GetIntUnsecured is insecured version of GetInt to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not an int value, it returns default zero int value.
// This function has to be used carefully
func (e Env) GetIntUnsecured(key string) int {
if val, err := getInt(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
}); err == nil {
return val
}
return 0
}
// GetFloat returns a float if variable exists
// or an error if value is not a float or doesn't exist
func (e Env) GetFloat(key string) (float32, error) {
return getFloat(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
})
}
// GetFloatUnsecured is insecured version of GetFloat to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a floating value, it returns default zero floating value.
// This function has to be used carefully
func (e Env) GetFloatUnsecured(key string) float32 {
if val, err := getFloat(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
}); err == nil {
return val
}
return 0
}
// GetBool returns a boolean if variable exists
// or an error if value is not a boolean or doesn't exist
func (e Env) GetBool(key string) (bool, error) {
return getBool(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
})
}
// GetBoolUnsecured is insecured version of GetBool to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a boolean value, it returns default zero boolean value.
// This function has to be used carefully
func (e Env) GetBoolUnsecured(key string) bool {
if val, err := getBool(func() (string, bool) {
v, ok := (*e.envs)[key]
return v, ok
}); err == nil {
return val
}
return false
}
// FindEntries retrieves all keys matching a given regexp and their
// corresponding values
func (e Env) FindEntries(reg string) (map[string]string, error) {
results := map[string]string{}
r, err := regexp.Compile(reg)
if err != nil {
return results, err
}
for k, v := range *e.envs {
if r.MatchString(k) {
results[k] = v
}
}
return results, nil
}
// FindEntriesUnsecured is insecured version of FindEntriesUnsecured to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a boolean value, it returns default empty map.
// This function has to be used carefully.
func (e Env) FindEntriesUnsecured(reg string) map[string]string {
results := map[string]string{}
r, err := regexp.Compile(reg)
if err != nil {
return results
}
for k, v := range *e.envs {
if r.MatchString(k) {
results[k] = v
}
}
return results
}