@@ -9,17 +9,25 @@ import (
9
9
10
10
"github.com/a8m/envsubst"
11
11
"github.com/joho/godotenv"
12
+ "github.com/rs/zerolog"
12
13
"github.com/samber/lo"
13
14
14
15
"github.com/pubgo/funk/assert"
16
+ "github.com/pubgo/funk/log/logfields"
15
17
"github.com/pubgo/funk/pathutil"
16
18
"github.com/pubgo/funk/v2/result"
17
19
)
18
20
19
21
func Set (key , value string ) result.Error {
20
- return result .ErrOf (os .Setenv (KeyHandler (key ), value ))
22
+ return result .ErrOf (os .Setenv (keyHandler (key ), value )).Log (func (e * zerolog.Event ) {
23
+ e .Str ("key" , key )
24
+ e .Str ("value" , value )
25
+ e .Str (logfields .Msg , "env_set_error" )
26
+ })
21
27
}
22
28
29
+ func MustSet (key , value string ) { Set (key , value ).Must () }
30
+
23
31
func GetDefault (name string , defaultVal string ) string {
24
32
val := Get (name )
25
33
return lo .If (val != "" , val ).Else (defaultVal )
@@ -28,29 +36,29 @@ func GetDefault(name string, defaultVal string) string {
28
36
func Get (names ... string ) string {
29
37
var val string
30
38
GetVal (& val , names ... )
31
- return trim ( val )
39
+ return val
32
40
}
33
41
34
42
func MustGet (names ... string ) string {
35
43
var val string
36
44
GetVal (& val , names ... )
37
45
assert .If (val == "" , "env not found, names=%q" , names )
38
- return trim ( val )
46
+ return val
39
47
}
40
48
41
49
func GetVal (val * string , names ... string ) {
42
50
for _ , name := range names {
43
51
env , ok := Lookup (name )
44
52
env = trim (env )
45
53
if ok && env != "" {
46
- * val = trim ( env )
54
+ * val = env
47
55
break
48
56
}
49
57
}
50
58
}
51
59
52
60
func GetBoolVal (val * bool , names ... string ) {
53
- dt := trim ( Get (names ... ) )
61
+ dt := Get (names ... )
54
62
if dt == "" {
55
63
return
56
64
}
@@ -65,7 +73,7 @@ func GetBoolVal(val *bool, names ...string) {
65
73
}
66
74
67
75
func GetIntVal (val * int , names ... string ) {
68
- dt := trim ( Get (names ... ) )
76
+ dt := Get (names ... )
69
77
if dt == "" {
70
78
return
71
79
}
@@ -80,7 +88,7 @@ func GetIntVal(val *int, names ...string) {
80
88
}
81
89
82
90
func GetFloatVal (val * float64 , names ... string ) {
83
- dt := trim ( Get (names ... ) )
91
+ dt := Get (names ... )
84
92
if dt == "" {
85
93
return
86
94
}
@@ -94,37 +102,59 @@ func GetFloatVal(val *float64, names ...string) {
94
102
* val = v
95
103
}
96
104
97
- func Lookup (key string ) (string , bool ) {
98
- return os .LookupEnv (Key (key ))
99
- }
105
+ func Lookup (key string ) (string , bool ) { return os .LookupEnv (keyHandler (key )) }
100
106
101
107
func Delete (key string ) result.Error {
102
- return result .ErrOf (os .Unsetenv (Key (key )))
108
+ return result .ErrOf (os .Unsetenv (keyHandler (key ))).Log (func (e * zerolog.Event ) {
109
+ e .Str ("key" , key )
110
+ e .Str (logfields .Msg , "env_delete_error" )
111
+ })
103
112
}
104
113
105
114
func Expand (value string ) result.Result [string ] {
106
- return result .Wrap (envsubst .String (value ))
115
+ return result .Wrap (envsubst .String (value )).Log (func (e * zerolog.Event ) {
116
+ e .Str ("value" , value )
117
+ e .Str (logfields .Msg , "env_expand_error" )
118
+ })
107
119
}
108
120
109
121
func Map () map [string ]string {
110
122
data := make (map [string ]string , len (os .Environ ()))
111
123
for _ , env := range os .Environ () {
112
124
envs := strings .SplitN (env , "=" , 2 )
113
- data [envs [0 ]] = envs [1 ]
125
+ if len (envs ) != 2 {
126
+ continue
127
+ }
128
+
129
+ data [keyHandler (envs [0 ])] = envs [1 ]
114
130
}
115
131
return data
116
132
}
117
133
118
134
func Key (key string ) string {
119
- return KeyHandler (key )
135
+ return keyHandler (key )
120
136
}
121
137
122
138
func LoadFiles (files ... string ) (r result.Error ) {
123
139
files = lo .Filter (files , func (item string , index int ) bool { return pathutil .IsExist (item ) })
140
+ if len (files ) == 0 {
141
+ return
142
+ }
143
+
124
144
if result .Catch (& r , godotenv .Overload (files ... )) {
125
145
return
126
146
}
127
147
128
148
loadEnv ()
129
149
return
130
150
}
151
+
152
+ // Normalize a-b=>a_b, a.b=>a_b, a/b=>a_b
153
+ func Normalize (key string ) (string , bool ) {
154
+ key = trim (key )
155
+ if key == "" || strings .HasPrefix (key , "_" ) || strings .HasPrefix (key , "=" ) {
156
+ return key , false
157
+ }
158
+
159
+ return keyHandler (key ), true
160
+ }
0 commit comments