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 (
|
2024-11-05 18:04:27 +08:00
|
|
|
"errors"
|
2024-11-07 18:47:44 +08:00
|
|
|
"fmt"
|
2024-11-05 18:20:20 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
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-07 18:47:44 +08:00
|
|
|
"github.com/spf13/viper"
|
2024-11-05 18:04:27 +08:00
|
|
|
"reflect"
|
2024-11-05 17:06:26 +08:00
|
|
|
)
|
|
|
|
|
2024-11-07 18:47:44 +08:00
|
|
|
// InitByViper 通过viper库初始化配置
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:44 2024/11/7
|
|
|
|
//
|
|
|
|
// 配置解析优先级 : 显示的值设置 > 命令行标记 > 环境变量 > 配置文件 > 键值存储 > 默认值
|
|
|
|
//
|
|
|
|
// 参数:
|
|
|
|
// - configReceiver 接收参数结果的指针
|
|
|
|
// - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件
|
|
|
|
// - configFilePath 配置文件目录
|
|
|
|
// - configFileNameList 要加载的配置文件列表
|
|
|
|
func InitByViper(configReceiver any, envFileList []string, configFilePath string, configFileNameList []string) error {
|
2024-11-07 18:51:33 +08:00
|
|
|
viper.AddConfigPath(configFilePath)
|
|
|
|
for _, configFileName := range configFileNameList {
|
|
|
|
viper.SetConfigName(configFileName)
|
|
|
|
}
|
2024-11-07 18:47:44 +08:00
|
|
|
// 解析配置
|
|
|
|
if err := viper.ReadInConfig(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 获取全部配置,并解析
|
|
|
|
if err := serialize.JSON.Transition(viper.AllSettings(), configReceiver); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
|
|
|
// 参数:
|
|
|
|
// - configReceiver 接收参数结果的指针
|
|
|
|
// - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件
|
|
|
|
// - configFile 配置文件路径
|
|
|
|
func Init(configReceiver any, envFileList []string, configFile string) error {
|
2024-11-05 17:06:26 +08:00
|
|
|
var (
|
2024-11-07 18:47:44 +08:00
|
|
|
err error
|
|
|
|
fileConfigMap = map[string]any{}
|
2024-11-05 17:06:26 +08:00
|
|
|
)
|
|
|
|
|
2024-11-05 18:20:20 +08:00
|
|
|
if nil == envFileList {
|
|
|
|
envFileList = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查结构体指针是否合法
|
2024-11-05 18:04:27 +08:00
|
|
|
if err = checkConfigReceiver(configReceiver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:20:20 +08:00
|
|
|
// 加载环境变量
|
|
|
|
if err = godotenv.Load(envFileList...); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 解析命令行参数
|
2024-11-07 18:47:44 +08:00
|
|
|
if _, err = flags.Parse(configReceiver); nil != err {
|
2024-11-05 18:20:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-11-07 18:47:44 +08:00
|
|
|
|
|
|
|
// 读取配置文件内容
|
|
|
|
if err = serialize.File.ReadAnyFileContent(configFile, &fileConfigMap); nil != err {
|
2024-11-05 18:20:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-11-07 18:47:44 +08:00
|
|
|
|
2024-11-05 18:20:20 +08:00
|
|
|
// 解析配置文件参数
|
2024-11-07 18:47:44 +08:00
|
|
|
configTypeReflect := reflect.TypeOf(configReceiver).Elem()
|
|
|
|
configValueReflect := reflect.ValueOf(configReceiver).Elem()
|
|
|
|
configFieldNum := configValueReflect.NumField()
|
|
|
|
for i := 0; i < configFieldNum; i++ {
|
|
|
|
fmt.Println(configTypeReflect.Field(i).Name)
|
2024-11-05 17:06:26 +08:00
|
|
|
}
|
2024-11-07 18:47:44 +08:00
|
|
|
|
2024-11-05 17:06:26 +08:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-05 18:04:27 +08:00
|
|
|
|
|
|
|
// checkConfigReceiver 校验配置接收者必须是指针
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:16 2024/11/5
|
|
|
|
func checkConfigReceiver(configReceiver any) error {
|
|
|
|
if nil == configReceiver {
|
|
|
|
return errors.New("config receiver is nil")
|
|
|
|
}
|
|
|
|
receiverType := reflect.TypeOf(configReceiver)
|
|
|
|
if receiverType.Kind() != reflect.Ptr {
|
|
|
|
return errors.New("config receiver is not a pointer")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloneReceiver ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:16 2024/11/5
|
|
|
|
func cloneReceiver(configReceiver any) any {
|
|
|
|
dataType := reflect.TypeOf(configReceiver).Elem()
|
|
|
|
return reflect.New(dataType).Interface()
|
|
|
|
}
|