-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.go
556 lines (490 loc) · 16.5 KB
/
environment.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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"fmt"
"io"
"net/url"
"os"
"sort"
"strconv"
"strings"
"text/template"
)
// Environment represents a mapping of environment variable keys to their values
type Environment map[string]string
// NewEnvironment creates a new Environment from a map of environment variables.
func NewEnvironment(envMap map[string]string) Environment {
return Environment(envMap)
}
// AsString retrieves a string value for the given environment key
// Panics if the key is not found
func (env Environment) AsString(key string) string {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
return value
}
// Exist will check if the key exists in the environment
func (env Environment) Exist(key string) bool {
_, ok := env[key]
return ok
}
// Exist will check if the key exists in the environment
func (env Environment) ExistAndNotEmpty(key string) bool {
value, ok := env[key]
if !ok {
return false
}
return isNotEmpty(value)
}
// NotExist will check if the key does not exist in the environment
func (env Environment) NotExist(key string) bool {
_, ok := env[key]
return !ok
}
// NotExistOrEmpty will check if the key does not exist in the environment
func (env Environment) NotExistOrEmpty(key string) bool {
value, ok := env[key]
if !ok {
return true
}
return isEmpty(value)
}
// AsStringOr retrieves a string value for the given environment key
// Returns the defaultValue if the key is not found
func (env Environment) AsStringOr(key, defaultValue string) string {
value, ok := env[key]
if !ok {
return defaultValue
}
return value
}
// AsStringSlice retrieves a string value for the given environment key and splits it by delimiter
// Panics if the key is not found
func (env Environment) AsStringSlice(key, delimiter string) []string {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
return strings.Split(value, delimiter)
}
// AsStringSliceTrim retrieves a string value for the given environment key, splits it by delimiter,
// and optionally trims each element using the specified trim characters
// Panics if the key is not found
func (env Environment) AsStringSliceTrim(key, delimiter string, trimChars string) []string {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
elements := strings.Split(value, delimiter)
for i, element := range elements {
elements[i] = strings.Trim(element, trimChars)
}
return elements
}
// AsBool retrieves a boolean value for the given environment key
// Accepts "true", "1", "yes" as true and "false", "0", "no" as false (case insensitive)
// Panics if the key is not found or the value cannot be parsed as a boolean
func (env Environment) AsBool(key string) bool {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
lowerValue := strings.ToLower(value)
switch lowerValue {
case "true", "1", "yes", "on", "enable", "enabled":
return true
case "false", "0", "no", "off", "disable", "disabled":
return false
default:
panic(fmt.Errorf("could not parse '%s' (value: '%s') as boolean", key, value))
}
}
// AsBoolOr retrieves a boolean value for the given environment key
// Accepts "true", "1", "yes" as true and "false", "0", "no" as false (case insensitive)
// Returns the defaultValue if the key is not found or the value cannot be parsed
func (env Environment) AsBoolOr(key string, defaultValue bool) bool {
value, ok := env[key]
if !ok {
return defaultValue
}
lowerValue := strings.ToLower(value)
switch lowerValue {
case "true", "1", "yes", "on", "enable", "enabled":
return true
case "false", "0", "no", "off", "disable", "disabled":
return false
default:
return defaultValue
}
}
// AsURL retrieves a URL value for the given environment key
// Panics if the key is not found or the value cannot be parsed as a URL
func (env Environment) AsURL(key string) string {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
u, err := url.ParseRequestURI(value)
if err != nil || u.Scheme == "" {
panic(fmt.Errorf("could not parse '%s' (value: '%s') as URL: %v", key, value, err))
}
return u.String()
}
// AsHostPort retrieves a host:port value for the given environment key
// Prefixes with "http://" before parsing to extract the host
// Panics if the key is not found or the value cannot be parsed
func (env Environment) AsHostPort(key string) string {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
u, err := url.ParseRequestURI("http://" + value)
if err != nil {
panic(fmt.Errorf("could not parse '%s' (value: '%s') as URL: %v", key, value, err))
}
port := 0
uPort, err := strconv.Atoi(u.Port())
if err == nil {
port = uPort
}
if port < 1 || port > 65535 {
panic(fmt.Errorf("port '%s' (value: '%s') is out of range (1-65535)", key, value))
}
return u.Host
}
// AsInt retrieves an integer value for the given environment key
// Panics if the key is not found or the value cannot be parsed as an integer
func (env Environment) AsInt(key string) int {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
intValue, err := strconv.Atoi(value)
if err != nil {
panic(fmt.Errorf("could not parse '%s' (value: '%s') as integer: %v", key, value, err))
}
return intValue
}
// AsIntOr retrieves an integer value for the given environment key
// Returns the defaultValue if the key is not found or the value cannot be parsed
func (env Environment) AsIntOr(key string, defaultValue int) int {
value, ok := env[key]
if !ok {
return defaultValue
}
intValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return intValue
}
// AsIntSlice retrieves a string value, splits it by delimiter, and converts each element to an integer
// Panics if the key is not found or any element cannot be parsed as an integer
func (env Environment) AsIntSlice(key, delimiter string) []int {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
stringElements := strings.Split(value, delimiter)
intSlice := make([]int, 0, len(stringElements))
for _, element := range stringElements {
trimmedElement := strings.TrimSpace(element)
intValue, err := strconv.Atoi(trimmedElement)
if err != nil {
panic(fmt.Errorf("on key '%s', could not parse '%s' as integer: %v", key, trimmedElement, err))
}
intSlice = append(intSlice, intValue)
}
return intSlice
}
// AsFloat retrieves an integer value for the given environment key
// Panics if the key is not found or the value cannot be parsed as an integer
func (env Environment) AsFloat(key string) float64 {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
panic(fmt.Errorf("could not parse '%s' (value: '%s') as float: %v", key, value, err))
}
return floatValue
}
// AsFloatOr retrieves an integer value for the given environment key
// Returns the defaultValue if the key is not found or the value cannot be parsed
func (env Environment) AsFloatOr(key string, defaultValue float64) float64 {
value, ok := env[key]
if !ok {
return defaultValue
}
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return floatValue
}
// AsFloatSlice retrieves a string value, splits it by delimiter, and converts each element to an integer
// Panics if the key is not found or any element cannot be parsed as an integer
func (env Environment) AsFloatSlice(key, delimiter string) []float64 {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
stringElements := strings.Split(value, delimiter)
intSlice := make([]float64, 0, len(stringElements))
for _, element := range stringElements {
trimmedElement := strings.TrimSpace(element)
floatValue, err := strconv.ParseFloat(trimmedElement, 64)
if err != nil {
panic(fmt.Errorf("on key '%s', could not parse '%s' as integer: %v", key, trimmedElement, err))
}
intSlice = append(intSlice, floatValue)
}
return intSlice
}
// AsPort retrieves a port number for the given environment key
// Validates that the port is in the valid range (1-65535)
// Panics if the key is not found, the value cannot be parsed, or is outside the valid range
func (env Environment) AsPort(key string) int {
value, ok := env[key]
if !ok {
panic(fmt.Errorf("environment variable '%s' not found", key))
}
intValue, err := strconv.Atoi(value)
if err != nil {
panic(fmt.Errorf("could not parse '%s' (value: '%s') as integer: %v", key, value, err))
}
if intValue < 1 || intValue > 65535 {
panic(fmt.Errorf("port '%s' (value: '%s') is out of range (1-65535)", key, value))
}
return intValue
}
// AsPortOr retrieves a port number for the given environment key
// Returns the defaultPort if the key is not found, the value cannot be parsed, or is outside the valid range
// Panics if the defaultPort is outside the valid range (1-65535)
func (env Environment) AsPortOr(key string, defaultPort int) int {
if defaultPort < 1 || defaultPort > 65535 {
panic(fmt.Errorf("default port '%d' is out of range (1-65535)", defaultPort))
}
value, ok := env[key]
if !ok {
return defaultPort
}
intValue, err := strconv.Atoi(value)
if err != nil {
return defaultPort
}
if intValue < 1 || intValue > 65535 {
return defaultPort
}
return intValue
}
// All returns the entire environment map
func (env Environment) All() map[string]string {
return env
}
// SortAll returns a new map with keys sorted alphabetically
func (env Environment) SortAll() map[string]string {
keys := make([]string, 0, len(env))
for k := range env {
keys = append(keys, k)
}
sort.Strings(keys)
result := make(map[string]string, len(env))
for _, k := range keys {
result[k] = env[k]
}
return result
}
// isEmpty checks if a string is empty or contains only whitespace
func isEmpty(s string) bool {
return strings.TrimSpace(s) == ""
}
// isNotEmpty checks if a string is not empty or contains only whitespace
func isNotEmpty(s string) bool {
return strings.TrimSpace(s) != ""
}
// contains checks if a string contains a substring
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}
// containsCaseInsensitive checks if a string contains a substring, ignoring case
func containsCaseInsensitive(s, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
}
// containsAny checks if a string contains any of the specified characters
func containsAny(s, chars string) bool {
return strings.ContainsAny(s, chars)
}
// hasPrefix checks if a string starts with the specified prefix
func hasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
// hasSuffix checks if a string ends with the specified suffix
func hasSuffix(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
// toLower converts a string to lowercase
func toLower(s string) string {
return strings.ToLower(s)
}
// toUpper converts a string to uppercase
func toUpper(s string) string {
return strings.ToUpper(s)
}
// trim removes the specified characters from the beginning and end of a string
func trim(s, cutset string) string {
return strings.Trim(s, cutset)
}
// trimLeft removes the specified characters from the beginning of a string
func trimLeft(s, cutset string) string {
return strings.TrimLeft(s, cutset)
}
// trimRight removes the specified characters from the end of a string
func trimRight(s, cutset string) string {
return strings.TrimRight(s, cutset)
}
// trimSpace removes whitespace from the beginning and end of a string
func trimSpace(s string) string {
return strings.TrimSpace(s)
}
// base64Encode encodes a string to base64
func base64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
// base64Decode decodes a base64 string
// Panics if the string cannot be decoded
func base64Decode(s string) string {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(fmt.Errorf("could not decode base64 string: %v", err))
}
return string(decoded)
}
// hash computes a hash of the input string using the specified algorithm
// Supported algorithms: md5, sha1, sha224, sha256, sha512
// Panics if an unsupported algorithm is specified
func hash(input string, algorithm string) string {
switch strings.ToLower(algorithm) {
case "md5":
return fmt.Sprintf("%x", md5.Sum([]byte(input)))
case "sha1":
return fmt.Sprintf("%x", sha1.Sum([]byte(input)))
case "sha224":
return fmt.Sprintf("%x", sha256.Sum224([]byte(input)))
case "sha256":
return fmt.Sprintf("%x", sha256.Sum256([]byte(input)))
case "sha512":
return fmt.Sprintf("%x", sha512.Sum512([]byte(input)))
default:
panic(fmt.Errorf("unsupported hash algorithm: %s", algorithm))
}
}
// sequence generates a slice of integers from start to end (inclusive)
// Returns nil if start > end
func sequence(start, end int) []int {
if start > end {
return nil
}
seq := make([]int, end-start+1)
for i := start; i <= end; i++ {
seq[i-start] = i
}
return seq
}
// fileExistOrDefault copies a default file to the destination path if the destination does not exist
// Preserves the file mode of the default file
// Panics if any file operation fails
func fileExistOrDefault(destination string, defaultPath string) bool {
if _, err := os.Stat(destination); os.IsNotExist(err) {
fileInfo, err := os.Stat(defaultPath)
if err != nil {
panic(fmt.Errorf("could not read file permissions for '%s': %v", defaultPath, err))
}
r, err := os.Open(defaultPath)
if err != nil {
panic(fmt.Errorf("could not open file '%s': %v", defaultPath, err))
}
defer r.Close()
w, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileInfo.Mode())
if err != nil {
panic(fmt.Errorf("could not open file '%s': %v", destination, err))
}
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
panic(fmt.Errorf("could not copy file '%s' to '%s': %v", defaultPath, destination, err))
}
}
return true
}
// GetTemplateFunctions returns a map of functions that can be used in templates
// The functions provide access to environment variables and various string utilities
func GetTemplateFunctions(env Environment) template.FuncMap {
return template.FuncMap{
// Environment accessors
"all": env.All,
"asString": env.AsString,
"asStringOr": env.AsStringOr,
"asStringSlice": env.AsStringSlice,
"asStringSliceTrim": env.AsStringSliceTrim,
"asBool": env.AsBool,
"asBoolOr": env.AsBoolOr,
"asInt": env.AsInt,
"asIntOr": env.AsIntOr,
"asIntSlice": env.AsIntSlice,
"asFloat": env.AsFloat,
"asFloatOr": env.AsFloatOr,
"asFloatSlice": env.AsFloatSlice,
"asPort": env.AsPort,
"asPortOr": env.AsPortOr,
"asURL": env.AsURL,
"asHostPort": env.AsHostPort,
"sortAll": env.SortAll,
"exist": env.Exist,
"existAndNotEmpty": env.ExistAndNotEmpty,
"notExist": env.NotExist,
"notExistOrEmpty": env.NotExistOrEmpty,
// String functions
"contains": contains,
"containsAny": containsAny,
"containsCaseInsensitive": containsCaseInsensitive,
"hasPrefix": hasPrefix,
"hasSuffix": hasSuffix,
"toLower": toLower,
"toUpper": toUpper,
"trim": trim,
"trimLeft": trimLeft,
"trimRight": trimRight,
"trimSpace": trimSpace,
"isEmpty": isEmpty,
"isNotEmpty": isNotEmpty,
// Encoding and utility functions
"base64Decode": base64Decode,
"base64Encode": base64Encode,
"hash": hash,
"sequence": sequence,
// File
"fileExistOrDefault": fileExistOrDefault,
}
}
// RenderTemplate processes the template string with the given environment.
// It returns the rendered output or an error if template parsing or execution fails.
func RenderTemplate(templateContent string, env Environment) (string, error) {
tmpl := template.New("envTemplate").Funcs(GetTemplateFunctions(env))
parsedTmpl, err := tmpl.Parse(templateContent)
if err != nil {
return "", fmt.Errorf("error parsing template: %w", err)
}
var buf bytes.Buffer
if err := parsedTmpl.Execute(&buf, env); err != nil {
return "", fmt.Errorf("error executing template: %w", err)
}
return buf.String(), nil
}