-
Notifications
You must be signed in to change notification settings - Fork 17
/
os.go
104 lines (96 loc) · 2.74 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
package os
import (
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// GetEnvVar retrieves the value of an environment variable having the specified
// name. If that value is the empty string, a specified default is returned
// instead.
func GetEnvVar(name, defaultValue string) string {
val := os.Getenv(name)
if val == "" {
return defaultValue
}
return val
}
// GetRequiredEnvVar retrieves the value of an environment variable having the
// specified name. If that value is the empty string, an error is returned.
func GetRequiredEnvVar(name string) (string, error) {
val := os.Getenv(name)
if val == "" {
return "", errors.Errorf(
"value not found for required environment variable %s",
name,
)
}
return val, nil
}
// GetStringSliceFromEnvVar retrieves comma-delimited values from an environment
// variable having the specified name and populates a string slice.
func GetStringSliceFromEnvVar(name string, defaultValue []string) []string {
valStr := os.Getenv(name)
if valStr == "" {
return defaultValue
}
return strings.Split(valStr, ",")
}
// GetIntFromEnvVar attempts to parse an integer from a string value retrieved
// from the specified environment variable. An error is returned if the string
// value cannot successfully be parsed as an integer.
func GetIntFromEnvVar(name string, defaultValue int) (int, error) {
valStr := os.Getenv(name)
if valStr == "" {
return defaultValue, nil
}
val, err := strconv.Atoi(valStr)
if err != nil {
return 0, errors.Errorf(
"value %q for environment variable %s was not parsable as an int",
valStr,
name,
)
}
return val, nil
}
// GetBoolFromEnvVar attempts to parse a bool from a string value retrieved from
// the specified environment variable. An error is returned if the string value
// cannot successfully be parsed as a bool.
func GetBoolFromEnvVar(name string, defaultValue bool) (bool, error) {
valStr := os.Getenv(name)
if valStr == "" {
return defaultValue, nil
}
val, err := strconv.ParseBool(valStr)
if err != nil {
return false, errors.Errorf(
"value %q for environment variable %s was not parsable as a bool",
valStr,
name,
)
}
return val, nil
}
// GetDurationFromEnvVar attempts to parse a time.Duration from a string value
// retrieved from the specified environment variable. An error is returned if
// the string value cannot successfully be parsed as a time.Duration.
func GetDurationFromEnvVar(
name string,
defaultValue time.Duration,
) (time.Duration, error) {
valStr := os.Getenv(name)
if valStr == "" {
return defaultValue, nil
}
val, err := time.ParseDuration(valStr)
if err != nil {
return 0, errors.Errorf(
"value %q for environment variable %s was not parsable as a duration",
valStr,
name,
)
}
return val, nil
}