@@ -10,17 +10,18 @@ import (
10
10
"strings"
11
11
12
12
"github.com/a8m/envsubst"
13
+ "github.com/rs/zerolog"
13
14
"github.com/samber/lo"
14
15
"gopkg.in/yaml.v3"
15
16
16
17
"github.com/pubgo/funk/assert"
17
- "github.com/pubgo/funk/errors"
18
18
"github.com/pubgo/funk/log"
19
+ "github.com/pubgo/funk/log/logfields"
19
20
"github.com/pubgo/funk/pathutil"
20
21
"github.com/pubgo/funk/pretty"
21
22
"github.com/pubgo/funk/recovery"
22
- "github.com/pubgo/funk/result"
23
23
"github.com/pubgo/funk/typex"
24
+ "github.com/pubgo/funk/v2/result"
24
25
"github.com/pubgo/funk/vars"
25
26
)
26
27
@@ -48,49 +49,55 @@ func init() {
48
49
49
50
func GetConfigData (cfgPath string ) (_ []byte , gErr error ) {
50
51
var configBytes []byte
51
- defer recovery . Err (& gErr , func (err error ) error {
52
+ defer result . RecoveryErr (& gErr , func (err error ) error {
52
53
log .Err (err ).Str ("config_path" , cfgPath ).Msgf ("config: %s" , configBytes )
53
54
return err
54
55
})
55
56
56
- configBytes = result .Of (os .ReadFile (cfgPath )).Expect ("failed to read config data: %s" , cfgPath )
57
+ configBytes = result .Wrap (os .ReadFile (cfgPath )).Expect ("failed to read config data: %s" , cfgPath )
57
58
configBytes = cfgFormat (configBytes , & config {workDir : filepath .Dir (cfgPath )})
58
- configBytes = result .Of (envsubst .Bytes (configBytes )).Expect ("failed to handler config env data: %s" , cfgPath )
59
+ configBytes = result .Wrap (envsubst .Bytes (configBytes )).Expect ("failed to handler config env data: %s" , cfgPath )
59
60
return configBytes , nil
60
61
}
61
62
62
- func LoadEnvConfigMap (cfgPath string ) EnvConfigMap { return loadEnvConfigMap (cfgPath ) }
63
+ func LoadEnvConfigMap (cfgPath string ) EnvSpecMap { return loadEnvConfigMap (cfgPath ) }
64
+
65
+ func loadEnvConfigMap (cfgPath string ) EnvSpecMap {
66
+ defer recovery .Exit (func (err error ) error {
67
+ log .Err (err ).Str ("path" , cfgPath ).Msg ("load env config map error" )
68
+ return err
69
+ })
63
70
64
- func loadEnvConfigMap (cfgPath string ) EnvConfigMap {
65
71
var res Resources
66
- configBytes := result .Of (os .ReadFile (cfgPath )).Expect ("failed to read config data: %s" , cfgPath )
72
+ configBytes := result .Wrap (os .ReadFile (cfgPath )).Expect ("failed to read config data: %s" , cfgPath )
67
73
assert .Must (yaml .Unmarshal (configBytes , & res ), "failed to unmarshal resource config" )
68
74
69
75
parentDir := filepath .Dir (cfgPath )
70
- var envCfgMap EnvConfigMap
76
+ var envSpecMap EnvSpecMap
71
77
for _ , envPath := range res .PatchEnvs {
72
78
envPath = filepath .Join (parentDir , envPath )
73
79
if pathutil .IsNotExist (envPath ) {
74
- log .Warn ().Str ("env_path" , envPath ).Msg ("env config cfgPath not found" )
80
+ log .Warn ().Str ("env_path" , envPath ).Msg ("env config path not found" )
75
81
continue
76
82
}
77
83
78
- pathList := listAllPath (envPath ).Expect ("failed to list envPath : %s" , envPath )
84
+ pathList := listAllPath (envPath ).Expect ("failed to list env config path : %s" , envPath )
79
85
for _ , p := range pathList {
80
- envConfigBytes := result .Of (os .ReadFile (p )).Expect ("failed to handler env config data, path=%s" , p )
86
+ envConfigBytes := result .Wrap (os .ReadFile (p )).Expect ("failed to handler env config data, path=%s" , p )
81
87
envConfigBytes = bytes .TrimSpace (envConfigBytes )
82
88
if len (envConfigBytes ) == 0 {
83
89
continue
84
90
}
85
91
86
- assert .MustF (yaml .Unmarshal (envConfigBytes , & envCfgMap ), "failed to unmarshal env config, data=%s path=%s" , envConfigBytes , p )
92
+ envConfigBytes = result .Wrap (envsubst .Bytes (envConfigBytes )).Expect ("failed to handler config env data: %s" , envConfigBytes )
93
+ assert .MustF (yaml .Unmarshal (envConfigBytes , & envSpecMap ), "failed to unmarshal env config, data=%s path=%s" , envConfigBytes , p )
87
94
}
88
95
}
89
- initEnv (envCfgMap )
90
- return envCfgMap
96
+ initEnv (envSpecMap )
97
+ return envSpecMap
91
98
}
92
99
93
- func LoadFromPath [T any ](val * T , cfgPath string ) EnvConfigMap {
100
+ func LoadFromPath [T any ](val * T , cfgPath string ) EnvSpecMap {
94
101
defer recovery .Exit (func (err error ) error {
95
102
log .Err (err ).Str ("config_path" , cfgPath ).Msg ("failed to load config" )
96
103
return err
@@ -113,7 +120,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
113
120
114
121
var envCfgMap = loadEnvConfigMap (cfgPath )
115
122
116
- configBytes := result .Of (GetConfigData (cfgPath )).Expect ("failed to handler config data" )
123
+ configBytes := result .Wrap (GetConfigData (cfgPath )).Expect ("failed to handler config data" )
117
124
defer recovery .Exit (func (err error ) error {
118
125
log .Err (err ).
119
126
Str ("config_path" , cfgPath ).
@@ -149,16 +156,15 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
149
156
return lo .Uniq (resPaths )
150
157
}
151
158
getCfg := func (resPath string ) T {
152
- resBytes := result .Of (GetConfigData (resPath )).Expect ("failed to handler config data" )
159
+ resBytes := result .Wrap (GetConfigData (resPath )).Expect ("failed to handler config data" )
153
160
154
161
var cfg1 T
155
- result.Err [any ](yaml .Unmarshal (resBytes , & cfg1 )).
156
- Unwrap (func (err error ) error {
157
- fmt .Println ("res_path" , resPath )
158
- fmt .Println ("config_data" , string (resBytes ))
159
- assert .Exit (os .WriteFile (resPath + ".err.yml" , resBytes , 0666 ))
160
- return errors .Wrap (err , "failed to unmarshal config" )
161
- })
162
+ result .ErrOf (yaml .Unmarshal (resBytes , & cfg1 )).Must (func (e * zerolog.Event ) {
163
+ fmt .Println ("res_path" , resPath )
164
+ fmt .Println ("config_data" , string (resBytes ))
165
+ assert .Exit (os .WriteFile (resPath + ".err.yml" , resBytes , 0666 ))
166
+ e .Str (logfields .Msg , "failed to unmarshal config" )
167
+ })
162
168
163
169
return cfg1
164
170
}
@@ -210,7 +216,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
210
216
type Cfg [T any ] struct {
211
217
T T
212
218
P * T
213
- EnvCfg * EnvConfigMap
219
+ EnvCfg * EnvSpecMap
214
220
}
215
221
216
222
func Load [T any ]() Cfg [T ] {
0 commit comments