Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix(config): replace viper with koanf to make map case sensitivity (#47)
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>

Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode authored Oct 14, 2022
1 parent 6fe8375 commit 0fe3121
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 238 deletions.
5 changes: 0 additions & 5 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

//// 捷径函数 ////
Expand All @@ -17,7 +16,6 @@ func flag(cmd *cobra.Command, name string, defaultVal interface{}, usage string)
case string:
f.String(name, y, usage)
}
viper.SetDefault(name, defaultVal)
}

func flagP(cmd *cobra.Command, name, shorthand string, defaultVal interface{}, usage string) {
Expand All @@ -30,15 +28,12 @@ func flagP(cmd *cobra.Command, name, shorthand string, defaultVal interface{}, u
case string:
f.StringP(name, shorthand, y, usage)
}
viper.SetDefault(name, defaultVal)
}

func flagV(cmd *cobra.Command, name string, defaultVal interface{}, usage string) {
flag(cmd, name, defaultVal, usage)
viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))
}

func flagPV(cmd *cobra.Command, name, shorthand string, defaultVal interface{}, usage string) {
flagP(cmd, name, shorthand, defaultVal, usage)
viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))
}
319 changes: 141 additions & 178 deletions config/config.go

Large diffs are not rendered by default.

57 changes: 30 additions & 27 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,55 @@ import (
"strings"
"time"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// Instance 配置实例
var Instance *Config
const DEFAULT_CONF_FILE = "artalk-go.yml"

// Init 初始化配置
func Init(cfgFile string, workDir string) {
viper.Reset()
viper.SetConfigType("yaml")
var (
kf = koanf.New(".")
parser = yaml.Parser()

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find config file in path.
viper.AddConfigPath(".")
viper.SetConfigName("artalk-go.yml")
}
// 配置实例
Instance *Config
cfgFileLoaded string
)

viper.SetEnvPrefix("ATG")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
func GetCfgFileLoaded() string {
return cfgFileLoaded
}

// Init 初始化配置
func Init(cfgFile string, workDir string) {
// 切换工作目录
if workDir != "" {
viper.AddConfigPath(workDir) // must before
if err := os.Chdir(workDir); err != nil {
logrus.Fatal("工作目录切换错误 ", err)
}
}

if err := viper.ReadInConfig(); err == nil {
// fmt.Print("\n")
// fmt.Println("- Using ArtalkGo config file:", viper.ConfigFileUsed())
} else {
logrus.Fatal("找不到配置文件,使用 `-h` 参数查看帮助")
if cfgFile == "" {
cfgFile = DEFAULT_CONF_FILE
}

// load yaml config
if err := kf.Load(file.Provider(cfgFile), parser); err != nil {
logrus.Errorln(err)
logrus.Fatal("配置文件读取错误")
}

Instance = &Config{}
err := viper.Unmarshal(&Instance)
if err != nil {
logrus.Errorf("unable to decode into struct, %v", err)

if err := kf.Unmarshal("", Instance); err != nil {
logrus.Errorln(err)
logrus.Fatal("配置文件解析错误")
}

cfgFileLoaded = cfgFile

// 后续处理
postInit()
}
Expand Down
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/jedib0t/go-pretty/v6 v6.2.4
github.com/jeremywohl/flatten v1.0.1
github.com/knadh/koanf v1.4.3
github.com/labstack/echo/v4 v4.5.0
github.com/markbates/pkger v0.17.1
github.com/microcosm-cc/bluemonday v1.0.18
github.com/mitchellh/mapstructure v1.4.1
github.com/mitchellh/mapstructure v1.5.0
github.com/nikoksr/notify v0.23.0
github.com/onrik/logrus v0.9.0
github.com/qwqcode/go-aliyun-email v0.0.0-20180120030821-cb6e7b1382bf
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/steambap/captcha v1.4.1
github.com/stretchr/testify v1.7.1
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.385
Expand Down Expand Up @@ -71,7 +71,6 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -88,31 +87,28 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/line/line-bot-sdk-go v7.8.0+incompatible // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.8 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pegasus-kv/thrift v0.13.0 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.10.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.18.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/slack-go/slack v0.10.2 // indirect
github.com/smartystreets/assertions v1.0.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand All @@ -136,6 +132,6 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
k8s.io/apimachinery v0.0.0-20191123233150-4c4803ed55e3 // indirect
)
Loading

0 comments on commit 0fe3121

Please sign in to comment.