-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_environment.go
45 lines (41 loc) · 1.23 KB
/
check_environment.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
package env
import (
"errors"
"log/slog"
"os"
)
// checkEnvironment checks if environment variables are present
func checkEnvironment(vars ...string) (errs []error) {
// iterate over necessary os vars
for _, v := range vars {
if len(os.Getenv(v)) == 0 {
msg := "Environment variable " + v + " is not present."
errs = append(errs, errors.New(msg))
}
}
return
}
// CheckRequiredEnvironmentVariables checks if environment variables are present
// panics if one is missing
func CheckRequiredEnvironmentVariables(requiredEnvironmentVariables ...string) {
errs := checkEnvironment(requiredEnvironmentVariables...)
// if necessary env variable: panic
if len(errs) != 0 {
for _, e := range errs {
slog.With("err", e).Error("environment variable is missing. Please add the variable to startup the system")
}
panic("Missing required environment variables")
}
return
}
// CheckOptionalEnvironmentVariables checks if
func CheckOptionalEnvironmentVariables(possibleEnvironmentVariables ...string) {
errs := checkEnvironment(possibleEnvironmentVariables...)
// if necessary env variable: panic
if len(errs) != 0 {
for _, e := range errs {
slog.With("err", e).Warn("environment variable can be set but is missing")
}
}
return
}