-
Notifications
You must be signed in to change notification settings - Fork 182
/
envutil.go
50 lines (42 loc) · 1.14 KB
/
envutil.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
// Package envutil provide some commonly ENV util functions.
package envutil
import (
"os"
"github.com/gookit/goutil/internal/comfunc"
)
// ValueGetter Env value provider func.
//
// TIPS: you can custom provide data.
var ValueGetter = os.Getenv
// VarReplace replaces ${var} or $var in the string according to the values.
//
// is alias of the os.ExpandEnv()
func VarReplace(s string) string { return os.ExpandEnv(s) }
// VarParse alias of the ParseValue
func VarParse(val string) string {
return comfunc.ParseEnvVar(val, ValueGetter)
}
// ParseEnvValue alias of the ParseValue
func ParseEnvValue(val string) string {
return comfunc.ParseEnvVar(val, ValueGetter)
}
// ParseValue parse ENV var value from input string, support default value.
//
// Format:
//
// ${var_name} Only var name
// ${var_name | default} With default value
//
// Usage:
//
// envutil.ParseValue("${ APP_NAME }")
// envutil.ParseValue("${ APP_ENV | dev }")
func ParseValue(val string) (newVal string) {
return comfunc.ParseEnvVar(val, ValueGetter)
}
// SetEnvs to os
func SetEnvs(mp map[string]string) {
for key, value := range mp {
_ = os.Setenv(key, value)
}
}