-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
env.go
33 lines (29 loc) · 682 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
package util
import (
"fmt"
"os"
"strconv"
)
func EnvInt(key string, defaultValue int32, minValue int32) int32 {
value, err := strconv.ParseInt(Env(key, fmt.Sprintf("%d", defaultValue)), 10, 32)
if err != nil || int32(value) < minValue {
return defaultValue
}
return int32(value)
}
// Env returns an environment variable or a default value if not specified.
func Env(key string, defaultValue string) string {
val := os.Getenv(key)
if len(val) == 0 {
return defaultValue
}
return val
}
// GetEnv returns an environment value if specified
func GetEnv(key string) (string, bool) {
val := os.Getenv(key)
if len(val) == 0 {
return "", false
}
return val, true
}