forked from darkhelmet/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
130 lines (118 loc) · 2.87 KB
/
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
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
package env
import (
"fmt"
"os"
"strconv"
)
// Get a string from the environment,
// panicking if it's not there
func String(key string) string {
val := os.Getenv(key)
if val == "" {
panic(fmt.Errorf("env: Environment variable %s doesn't exist", key))
}
return val
}
// Get a string from the environment,
// returning the default if it's not present
func StringDefault(key, def string) (val string) {
defer func() {
if recover() != nil {
val = def
}
}()
val = String(key)
return
}
// Get a string from the environment,
// returning the result of the default function if it's not present
func StringDefaultF(key string, def func() string) (val string) {
defer func() {
if recover() != nil {
val = def()
}
}()
val = String(key)
return
}
// Get an int from the environment,
// panicking if it's not present or doesn't parse properly
func Int(key string) int {
i, err := strconv.ParseInt(String(key), 10, 32)
if err != nil {
panic(fmt.Errorf("env: failed parsing int: %s", err))
}
return int(i)
}
// Get an int from the environment,
// returning the default if it's not present or doesn't parse properly
func IntDefault(key string, def int) (val int) {
defer func() {
if recover() != nil {
val = def
}
}()
val = Int(key)
return
}
// Get an int from the environment,
// returning the result of the default function if it's not present or doesn't parse properly
func IntDefaultF(key string, def func() int) (val int) {
defer func() {
if recover() != nil {
val = def()
}
}()
val = Int(key)
return
}
// Get a float from the environment,
// panicking if it's not present or doesn't parse properly
func Float(key string) float64 {
val, err := strconv.ParseFloat(String(key), 64)
if err != nil {
panic(fmt.Errorf("env: failed parsing float: %s", err))
}
return val
}
// Get a float from the environment,
// returning the default if it's not present or doesn't parse properly
func FloatDefault(key string, def float64) (val float64) {
defer func() {
if recover() != nil {
val = def
}
}()
val = Float(key)
return
}
// Get a float from the environment,
// returning the result of the default function if it's not present or doesn't parse properly
func FloatDefaultF(key string, def func() float64) (val float64) {
defer func() {
if recover() != nil {
val = def()
}
}()
val = Float(key)
return
}
// Get a boolean from the environment,
// panicking if it's not present or doesn't parse properly
func Bool(key string) bool {
val, err := strconv.ParseBool(String(key))
if err != nil {
panic(fmt.Errorf("env: failed parsing boolean: %s", err))
}
return val
}
// Get a boolean environment variable or default
// returning the default if it's not present or doesn't parse
func BoolDefault(key string, def bool) bool {
sVal := StringDefault(key, strconv.FormatBool(def))
val, err := strconv.ParseBool(sVal)
if err != nil {
return val
}
return def
}