config/init.go

135 lines
3.3 KiB
Go
Raw Normal View History

2024-11-05 17:06:26 +08:00
// Package config ...
//
// Description : config ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-05 16:23
package config
import (
"errors"
2024-11-05 18:20:20 +08:00
"git.zhangdeman.cn/zhangdeman/serialize"
2024-11-08 14:54:32 +08:00
"github.com/caarlos0/env/v9"
2024-11-05 17:06:26 +08:00
"github.com/jessevdk/go-flags"
2024-11-05 18:20:20 +08:00
"github.com/joho/godotenv"
2024-11-08 16:56:54 +08:00
"os"
2024-11-08 17:06:14 +08:00
"strings"
2024-11-05 17:06:26 +08:00
)
// Init 初始化配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2024/11/5
2024-11-05 18:20:20 +08:00
//
// 参数:
2024-11-08 14:54:32 +08:00
// - configReceiver 接收最终配置结果的指针
// - cliReceiver 命令函参数配置结果
// - envReceiver 环境变量配置结果
// - fileReceiver 文件配置结果
2024-11-05 18:20:20 +08:00
// - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件
2024-11-08 16:24:33 +08:00
// - configFileList 配置文件列表
func Init(configReceiver any, cliReceiver any, envReceiver any, fileReceiver any, envFileList []string, configFileList []*string) error {
2024-11-05 17:06:26 +08:00
var (
2024-11-08 14:54:32 +08:00
err error
2024-11-05 17:06:26 +08:00
)
2024-11-08 16:24:33 +08:00
// 解析命令行参数
if err = parseCliParam(cliReceiver); nil != err {
return err
2024-11-05 18:20:20 +08:00
}
2024-11-08 16:24:33 +08:00
// 解析环境变量
if err = parseEnvParam(envReceiver, envFileList); nil != err {
return err
}
2024-11-08 16:24:33 +08:00
// 解析文件参数
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
}
2024-11-05 18:20:20 +08:00
2024-11-08 16:24:33 +08:00
// parseCliParam 解析命令行参数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:04 2024/11/8
func parseCliParam(cliReceiver any) error {
if nil == cliReceiver {
return nil
2024-11-05 18:20:20 +08:00
}
2024-11-08 14:54:32 +08:00
// 解析命令行参数
2024-11-08 16:56:54 +08:00
parser := flags.NewParser(cliReceiver, flags.Default)
if _, err := parser.ParseArgs(os.Args[1:]); nil != err {
2024-11-08 17:06:14 +08:00
if strings.Contains(strings.ToLower(err.Error()), "usage:") {
// 显示帮助文档, 严格来讲不是error, go-flags库利用error实现
return err
}
2024-11-08 16:24:33 +08:00
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 {
2024-11-05 18:20:20 +08:00
return err
}
2024-11-08 16:24:33 +08:00
if err := env.Parse(envReceiver); nil != err {
return errors.New("env param parse fail : " + err.Error())
2024-11-05 17:06:26 +08:00
}
2024-11-08 16:24:33 +08:00
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())
}
}
2024-11-05 17:06:26 +08:00
return nil
}