-
Notifications
You must be signed in to change notification settings - Fork 7
/
env.go
205 lines (161 loc) · 4.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package handy
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
)
// EnvChecker holds instructions to assert an environment variable
type EnvChecker struct {
VarName string
DefaultValue string
Mandatory bool
DebugPrint bool
}
func debugLog(msg string, debugPrint bool) {
if debugPrint {
log.Println(msg)
}
}
// EnvCheck Test environment variables
func EnvCheck(varName, defaultValue string, mandatory, debugPrint bool) error {
if os.Getenv(varName) != `` {
debugLog(fmt.Sprintf(`environment variable "%s" asserted`, varName), debugPrint)
return nil
}
if defaultValue != `` {
if err := os.Setenv(varName, defaultValue); err != nil {
return nil
}
debugLog(fmt.Sprintf(`environment variable "%s" asserted with default value`, varName), debugPrint)
return nil
}
if mandatory {
return fmt.Errorf(`required environment variable "%s" isn't set`, varName)
}
return nil
}
// EnvStr returns the env var value as string
func EnvStr(key, defaultValue string) string {
if os.Getenv(key) != `` {
return os.Getenv(key)
}
return defaultValue
}
// EnvStrS returns the env var value as []string (string slice)
func EnvStrS(key, separator string, defaultValue []string) []string {
if os.Getenv(key) != `` {
return strings.Split(os.Getenv(key), separator)
}
if len(defaultValue) > 0 {
return defaultValue
}
return []string{}
}
// EnvInt returns the env var value as int
func EnvInt(key string, defaultValue int) int {
if os.Getenv(key) != `` {
if i, err := strconv.Atoi(os.Getenv(key)); err == nil {
return i
}
}
return defaultValue
}
// EnvInt64 returns the env var value as int64
func EnvInt64(key string, defaultValue int64) int64 {
if os.Getenv(key) != `` {
if i, err := strconv.ParseInt(os.Getenv(key), 10, 64); err == nil {
return i
}
}
return defaultValue
}
// EnvIntS returns the env var value as []int
func EnvIntS(key, separator string, defaultValue []int) []int {
if os.Getenv(key) != `` {
a := strings.Split(os.Getenv(key), separator)
is := make([]int, len(a))
for i, x := range a {
is[i], _ = strconv.Atoi(x)
}
return is
}
if len(defaultValue) > 0 {
return defaultValue
}
return []int{}
}
// EnvFloat64 returns the env var value as float64
func EnvFloat64(key string, defaultValue float64) float64 {
if os.Getenv(key) != `` {
if f, err := strconv.ParseFloat(os.Getenv(key), 64); err == nil {
return f
}
}
return defaultValue
}
// EnvBool returns the env var value as boolean
func EnvBool(key string, defaultValue bool) bool {
if os.Getenv(key) != `` {
if b, err := strconv.ParseBool(os.Getenv(key)); err == nil {
return b
}
}
return defaultValue
}
// EnvCheckerNew returns a new instance of EnvChecker to be used with EnvCheckMany()
func EnvCheckerNew(varName, defaultValue string, mandatory, debugPrint bool) EnvChecker {
return EnvChecker{
VarName: varName,
DefaultValue: defaultValue,
Mandatory: mandatory,
DebugPrint: debugPrint,
}
}
// EnvCheckMany Test multiple environment variables at once
func EnvCheckMany(envCheckers []EnvChecker) error {
for _, c := range envCheckers {
if err := EnvCheck(c.VarName, c.DefaultValue, c.Mandatory, c.DebugPrint); err != nil {
return err
}
}
return nil
}
var reEnvVarRow = regexp.MustCompile(`^([A-Za-z][0-9A-Za-z_]*)=(\S+)`)
// EnvLoadFromDisk loads a file from disk, containing variables written in KEY=VALUE format
// fileName is the file name with complete path
// mustHave forces an error if the file doesn't exist
// overwriteValue when false, makes the engine skip env vars that are already definied
func EnvLoadFromDisk(fileName string, mustHave, overwriteValues bool) error {
content, err := ioutil.ReadFile(fileName)
if err != nil {
if mustHave {
return err
}
return nil
}
text := string(content)
text = strings.ReplaceAll(text, "\r\n", "\n")
rows := strings.Split(text, "\n")
for _, row := range rows {
row = strings.TrimSpace(row)
matches := reEnvVarRow.FindStringSubmatch(row)
if len(matches) != 3 {
continue
}
key := matches[1]
value := matches[2]
if os.Getenv(key) != "" {
if !overwriteValues {
continue
}
}
if err := os.Setenv(key, value); err != nil {
return err
}
}
return nil
}