@@ -9,17 +9,25 @@ import (
99
1010 "github.com/a8m/envsubst"
1111 "github.com/joho/godotenv"
12+ "github.com/rs/zerolog"
1213 "github.com/samber/lo"
1314
1415 "github.com/pubgo/funk/assert"
16+ "github.com/pubgo/funk/log/logfields"
1517 "github.com/pubgo/funk/pathutil"
1618 "github.com/pubgo/funk/v2/result"
1719)
1820
1921func 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+ })
2127}
2228
29+ func MustSet (key , value string ) { Set (key , value ).Must () }
30+
2331func GetDefault (name string , defaultVal string ) string {
2432 val := Get (name )
2533 return lo .If (val != "" , val ).Else (defaultVal )
@@ -28,29 +36,29 @@ func GetDefault(name string, defaultVal string) string {
2836func Get (names ... string ) string {
2937 var val string
3038 GetVal (& val , names ... )
31- return trim ( val )
39+ return val
3240}
3341
3442func MustGet (names ... string ) string {
3543 var val string
3644 GetVal (& val , names ... )
3745 assert .If (val == "" , "env not found, names=%q" , names )
38- return trim ( val )
46+ return val
3947}
4048
4149func GetVal (val * string , names ... string ) {
4250 for _ , name := range names {
4351 env , ok := Lookup (name )
4452 env = trim (env )
4553 if ok && env != "" {
46- * val = trim ( env )
54+ * val = env
4755 break
4856 }
4957 }
5058}
5159
5260func GetBoolVal (val * bool , names ... string ) {
53- dt := trim ( Get (names ... ) )
61+ dt := Get (names ... )
5462 if dt == "" {
5563 return
5664 }
@@ -65,7 +73,7 @@ func GetBoolVal(val *bool, names ...string) {
6573}
6674
6775func GetIntVal (val * int , names ... string ) {
68- dt := trim ( Get (names ... ) )
76+ dt := Get (names ... )
6977 if dt == "" {
7078 return
7179 }
@@ -80,7 +88,7 @@ func GetIntVal(val *int, names ...string) {
8088}
8189
8290func GetFloatVal (val * float64 , names ... string ) {
83- dt := trim ( Get (names ... ) )
91+ dt := Get (names ... )
8492 if dt == "" {
8593 return
8694 }
@@ -94,37 +102,59 @@ func GetFloatVal(val *float64, names ...string) {
94102 * val = v
95103}
96104
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 )) }
100106
101107func 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+ })
103112}
104113
105114func 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+ })
107119}
108120
109121func Map () map [string ]string {
110122 data := make (map [string ]string , len (os .Environ ()))
111123 for _ , env := range os .Environ () {
112124 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 ]
114130 }
115131 return data
116132}
117133
118134func Key (key string ) string {
119- return KeyHandler (key )
135+ return keyHandler (key )
120136}
121137
122138func LoadFiles (files ... string ) (r result.Error ) {
123139 files = lo .Filter (files , func (item string , index int ) bool { return pathutil .IsExist (item ) })
140+ if len (files ) == 0 {
141+ return
142+ }
143+
124144 if result .Catch (& r , godotenv .Overload (files ... )) {
125145 return
126146 }
127147
128148 loadEnv ()
129149 return
130150}
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