Core

/app/util/env.go (1.4 KB)

 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
package util

import (
"fmt"
"os"
"strconv"
"strings"
"time"
)

func ReplaceEnvVars(s string, logger Logger) string {
sIdx := strings.Index(s, "${")
if sIdx > -1 {
eIdx := strings.Index(s[sIdx:], "}")
if eIdx > -1 {
orig := s[sIdx : sIdx+eIdx+1]

n := orig[2 : len(orig)-1]
d := ""

dIdx := strings.Index(orig, "|")
if dIdx > -1 {
n = orig[2:dIdx]
d = orig[dIdx+1 : len(orig)-1]
}

o := GetEnv(n)
if o == "" {
o = d
}
return ReplaceEnvVars(strings.Replace(s, orig, o, 1), logger)
}
}
return s
}

func GetEnv(name string, defaultValue ...string) string {
if v := os.Getenv(name); v != "" {
return v
}
if l := strings.ToLower(name); l != name {
if v := os.Getenv(l); v != "" {
return v
}
}
if u := strings.ToUpper(name); u != name {
if v := os.Getenv(u); v != "" {
return v
}
}
return strings.Join(defaultValue, "")
}

func GetEnvBool(name string, defaultValue bool) bool {
return GetEnv(name, fmt.Sprint(defaultValue)) == BoolTrue
}

func GetEnvInt(name string, defaultValue int) int {
v := GetEnv(name, "")
i, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return defaultValue
}
return int(i)
}

func GetEnvDuration(name string, defaultValue time.Duration) time.Duration {
v := GetEnv(name, "")
ret, err := time.ParseDuration(v)
if err != nil {
return defaultValue
}
return ret
}