-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getters.go
170 lines (150 loc) · 4.96 KB
/
getters.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
package env
import (
"fmt"
"strconv"
"strings"
)
// Get returns the value of an environment variable.
func Get(key string) (string, error) {
if value, ok := Lookup(key); ok {
return value, nil
}
return "", fmt.Errorf("environment variable %s not set", key)
}
// GetWithFallback returns the value of an environment variable or a fallback
// value if the environment variable is not set.
func GetWithFallback(key string, fallback string) string {
if value, err := Get(key); err == nil {
return value
}
return fallback
}
// GetBool returns the value of an environment variable as a boolean.
func GetBool(key string) (bool, error) {
value, err := Get(key)
if err != nil {
return false, err
}
return parseBool(value)
}
// GetBoolWithFallback returns the value of an environment variable as a boolean
// or a fallback value if the environment variable is not set.
func GetBoolWithFallback(key string, fallback bool) (bool, error) {
if value, err := GetBool(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetInt returns the value of an environment variable as an integer.
func GetInt(key string) (int, error) {
value, err := Get(key)
if err != nil {
return 0, err
}
return strconv.Atoi(value)
}
// GetIntWithFallback returns the value of an environment variable as an integer
// or a fallback value if the environment variable is not set or invalid.
func GetIntWithFallback(key string, fallback int) (int, error) {
if value, err := GetInt(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetFloat returns the value of an environment variable as a float.
func GetFloat(key string) (float64, error) {
value, err := Get(key)
if err != nil {
return 0, err
}
return strconv.ParseFloat(value, 64)
}
// GetFloatWithFallback returns the value of an environment variable as a float
// or a fallback value if the environment variable is not set or invalid.
func GetFloatWithFallback(key string, fallback float64) (float64, error) {
if value, err := GetFloat(key); err == nil {
return value, nil
}
return fallback, nil
}
// -- Slice Getters --
// GetStringSlice returns the value of a comma-separated environment variable as a slice of strings.
func GetStringSlice(key string) ([]string, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
return strings.Split(value, ","), nil
}
// GetStringSliceWithFallback returns the value of a comma-separated environment variable as a slice
// of strings or a fallback value if the environment variable is not set.
func GetStringSliceWithFallback(key string, fallback []string) ([]string, error) {
if value, err := GetStringSlice(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetBoolSlice returns the value of a comma-separated environment variable as a slice of bools.
func GetBoolSlice(key string) ([]bool, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
return parseBoolSlice(value)
}
// GetBoolSliceWithFallback returns the value of a comma-separated environment variable as a slice
// of bools or a fallback value if the environment variable is not set.
func GetBoolSliceWithFallback(key string, fallback []bool) ([]bool, error) {
if value, err := GetBoolSlice(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetIntSlice returns the value of a comma-separated environment variable as a slice of ints.
func GetIntSlice(key string) ([]int, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
return parseIntSlice(value)
}
// GetIntSliceWithFallback returns the value of a comma-separated environment variable as a slice
// of ints or a fallback value if the environment variable is not set.
func GetIntSliceWithFallback(key string, fallback []int) ([]int, error) {
if value, err := GetIntSlice(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetUintSlice returns the value of a comma-separated environment variable as a slice of uints.
func GetUintSlice(key string) ([]uint, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
return parseUintSlice(value)
}
// GetUintSliceWithFallback returns the value of a comma-separated environment variable as a slice
// of uints or a fallback value if the environment variable is not set.
func GetUintSliceWithFallback(key string, fallback []uint) ([]uint, error) {
if value, err := GetUintSlice(key); err == nil {
return value, nil
}
return fallback, nil
}
// GetFloatSlice returns the value of a comma-separated environment variable as a slice of floats.
func GetFloatSlice(key string) ([]float64, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
return parseFloatSlice(value)
}
// GetFloatSliceWithFallback returns the value of a comma-separated environment variable as a slice
// of floats or a fallback value if the environment variable is not set.
func GetFloatSliceWithFallback(key string, fallback []float64) ([]float64, error) {
if value, err := GetFloatSlice(key); err == nil {
return value, nil
}
return fallback, nil
}