-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
51 lines (46 loc) · 1.53 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
package helpers
import (
"log"
"os"
"strconv"
"strings"
)
// GetEnv returns the value of an environment variable or
// a default value if the environment variable is not set.
// The value is trimmed of leading and trailing whitespace.
//
// Parameters:
// - env (string): The name of the environment variable.
// - defaultValue (string): The default value to return if the environment variable is not set.
//
// Returns:
// - environment (string): The value of the environment variable or the default value.
func GetEnv(env, defaultValue string) string {
environment := strings.TrimSpace(os.Getenv(env))
if environment == "" {
return defaultValue
}
return environment
}
// GetEnvAsInt returns the value of an environment variable as an integer or
// a default value if the environment variable is not set or is not a valid integer.
// The value is trimmed of leading and trailing whitespace.
//
// Parameters:
// - env (string): The name of the environment variable.
// - defaultValue (int): The default value to return if the environment variable is not set or is not a valid integer.
//
// Returns:
// - value (int): The value of the environment variable as an integer or the default value.
func GetEnvAsInt(env string, defaultValue int) int {
environment := strings.TrimSpace(os.Getenv(env))
if environment == "" {
return defaultValue
}
value, err := strconv.Atoi(environment)
if err != nil {
log.Printf("Warning: %s is not a valid integer. Using default value: %d", env, defaultValue)
return defaultValue
}
return value
}