-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
494 lines (467 loc) · 10.9 KB
/
env.go
File metadata and controls
494 lines (467 loc) · 10.9 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package env
import (
"os"
"strconv"
"strings"
"time"
)
// Get returns the environment variable for key or fallback when empty.
// @group Typed getters
// @behavior readonly
//
// Examples use github.com/goforj/godump to illustrate the concrete type.
//
// Example: fallback when unset
//
// os.Unsetenv("DB_HOST")
// host := env.Get("DB_HOST", "localhost")
// env.Dump(host)
// // #string "localhost"
//
// Example: prefer existing value
//
// _ = os.Setenv("DB_HOST", "db.internal")
// host = env.Get("DB_HOST", "localhost")
// env.Dump(host)
// // #string "db.internal"
func Get(key, fallback string) string {
val := os.Getenv(key)
if len(val) == 0 {
return fallback
}
return val
}
// GetInt parses an int from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Example: fallback used
//
// os.Unsetenv("PORT")
// port := env.GetInt("PORT", "3000")
// env.Dump(port)
// // #int 3000
//
// Example: env overrides fallback
//
// _ = os.Setenv("PORT", "8080")
// port = env.GetInt("PORT", "3000")
// env.Dump(port)
// // #int 8080
func GetInt(key, fallback string) int {
val := os.Getenv(key)
if val != "" {
if ret, err := strconv.Atoi(val); err == nil {
return ret
}
}
if fallback != "" {
if ret, err := strconv.Atoi(fallback); err == nil {
return ret
}
}
return 0
}
// GetInt64 parses an int64 from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Example: parse large numbers safely
//
// _ = os.Setenv("MAX_SIZE", "1048576")
// size := env.GetInt64("MAX_SIZE", "512")
// env.Dump(size)
// // #int64 1048576
//
// Example: fallback when unset
//
// os.Unsetenv("MAX_SIZE")
// size = env.GetInt64("MAX_SIZE", "512")
// env.Dump(size)
// // #int64 512
func GetInt64(key, fallback string) int64 {
val := os.Getenv(key)
if val != "" {
if ret, err := strconv.ParseInt(val, 10, 64); err == nil {
return ret
}
}
if fallback != "" {
if ret, err := strconv.ParseInt(fallback, 10, 64); err == nil {
return ret
}
}
return 0
}
// GetUint parses a uint from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Example: defaults to fallback when missing
//
// os.Unsetenv("WORKERS")
// workers := env.GetUint("WORKERS", "4")
// env.Dump(workers)
// // #uint 4
//
// Example: uses provided unsigned value
//
// _ = os.Setenv("WORKERS", "16")
// workers = env.GetUint("WORKERS", "4")
// env.Dump(workers)
// // #uint 16
func GetUint(key, fallback string) uint {
val := os.Getenv(key)
if val != "" {
if i, err := strconv.ParseUint(val, 10, 32); err == nil {
return uint(i)
}
}
if fallback != "" {
if i, err := strconv.ParseUint(fallback, 10, 32); err == nil {
return uint(i)
}
}
return 0
}
// GetUint64 parses a uint64 from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Example: high range values
//
// _ = os.Setenv("MAX_ITEMS", "5000")
// maxItems := env.GetUint64("MAX_ITEMS", "100")
// env.Dump(maxItems)
// // #uint64 5000
//
// Example: fallback when unset
//
// os.Unsetenv("MAX_ITEMS")
// maxItems = env.GetUint64("MAX_ITEMS", "100")
// env.Dump(maxItems)
// // #uint64 100
func GetUint64(key, fallback string) uint64 {
val := os.Getenv(key)
if val != "" {
if i, err := strconv.ParseUint(val, 10, 64); err == nil {
return i
}
}
if fallback != "" {
if i, err := strconv.ParseUint(fallback, 10, 64); err == nil {
return i
}
}
return 0
}
// GetFloat parses a float64 from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Example: override threshold
//
// _ = os.Setenv("THRESHOLD", "0.82")
// threshold := env.GetFloat("THRESHOLD", "0.75")
// env.Dump(threshold)
// // #float64 0.82
//
// Example: fallback with decimal string
//
// os.Unsetenv("THRESHOLD")
// threshold = env.GetFloat("THRESHOLD", "0.75")
// env.Dump(threshold)
// // #float64 0.75
func GetFloat(key, fallback string) float64 {
val := os.Getenv(key)
if val != "" {
if f, err := strconv.ParseFloat(val, 64); err == nil {
return f
}
}
if fallback != "" {
if f, err := strconv.ParseFloat(fallback, 64); err == nil {
return f
}
}
return 0
}
// GetBool parses a boolean from an environment variable or fallback string.
// @group Typed getters
// @behavior readonly
//
// Accepted values: true/false, 1/0, t/f (case-insensitive). Invalid entries fall back.
//
// Example: numeric truthy
//
// _ = os.Setenv("DEBUG", "1")
// debug := env.GetBool("DEBUG", "false")
// env.Dump(debug)
// // #bool true
//
// Example: fallback string
//
// os.Unsetenv("DEBUG")
// debug = env.GetBool("DEBUG", "false")
// env.Dump(debug)
// // #bool false
func GetBool(key, fallback string) bool {
val := os.Getenv(key)
if val != "" {
if ret, err := strconv.ParseBool(val); err == nil {
return ret
}
}
if fallback != "" {
if ret, err := strconv.ParseBool(fallback); err == nil {
return ret
}
}
return false
}
// GetDuration parses a Go duration string (e.g. "5s", "10m", "1h").
// @group Typed getters
// @behavior readonly
//
// Example: override request timeout
//
// _ = os.Setenv("HTTP_TIMEOUT", "30s")
// timeout := env.GetDuration("HTTP_TIMEOUT", "5s")
// env.Dump(timeout)
// // #time.Duration 30s
//
// Example: fallback when unset
//
// os.Unsetenv("HTTP_TIMEOUT")
// timeout = env.GetDuration("HTTP_TIMEOUT", "5s")
// env.Dump(timeout)
// // #time.Duration 5s
func GetDuration(key, fallback string) time.Duration {
val := os.Getenv(key)
if val != "" {
if d, err := time.ParseDuration(val); err == nil {
return d
}
}
if fallback != "" {
if d, err := time.ParseDuration(fallback); err == nil {
return d
}
}
return 0
}
// GetSlice splits a comma-separated string into a []string with trimming.
// @group Typed getters
// @behavior readonly
//
// Example: trimmed addresses
//
// _ = os.Setenv("PEERS", "10.0.0.1, 10.0.0.2")
// peers := env.GetSlice("PEERS", "")
// env.Dump(peers)
// // #[]string [
// // 0 => "10.0.0.1" #string
// // 1 => "10.0.0.2" #string
// // ]
//
// Example: empty becomes empty slice
//
// os.Unsetenv("PEERS")
// peers = env.GetSlice("PEERS", "")
// env.Dump(peers)
// // #[]string []
func GetSlice(key, fallback string) []string {
val := Get(key, fallback)
if val == "" {
return []string{}
}
parts := strings.Split(val, ",")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
return parts
}
// GetMap parses key=value pairs separated by commas into a map.
// @group Typed getters
// @behavior readonly
//
// Example: parse throttling config
//
// _ = os.Setenv("LIMITS", "read=10, write=5, burst=20")
// limits := env.GetMap("LIMITS", "")
// env.Dump(limits)
// // #map[string]string [
// // "burst" => "20" #string
// // "read" => "10" #string
// // "write" => "5" #string
// // ]
//
// Example: returns empty map when unset or blank
//
// os.Unsetenv("LIMITS")
// limits = env.GetMap("LIMITS", "")
// env.Dump(limits)
// // #map[string]string []
func GetMap(key, fallback string) map[string]string {
val := Get(key, fallback)
m := map[string]string{}
if strings.TrimSpace(val) == "" {
return m
}
pairs := strings.Split(val, ",")
for _, p := range pairs {
kv := strings.SplitN(strings.TrimSpace(p), "=", 2)
if len(kv) == 2 {
m[kv[0]] = kv[1]
}
}
return m
}
// GetMapInt parses key=int pairs separated by commas into a map.
// Invalid, missing, or non-positive values fall back to defaultValue.
// @group Typed getters
// @behavior readonly
//
// Example: parse worker queue weights
//
// _ = os.Setenv("QUEUE_WEIGHTS", "critical=6, default=3, low=1")
// weights := env.GetMapInt("QUEUE_WEIGHTS", "", 1)
// env.Dump(weights)
// // #map[string]int [
// // "critical" => 6 #int
// // "default" => 3 #int
// // "low" => 1 #int
// // ]
//
// Example: invalid values use defaultValue
//
// os.Unsetenv("QUEUE_WEIGHTS")
// weights = env.GetMapInt("QUEUE_WEIGHTS", "critical=,default=0,low=nope,misc", 2)
// env.Dump(weights)
// // #map[string]int [
// // "critical" => 2 #int
// // "default" => 2 #int
// // "low" => 2 #int
// // "misc" => 2 #int
// // ]
func GetMapInt(key, fallback string, defaultValue int) map[string]int {
val := Get(key, fallback)
m := map[string]int{}
if defaultValue <= 0 {
defaultValue = 1
}
if strings.TrimSpace(val) == "" {
return m
}
pairs := strings.Split(val, ",")
for _, p := range pairs {
entry := strings.TrimSpace(p)
if entry == "" {
continue
}
kv := strings.SplitN(entry, "=", 2)
name := strings.TrimSpace(kv[0])
if name == "" {
continue
}
parsed := defaultValue
if len(kv) == 2 {
if n, err := strconv.Atoi(strings.TrimSpace(kv[1])); err == nil && n > 0 {
parsed = n
}
}
m[name] = parsed
}
return m
}
// GetEnum ensures the environment variable's value is in the allowed list.
// @group Typed getters
// @behavior readonly
//
// Returns fallback when the environment value is not in the allowed slice.
//
// Example: accept only staged environments
//
// _ = os.Setenv("APP_ENV", "production")
// appEnv := env.GetEnum("APP_ENV", "local", []string{"local", "staging", "production"})
// env.Dump(appEnv)
// // #string "production"
//
// Example: fallback when unset
//
// os.Unsetenv("APP_ENV")
// appEnv = env.GetEnum("APP_ENV", "local", []string{"local", "staging", "production"})
// env.Dump(appEnv)
// // #string "local"
func GetEnum(key, fallback string, allowed []string) string {
val := Get(key, fallback)
for _, a := range allowed {
if val == a {
return val
}
}
for _, a := range allowed {
if fallback == a {
return fallback
}
}
return fallback
}
// MustGet returns the value of key or panics if missing/empty.
// @group Typed getters
// @behavior panic
//
// Example: required secret
//
// _ = os.Setenv("API_SECRET", "s3cr3t")
// secret := env.MustGet("API_SECRET")
// env.Dump(secret)
// // #string "s3cr3t"
//
// Example: panic on missing value
//
// os.Unsetenv("API_SECRET")
// secret = env.MustGet("API_SECRET") // panics: env variable missing: API_SECRET
func MustGet(key string) string {
val := os.Getenv(key)
if val == "" {
panic("env variable missing: " + key)
}
return val
}
// MustGetInt panics if the value is missing or not an int.
// @group Typed getters
// @behavior panic
//
// Example: ensure numeric port
//
// _ = os.Setenv("PORT", "8080")
// port := env.MustGetInt("PORT")
// env.Dump(port)
// // #int 8080
//
// Example: panic on bad value
//
// _ = os.Setenv("PORT", "not-a-number")
// _ = env.MustGetInt("PORT") // panics when parsing
func MustGetInt(key string) int {
return GetInt(key, "")
}
// MustGetBool panics if missing or invalid.
// @group Typed getters
// @behavior panic
//
// Example: gate features explicitly
//
// _ = os.Setenv("FEATURE_ENABLED", "true")
// enabled := env.MustGetBool("FEATURE_ENABLED")
// env.Dump(enabled)
// // #bool true
//
// Example: panic on invalid value
//
// _ = os.Setenv("FEATURE_ENABLED", "maybe")
// _ = env.MustGetBool("FEATURE_ENABLED") // panics when parsing
func MustGetBool(key string) bool {
return GetBool(key, "")
}