-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
42 lines (38 loc) · 905 Bytes
/
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
package main
import (
"fmt"
"os"
"strconv"
)
// returns the string value of an environment variable or an error if not set or empty
func getEnvStr(key string) (string, error) {
value := os.Getenv(key)
if value == "" {
return value, fmt.Errorf(fmt.Sprintf("environment variable %s empty", key))
}
return value, nil
}
// returns the int value of an environment variable or an error if not set or empty
func getEnvInt(key string) (int, error) {
str, err := getEnvStr(key)
if err != nil {
return 0, err
}
value, err := strconv.Atoi(str)
if err != nil {
return 0, err
}
return value, nil
}
// returns the boole value of an environment variable or an error if not set or empty
func getEnvBool(key string) (bool, error) {
str, err := getEnvStr(key)
if err != nil {
return false, err
}
value, err := strconv.ParseBool(str)
if err != nil {
return false, err
}
return value, nil
}