Skip to content

Commit

Permalink
Merge pull request #2 from paradeum-team/kli
Browse files Browse the repository at this point in the history
add environment variable configuration
  • Loading branch information
likai1130 authored Oct 13, 2022
2 parents 2e804c6 + e1ade7d commit ce8b747
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"github.com/spf13/viper"
"strings"
)

var (
Expand Down Expand Up @@ -73,16 +74,29 @@ func Setup(configFile string,
Extend: ExtendConfig,
callbacks: fs,
}

v := viper.New()
//自动获取全部的env加入到viper中。默认别名和环境变量名一致。(如果环境变量多就全部加进来)
v.AutomaticEnv()

//替换读取格式。默认a.b.c.d格式读取env,改为a_b_c_d格式读取
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

// 本地配置文件位置
v.SetConfigFile(configFile)

//支持 "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
v.SetConfigType("yaml")

//读配置文件到viper配置中
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}

if err := v.Unmarshal(&_cfg); err != nil {
fmt.Println(err)
// 系列化成config对象
if err = v.Unmarshal(&_cfg); err != nil {
panic(err)
}

_cfg.Init()
Expand Down

0 comments on commit ce8b747

Please sign in to comment.