-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
49 lines (42 loc) · 1.07 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
package osenv
import (
"fmt"
"os"
"runtime"
"strconv"
)
func Get(key string, msg string, defaultValue string) string {
return getWithCallerSkip(key, msg, defaultValue, 3)
}
func GetBool(key string, msg string, defaultValue bool) (r bool) {
sv := getWithCallerSkip(key, msg, fmt.Sprint(defaultValue), 3)
if sv == "" {
return defaultValue
}
r, _ = strconv.ParseBool(sv)
return
}
func GetInt64(key string, msg string, defaultValue int64) (r int64) {
sv := getWithCallerSkip(key, msg, fmt.Sprint(defaultValue), 3)
if sv == "" {
return defaultValue
}
r, _ = strconv.ParseInt(sv, 10, 64)
return
}
func getWithCallerSkip(key string, msg string, defaultValue string, callerSkip int) string {
defer func() {
pc, _, _, _ := runtime.Caller(callerSkip)
funcName := runtime.FuncForPC(pc).Name()
defaultMessage := ""
if defaultValue != "" {
defaultMessage = fmt.Sprintf(" (default: %s)", defaultValue)
}
fmt.Printf("%s: %s,%s, at: %s\n", key, msg, defaultMessage, funcName)
}()
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}