-
Notifications
You must be signed in to change notification settings - Fork 0
/
getenv.go
64 lines (49 loc) · 1.75 KB
/
getenv.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
// Copyright 2024 Factorial GmbH. All rights reserved.
package main
import (
"log/slog"
"os"
"strconv"
)
// GetEnvString gets the environment variable for a key and if that env-var hasn't been set it returns the default value
func GetEnvString(key string, defaultVal string) string {
value := os.Getenv(key)
if len(value) == 0 {
value = defaultVal
}
slog.Debug("Set Environment ", "key", key, "value", value)
return value
}
// GetEnvBool gets the environment variable for a key and if that env-var hasn't been set it returns the default value
func GetEnvBool(key string, defaultVal bool) bool {
envvalue := os.Getenv(key)
value, err := strconv.ParseBool(envvalue)
if len(envvalue) == 0 || err != nil {
value := defaultVal
return value
}
slog.Debug("Set Environment ", "key", key, "value", value)
return value
}
// GetEnvInt gets the environment variable for a key and if that env-var hasn't been set it returns the default value. This function is equivalent to ParseInt(s, 10, 0) to convert env-vars to type int
func GetEnvInt(key string, defaultVal int) int {
envvalue := os.Getenv(key)
value, err := strconv.Atoi(envvalue)
if len(envvalue) == 0 || err != nil {
value := defaultVal
return value
}
slog.Debug("Set Environment ", "key", key, "value", value)
return value
}
// GetEnvFloat gets the environment variable for a key and if that env-var hasn't been set it returns the default value. This function uses bitSize of 64 to convert string to float64.
func GetEnvFloat(key string, defaultVal float64) float64 {
envvalue := os.Getenv(key)
value, err := strconv.ParseFloat(envvalue, 64)
if len(envvalue) == 0 || err != nil {
value := defaultVal
return value
}
slog.Debug("Set Environment ", "key", key, "value", value)
return value
}