1
- // Utility functions to read environment variables and set defaults.
1
+ // Package bootstrap contains utility functions to read environment variables, if a default is not provided and
2
+ // the value of an environment variable is not set then the application will panic.
2
3
package bootstrap
3
4
4
5
import (
5
- "log"
6
+ "fmt"
7
+ "log/slog"
6
8
"os"
7
9
"strconv"
8
10
)
9
11
12
+ // GetIntEnvVar reads an environment variable and returns the value as an int, if the environment variable is not set
13
+ // then the application will panic.
10
14
func GetIntEnvVar (key string ) int {
11
15
value , exists := os .LookupEnv (key )
12
16
if ! exists {
13
- log . Panicf ("missing required env var %v" , key )
17
+ panic ( fmt . Sprintf ("missing required env var %v" , key ) )
14
18
}
15
19
16
20
var err error
17
21
result , err := strconv .Atoi (value )
18
22
if err != nil {
19
- log . Panicf ("cannot parse %v, error: %v" , key , err )
23
+ panic ( fmt . Sprintf ("cannot parse %v, error: %v" , key , err ) )
20
24
}
21
25
22
- log . Printf ( "%v set to %v " , key , value )
26
+ slog . Info ( "Environment Variable Set " , key , value )
23
27
24
28
return result
25
29
}
26
30
31
+ // GetEnvVar reads an environment variable and returns the value as a string, if the environment variable is not set
32
+ // then the application will panic.
27
33
func GetEnvVar (key string ) string {
28
34
value , exists := os .LookupEnv (key )
29
35
if ! exists {
30
- log . Panicf ("missing required env var %v" , key )
36
+ panic ( fmt . Sprintf ("missing required env var %v" , key ) )
31
37
}
32
38
33
- log . Printf ( "%v set to %v " , key , value )
39
+ slog . Info ( "Environment Variable Set " , key , value )
34
40
35
41
return value
36
42
}
37
43
44
+ // GetOptionalEnvVar reads an environment variable and returns the value as a string, or the default value if the
45
+ // environment variable is not set.
38
46
func GetOptionalEnvVar (key string , def string ) string {
39
47
strValue , exists := os .LookupEnv (key )
40
48
result := def
41
49
if exists {
42
50
result = strValue
43
51
}
44
52
45
- log . Printf ( "%v set to %v " , key , result )
53
+ slog . Info ( "Environment Variable Set " , key , strValue )
46
54
47
55
return result
48
56
}
49
57
58
+ // GetOptionalBoolEnvVar reads an environment variable and returns the value as a bool, or the default value if the
59
+ // environment variable is not set. If the environment variable is set but cannot be parsed as a bool then the
60
+ // application will panic.
50
61
func GetOptionalBoolEnvVar (key string , def bool ) bool {
51
62
strValue , exists := os .LookupEnv (key )
52
63
result := def
53
64
if exists {
54
65
var err error
55
66
result , err = strconv .ParseBool (strValue )
56
67
if err != nil {
57
- log . Panicf ("cannot parse %v, error: %v" , key , err )
68
+ panic ( fmt . Sprintf ("cannot parse %v, error: %v" , key , err ) )
58
69
}
59
70
}
60
71
61
- log . Printf ( "%v set to %v " , key , result )
72
+ slog . Info ( "Environment Variable Set " , key , strValue )
62
73
63
74
return result
64
75
}
65
76
77
+ // GetBoolEnvVar reads an environment variable and returns the value as a bool, if the environment variable is not set
78
+ // then the application will panic.
66
79
func GetBoolEnvVar (key string ) bool {
67
80
value , exists := os .LookupEnv (key )
68
81
if ! exists {
69
- log . Panicf ("missing required env var %v" , key )
82
+ panic ( fmt . Sprintf ("missing required env var %v" , key ) )
70
83
}
71
84
72
85
var err error
73
86
result , err := strconv .ParseBool (value )
74
87
if err != nil {
75
- log . Panicf ("cannot parse %v, error: %v" , key , err )
88
+ panic ( fmt . Sprintf ("cannot parse %v, error: %v" , key , err ) )
76
89
}
77
90
78
- log . Printf ( "%v set to %v " , key , value )
91
+ slog . Info ( "Environment Variable Set " , key , value )
79
92
80
93
return result
81
94
}
82
95
96
+ // GetOptionalIntEnvVar reads an environment variable and returns the value as an int, or the default value if the
97
+ // environment variable is not set. If the environment variable is set but cannot be parsed as an int then the
98
+ // application will panic.
83
99
func GetOptionalIntEnvVar (key string , def int ) int {
84
100
strValue , exists := os .LookupEnv (key )
85
101
result := def
86
102
if exists {
87
103
var err error
88
104
result , err = strconv .Atoi (strValue )
89
105
if err != nil {
90
- log . Panicf ("cannot parse %v, error: %v" , key , err )
106
+ panic ( fmt . Sprintf ("cannot parse %v, error: %v" , key , err ) )
91
107
}
92
108
}
93
109
94
- log . Printf ( "%v set to %v " , key , result )
110
+ slog . Info ( "Environment Variable Set " , key , strValue )
95
111
96
112
return result
97
113
}
98
114
115
+ // GetOptionalFloatEnvVar reads an environment variable and returns the value as a float64, if the environment variable is not set
116
+ // then the application will panic. If the environment variable is set but cannot be parsed as a float64 then the
117
+ // application will panic.
99
118
func GetOptionalFloatEnvVar (key string , def float64 ) float64 {
100
119
strValue , exists := os .LookupEnv (key )
101
120
result := def
102
121
if exists {
103
122
var err error
104
123
result , err = strconv .ParseFloat (strValue , 64 )
105
124
if err != nil {
106
- log . Panicf ("cannot parse %v, error: %v" , key , err )
125
+ panic ( fmt . Sprintf ("cannot parse %v, error: %v" , key , err ) )
107
126
}
108
127
}
109
128
110
- log . Printf ( "%v set to %v " , key , result )
129
+ slog . Info ( "Environment Variable Set " , key , result )
111
130
112
131
return result
113
- }
132
+ }
0 commit comments