diff --git a/go.mod b/go.mod index e072af5..9be3978 100644 --- a/go.mod +++ b/go.mod @@ -2,17 +2,20 @@ module git.zhangdeman.cn/zhangdeman/config go 1.23.2 +require ( + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc + github.com/caarlos0/env/v9 v9.0.0 + github.com/jessevdk/go-flags v1.6.1 + github.com/joho/godotenv v1.5.1 +) + require ( git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 // indirect - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e // indirect git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect github.com/BurntSushi/toml v1.4.0 // indirect - github.com/caarlos0/env/v9 v9.0.0 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/jessevdk/go-flags v1.6.1 // indirect - github.com/joho/godotenv v1.5.1 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect @@ -27,7 +30,7 @@ require ( go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect - golang.org/x/sys v0.26.0 // indirect + golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.19.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 339ec8c..74f4d70 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e h1:A045F67AMSqFKGD9kk2uLa+6c/zpmW8vjjSRmSsdjPs= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= @@ -50,6 +52,8 @@ golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBn golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/init.go b/init.go index 145c5bd..3b4f1df 100644 --- a/init.go +++ b/init.go @@ -13,7 +13,6 @@ import ( "github.com/caarlos0/env/v9" "github.com/jessevdk/go-flags" "github.com/joho/godotenv" - "path/filepath" ) // Init 初始化配置 @@ -28,43 +27,101 @@ import ( // - envReceiver 环境变量配置结果 // - fileReceiver 文件配置结果 // - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件 -// - configFilePath 配置文件目录 -// - configFileNameList 配置文件列表 -func Init(configReceiver any, cliReceiver any, envReceiver any, fileReceiver any, envFileList []string, configFilePath string, configFileNameList []string) error { +// - configFileList 配置文件列表 +func Init(configReceiver any, cliReceiver any, envReceiver any, fileReceiver any, envFileList []string, configFileList []*string) error { var ( err error ) - if len(envFileList) == 0 { - envFileList = []string{".env"} - } - - if nil != envFileList { - // 加载环境变量 - if err = godotenv.Load(envFileList...); nil != err { - return err - } - if err = env.Parse(envReceiver); nil != err { - return errors.New("env param parse fail : " + err.Error()) - } - } - - if nil != fileReceiver && len(configFileNameList) > 0 { - // 读取配置文件内容 - for _, itemConfigFileName := range configFileNameList { - configFile := filepath.Join(configFilePath, itemConfigFileName) - if err = serialize.File.ReadAnyFileContent(configFile, &fileReceiver); nil != err { - return errors.New(configFile + " -> parse config file fail : " + err.Error()) - } - } - - } // 解析命令行参数 - if _, err = flags.Parse(cliReceiver); nil != err { + if err = parseCliParam(cliReceiver); nil != err { return err } + + // 解析环境变量 + if err = parseEnvParam(envReceiver, envFileList); nil != err { + return err + } + + // 解析文件参数 + if err = parseFilParam(fileReceiver, configFileList); nil != err { + return err + } + + // 合并配置数据 if err = serialize.JSON.MergeDataForReceiver(configReceiver, fileReceiver, envReceiver, configReceiver); nil != err { return errors.New("merge config data fail : " + err.Error()) } return nil } + +// parseCliParam 解析命令行参数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:04 2024/11/8 +func parseCliParam(cliReceiver any) error { + if nil == cliReceiver { + return nil + } + // 解析命令行参数 + if _, err := flags.Parse(cliReceiver); nil != err { + return errors.New("cli param parse fail : " + err.Error()) + } + return nil +} + +// parseEnvParam 解析环境变量参数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:50 2024/11/8 +func parseEnvParam(envReceiver any, envFileList []string) error { + if nil == envReceiver { + return nil + } + if nil == envFileList { + // 传nil视为默认加载 .env 的行为禁用掉 + envFileList = []string{} + } else { + if len(envFileList) == 0 { + envFileList = []string{".env"} + } + } + + // 加载环境变量 + if err := godotenv.Load(envFileList...); nil != err { + return err + } + if err := env.Parse(envReceiver); nil != err { + return errors.New("env param parse fail : " + err.Error()) + } + + return nil +} + +// parseFilParam ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:54 2024/11/8 +func parseFilParam(fileReceiver any, configFileList []*string) error { + if nil == fileReceiver || len(configFileList) == 0 { + return nil + } + // 读取配置文件内容 + for _, itemConfigFilePath := range configFileList { + if nil == itemConfigFilePath { + continue + } + itemConfigFile := *itemConfigFilePath + if len(itemConfigFile) == 0 { + continue + } + if err := serialize.File.ReadAnyFileContent(itemConfigFile, &fileReceiver); nil != err { + return errors.New(itemConfigFile + " -> parse config file fail : " + err.Error()) + } + } + + return nil +}