完成参数解析
This commit is contained in:
115
init.go
115
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user