-
Notifications
You must be signed in to change notification settings - Fork 1
/
genv.go
236 lines (182 loc) · 4.13 KB
/
genv.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package genv
import (
"bufio"
"log"
"os"
"regexp"
"strconv"
"strings"
"sync"
)
var once sync.Once
func init() {
once.Do(func() {
Load()
})
}
// EnvVariable contains information about the environment variable, such as key,
// value, and default value.
type EnvVariable struct {
Key string
Val string
DefaultValue interface{}
IsDefined bool
}
// EnvVariables is where environment variables are stored.
var EnvVariables = make(map[string]*EnvVariable)
// Key is used to determine the path of the environment variable to be accessed.
//
// genv.Key("env-key").String()
func Key(key string) *EnvVariable {
envVar, ok := EnvVariables[key]
if !ok {
val, ok := os.LookupEnv(key)
EnvVariables[key] = &EnvVariable{Key: key, Val: val, IsDefined: ok}
return EnvVariables[key]
}
return envVar
}
// Default is used to specify the default value for the environment
// variable to be accessed.
//
// genv.Key("env-key").Default("defaultValue").String()
func (e *EnvVariable) Default(defaultValue interface{}) *EnvVariable {
e.DefaultValue = defaultValue
return e
}
// Update is used to update the value of the corresponding environment variable.
//
// genv.Key("env-key").Update("updatedValue")
func (e *EnvVariable) Update(value interface{}) {
switch value := value.(type) {
case bool:
e.Val = strconv.FormatBool(value)
case float64:
e.Val = strconv.FormatFloat(value, 'f', -1, 64)
case int:
e.Val = strconv.FormatInt(int64(value), 10)
case string:
e.Val = value
}
e.IsDefined = true
os.Setenv(e.Key, e.Val)
}
// Bool method is used for environment variables of type bool.
//
// genv.Key("env-key").Bool()
func (e *EnvVariable) Bool() bool {
var dv bool
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(bool)
}
return dv
}
val, _ := strconv.ParseBool(e.Val)
return val
}
// Float method is used for environment variables of type float.
//
// genv.Key("env-key").Float()
func (e *EnvVariable) Float() float64 {
var dv float64
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(float64)
}
return dv
}
val, _ := strconv.ParseFloat(e.Val, 64)
return val
}
// Int method is used for environment variables of type int.
//
// genv.Key("env-key").Int()
func (e *EnvVariable) Int() int {
var dv int
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(int)
}
return dv
}
val, _ := strconv.ParseInt(e.Val, 10, 32)
return int(val)
}
// String method is used for environment variables of type string.
//
// genv.Key("env-key").String()
func (e *EnvVariable) String() string {
var dv string
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(string)
}
return dv
}
return e.Val
}
const keyValRegex = `^\s*([\w.-]+)\s*=\s*(.*)?\s*$`
// Load method allows environment variables to be loaded from the desired file.
//
// dotenv.Load(".envfile")
func Load(path ...string) (err error) {
var dotenvPath string
if len(path) > 0 {
dotenvPath = path[0]
} else {
dotenvPath = ".env"
}
source, err := os.Open(dotenvPath)
if err != nil {
log.Print(err)
return
}
defer source.Close()
variables, err := parse(source)
if err != nil {
log.Print(err)
return
}
for key, val := range variables {
if _, ok := os.LookupEnv(key); !ok {
os.Setenv(key, val)
}
}
return
}
func parse(source *os.File) (variables map[string]string, err error) {
variables = make(map[string]string)
r, err := regexp.Compile(keyValRegex)
if err != nil {
return
}
scanner := bufio.NewScanner(source)
for scanner.Scan() {
key, val := parseLine(scanner.Text(), r)
if key != "" && val != "" {
variables[key] = val
}
}
if err = scanner.Err(); err != nil {
return
}
return
}
func parseLine(line string, r *regexp.Regexp) (key string, val string) {
matches := r.FindStringSubmatch(line)
if len(matches) == 3 {
key, val = matches[1], matches[2]
if val != "" {
end := len(val) - 1
isDoubleQuoted := val[0] == '"' && val[end] == '"'
isSingleQuoted := val[0] == '\'' && val[end] == '\''
if isSingleQuoted || isDoubleQuoted {
val = val[1:end]
} else {
val = strings.Trim(val, " ")
}
}
}
return
}