-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.go
114 lines (102 loc) · 4.31 KB
/
os.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
package utils
import (
"fmt"
"os"
"strconv"
)
// MustGetenv attempts to lookup and return the value associated with the specified environment variable identifier, panic'ing if no value is associated with that identifier
func MustGetenv(env string) string {
value, ok := os.LookupEnv(env)
if !ok {
panic(fmt.Sprintf("Failed to find environment variable with identifier: %s\n", env))
}
return value
}
// TransitiveMustGetenv will (if a transitive dependnecy such as the value of another environment is set)
//
// attempt to lookup the specified environment variable and return its string panicing if not provided
func TransitiveMustGetenv(env string, active bool) string {
if !active {
return ""
}
return MustGetenv(env)
}
// MustGetenvInt attempts to lookup and return the value associated with the specified environment variable identifier and cast it to an int,
// panic'ing if no value is associated with that identifier or it cannot be cast
func MustGetenvInt(env string) int {
value := MustGetenv(env)
intVal, err := strconv.Atoi(value)
if err != nil {
panic(fmt.Sprintf("Provided Environment variable for: %v can not be cast to int: %s\n", env, value))
}
return intVal
}
// TransitiveMustGetenvInt will (if a transitive dependnecy such as the value of another environment is set)
// attempt to lookup the specified environment variable parsed as an int and return its value panicing if not provided
func TransitiveMustGetenvInt(env string, active bool) int {
if !active {
return 0
}
return MustGetenvInt(env)
}
// MustGetenvIntNonZero attempts to lookup and return the value associated with the specified environment variable identifier and cast it to an int,
// panic'ing if no value is associated with that identifier, if it cannot be cast, or if once cast equals zero
func MustGetenvIntNonZero(env string) int {
value := MustGetenvInt(env)
if value == 0 {
panic(fmt.Sprintf("Provided Environment variable equals 0 when explicitly not allowed: %s\n", env))
}
return value
}
// MustGetenvFloat attempts to lookup and return the value associated with the specified environment variable identifier and cast it to a float,
// panic'ing if no value is associated with that identifier or it cannot be cast
func MustGetenvFloat(env string) float64 {
value := MustGetenv(env)
floatVal, err := strconv.ParseFloat(value, 64)
if err != nil {
panic(fmt.Sprintf("Provided Environment variable for: %v can not be cast to float: %s\n", env, value))
}
return floatVal
}
// TransitiveMustGetenvFloat will (if a transitive dependnecy such as the value of another environment is set)
// attempt to lookup the specified environment variable parsed as a float and return its value panicing if not provided
func TransitiveMustGetenvFloat(env string, active bool) float64 {
if !active {
return 0
}
return MustGetenvFloat(env)
}
// MustGetenvFloatNonZero attempts to lookup and return the value associated with the specified environment variable identifier and cast it to a float,
// panic'ing if no value is associated with that identifier, if it cannot be cast, or if once cast equals zero
func MustGetenvFloatNonZero(env string) float64 {
value := MustGetenvFloat(env)
if value == 0 {
panic(fmt.Sprintf("Provided Environment variable equals 0 when explicitly not allowed: %s\n", env))
}
return value
}
// MustGetenvBool attempts to lookup and return the value associated with the specified environment variable identifier and cast it to a bool,
// panic'ing if no value is associated with that identifier, if it cannot be cast
func MustGetenvBool(env string) bool {
value := MustGetenv(env)
boolVal, err := strconv.ParseBool(value)
if err != nil {
panic(fmt.Sprintf("Provided Environment variable for: %v could not be parsed to a bool: %s\n", env, value))
}
return boolVal
}
// TransitiveMustGetenvBool will (if a transitive dependnecy such as the value of another environment is set)
// attempt to lookup the specified environment variable parsed as a bool and return its value panicing if not provided
func TransitiveMustGetenvBool(env string, active bool) bool {
if !active {
return false
}
return MustGetenvBool(env)
}
// EnvOrDefault fetches an environment variable value, or if not set returns the fallback value
func EnvOrDefault(key string, fallback string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return fallback
}