config/init.go

135 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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