1 Star 0 Fork 0

天雨流芳 / go-micro-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
viper_config.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
天雨流芳 提交于 2024-03-19 19:58 . 通用的应用启动逻辑封装
package config
import (
"errors"
"fmt"
"gitee.com/tylf2018/go-micro-framework/pkg/common/util/fileutil"
"github.com/spf13/viper"
)
// ReadFile 通用的读取配置文件的方法,传入路径和文件名以及类型,返回一个Viper的指针
func ReadFile(filePath, fileName, configType string) (config *viper.Viper, err error) {
config = viper.New()
filePath = fileutil.TransToRedirectPaths([]string{filePath})[0]
config.AddConfigPath(filePath)
config.SetConfigName(fileName)
config.SetConfigType(configType)
if err := config.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
fmt.Println("Not found config file")
}
}
return
}
// ReadIniConfig 读取ini类型配置文件
func ReadIniConfig(filePath, fileName string) (*viper.Viper, error) {
return ReadFile(filePath, fileName, "ini")
}
// ReadJsonConfig 读取json类型配置文件
func ReadJsonConfig(filePath, fileName string) (*viper.Viper, error) {
return ReadFile(filePath, fileName, "json")
}
// ReadYamlConfig 读取yaml类型配置文件
func ReadYamlConfig(filePath, fileName string) (*viper.Viper, error) {
return ReadFile(filePath, fileName, "yaml")
}
func MappingConfigToStruct(config *viper.Viper, configTypes ...interface{}) error {
if config == nil || len(configTypes) == 0 {
return nil
}
for _, configType := range configTypes {
err := config.Unmarshal(configType)
if err != nil {
return err
}
}
return nil
}
1
https://gitee.com/tylf2018/go-micro-framework.git
git@gitee.com:tylf2018/go-micro-framework.git
tylf2018
go-micro-framework
go-micro-framework
e87e0c3d7074

搜索帮助