@@ -10,17 +10,18 @@ import (
1010 "strings"
1111
1212 "github.com/a8m/envsubst"
13+ "github.com/rs/zerolog"
1314 "github.com/samber/lo"
1415 "gopkg.in/yaml.v3"
1516
1617 "github.com/pubgo/funk/assert"
17- "github.com/pubgo/funk/errors"
1818 "github.com/pubgo/funk/log"
19+ "github.com/pubgo/funk/log/logfields"
1920 "github.com/pubgo/funk/pathutil"
2021 "github.com/pubgo/funk/pretty"
2122 "github.com/pubgo/funk/recovery"
22- "github.com/pubgo/funk/result"
2323 "github.com/pubgo/funk/typex"
24+ "github.com/pubgo/funk/v2/result"
2425 "github.com/pubgo/funk/vars"
2526)
2627
@@ -48,49 +49,55 @@ func init() {
4849
4950func GetConfigData (cfgPath string ) (_ []byte , gErr error ) {
5051 var configBytes []byte
51- defer recovery . Err (& gErr , func (err error ) error {
52+ defer result . RecoveryErr (& gErr , func (err error ) error {
5253 log .Err (err ).Str ("config_path" , cfgPath ).Msgf ("config: %s" , configBytes )
5354 return err
5455 })
5556
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 )
5758 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 )
5960 return configBytes , nil
6061}
6162
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+ })
6370
64- func loadEnvConfigMap (cfgPath string ) EnvConfigMap {
6571 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 )
6773 assert .Must (yaml .Unmarshal (configBytes , & res ), "failed to unmarshal resource config" )
6874
6975 parentDir := filepath .Dir (cfgPath )
70- var envCfgMap EnvConfigMap
76+ var envSpecMap EnvSpecMap
7177 for _ , envPath := range res .PatchEnvs {
7278 envPath = filepath .Join (parentDir , envPath )
7379 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" )
7581 continue
7682 }
7783
78- pathList := listAllPath (envPath ).Expect ("failed to list envPath : %s" , envPath )
84+ pathList := listAllPath (envPath ).Expect ("failed to list env config path : %s" , envPath )
7985 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 )
8187 envConfigBytes = bytes .TrimSpace (envConfigBytes )
8288 if len (envConfigBytes ) == 0 {
8389 continue
8490 }
8591
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 )
8794 }
8895 }
89- initEnv (envCfgMap )
90- return envCfgMap
96+ initEnv (envSpecMap )
97+ return envSpecMap
9198}
9299
93- func LoadFromPath [T any ](val * T , cfgPath string ) EnvConfigMap {
100+ func LoadFromPath [T any ](val * T , cfgPath string ) EnvSpecMap {
94101 defer recovery .Exit (func (err error ) error {
95102 log .Err (err ).Str ("config_path" , cfgPath ).Msg ("failed to load config" )
96103 return err
@@ -113,7 +120,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
113120
114121 var envCfgMap = loadEnvConfigMap (cfgPath )
115122
116- configBytes := result .Of (GetConfigData (cfgPath )).Expect ("failed to handler config data" )
123+ configBytes := result .Wrap (GetConfigData (cfgPath )).Expect ("failed to handler config data" )
117124 defer recovery .Exit (func (err error ) error {
118125 log .Err (err ).
119126 Str ("config_path" , cfgPath ).
@@ -149,16 +156,15 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
149156 return lo .Uniq (resPaths )
150157 }
151158 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" )
153160
154161 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+ })
162168
163169 return cfg1
164170 }
@@ -210,7 +216,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
210216type Cfg [T any ] struct {
211217 T T
212218 P * T
213- EnvCfg * EnvConfigMap
219+ EnvCfg * EnvSpecMap
214220}
215221
216222func Load [T any ]() Cfg [T ] {
0 commit comments