@@ -3,12 +3,14 @@ package config
33import (
44 "fmt"
55 "io"
6+ "io/fs"
67 "path/filepath"
78 "reflect"
89 "strings"
910 "sync"
1011
1112 "github.com/mitchellh/mapstructure"
13+ "github.com/pubgo/funk"
1214 "github.com/pubgo/x/iox"
1315 "github.com/pubgo/x/pathutil"
1416 "github.com/pubgo/xerror"
@@ -25,10 +27,11 @@ import (
2527)
2628
2729var (
28- CfgType = "yaml"
29- CfgName = "config"
30- CfgDir = env .Get ("cfg_dir" , "app_cfg_dir" )
31- CfgPath = filepath .Join ("configs" , "config" , "config.yaml" )
30+ CfgType = "yaml"
31+ CfgName = "config"
32+ CfgDir = env .Get ("app_cfg_home" , "project_cfg_home" , consts .EnvCfgHome )
33+ CfgPath = filepath .Join ("configs" , "config" , "config.yaml" )
34+ EnvPrefix = utils .FirstNotEmpty (env .Get ("cfg_env_prefix" , "app_env_prefix" , "project_env_prefix" , consts .EnvCfgPrefix ), "lava" )
3235)
3336
3437// Init 处理所有的配置,环境变量和flag
3740// flag可以指定配置文件位置
3841// 始化配置文件
3942func newCfg () * configImpl {
40- defer xerror .RecoverAndExit ()
41-
4243 var t = & configImpl {v : viper .New ()}
4344 // 配置处理
4445 v := t .v
@@ -47,40 +48,52 @@ func newCfg() *configImpl {
4748 v .SetConfigType (CfgType )
4849 v .SetConfigName (CfgName )
4950 v .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" , "-" , "_" ))
50- prefix := utils .FirstNotEmpty (env .Get ("cfg_env_prefix" , "env_prefix" ), "lava" )
51- v .SetEnvPrefix (prefix )
51+ v .SetEnvPrefix (strings .ToUpper (EnvPrefix ))
5252 v .AutomaticEnv ()
5353
5454 // 初始化框架, 加载环境变量, 加载本地配置
5555 // 初始化完毕所有的配置以及外部配置以及相关的参数和变量
5656 // 然后获取配置了
5757 xerror .PanicF (t .initCfg (v ), "config file load error" )
58- t .LoadEnv ()
59-
6058 CfgPath = v .ConfigFileUsed ()
6159 CfgDir = filepath .Dir (filepath .Dir (v .ConfigFileUsed ()))
62- xerror .Panic (env .Set (consts .EnvCfgDir , CfgDir ))
60+ xerror .Panic (env .Set (consts .EnvCfgHome , CfgDir ))
61+ t .LoadPath (CfgPath )
6362
6463 // 加载自定义配置
65- //t.LoadPath(customCfgPath(app.Mode.String()))
66- //t.LoadPath(customCfgPath(app.Project))
64+ t .loadCustomCfg ()
6765 return t
6866}
6967
70- func customCfgPath (name string ) string {
71- return filepath .Join (filepath .Dir (CfgPath ), fmt .Sprintf ("%s.%s.%s" , CfgName , name , CfgType ))
72- }
73-
7468var _ Config = (* configImpl )(nil )
7569
7670type configImpl struct {
7771 rw sync.RWMutex
7872 v * viper.Viper
7973}
8074
81- func (t * configImpl ) LoadEnv (names ... string ) {
82- names = append (names , "cfg_env_prefix" , "env_prefix" )
83- loadEnvFromPrefix (env .Get (names ... ), t .v )
75+ func (t * configImpl ) loadCustomCfg () {
76+ var path = filepath .Dir (t .v .ConfigFileUsed ())
77+ funk .MustMsg (filepath .Walk (path , func (path string , info fs.FileInfo , err error ) error {
78+ if err != nil {
79+ return err
80+ }
81+
82+ if info .IsDir () {
83+ return nil
84+ }
85+
86+ if ! strings .HasSuffix (info .Name (), "." + CfgType ) {
87+ return nil
88+ }
89+
90+ if info .Name () == CfgName + "." + CfgType {
91+ return nil
92+ }
93+
94+ t .LoadPath (path )
95+ return nil
96+ }), "walk path failed, path=%s" , path )
8497}
8598
8699func (t * configImpl ) All () map [string ]interface {} {
@@ -205,17 +218,17 @@ func (t *configImpl) initWithDir(v *viper.Viper) bool {
205218 return false
206219 }
207220
208- xerror .Assert (pathutil .IsNotExist (CfgDir ), "config dir not found, path:%s" , CfgPath )
221+ funk .Assert (pathutil .IsNotExist (CfgDir ), "config dir not found, path:%s" , CfgPath )
209222 v .AddConfigPath (filepath .Join (CfgDir , CfgName ))
210- xerror . PanicF (v .ReadInConfig (), "config load error, dir:%s" , CfgDir )
223+ funk . MustMsg (v .ReadInConfig (), "config load error, dir:%s" , CfgDir )
211224
212225 return true
213226}
214227
215228func (t * configImpl ) initCfg (v * viper.Viper ) (err error ) {
216229 defer xerror .RecoverErr (& err )
217230
218- // 指定配置文件目录
231+ // 指定配置目录
219232 if t .initWithDir (v ) {
220233 return
221234 }
@@ -226,7 +239,7 @@ func (t *configImpl) initCfg(v *viper.Viper) (err error) {
226239 }
227240
228241 var pathList = strMap (getPathList (), func (str string ) string { return filepath .Join (str , "configs" , CfgName ) })
229- xerror .Assert (len (pathList ) == 0 , "pathList is zero " )
242+ xerror .Assert (len (pathList ) == 0 , "config path not found " )
230243
231244 for i := range pathList {
232245 if t .addConfigPath (pathList [i ]) {
@@ -243,6 +256,8 @@ func (t *configImpl) LoadPath(path string) {
243256 return
244257 }
245258
259+ fmt .Printf ("load config %s\n " , path )
260+
246261 tmp , err := fasttemplate .NewTemplate (xerror .PanicStr (iox .ReadText (path )), "{{" , "}}" )
247262 xerror .Panic (err , "unexpected error when parsing template" )
248263
@@ -256,5 +271,4 @@ func (t *configImpl) LoadPath(path string) {
256271
257272 return w .Write ([]byte (t .v .GetString (tag )))
258273 }))))
259- t .LoadEnv ()
260274}
0 commit comments