From c926656fb86a8b33b852df52098d1f47044d2b52 Mon Sep 17 00:00:00 2001 From: maplepie <9009008+maplepie@users.noreply.github.com> Date: Sat, 12 Sep 2020 12:05:49 +0800 Subject: [PATCH] Update config.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持自定义配置文件位置 --- server/core/config.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/server/core/config.go b/server/core/config.go index dcb5b95e..c06bcca1 100644 --- a/server/core/config.go +++ b/server/core/config.go @@ -6,13 +6,27 @@ import ( _ "gin-vue-admin/packfile" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "github.com/spf13/pflag" ) const defaultConfigFile = "config.yaml" func init() { + pflag.StringP("configFile","c", "", "choose config file.") + pflag.Parse() + + // 优先级: 命令行 > 环境变量 > 默认值 v := viper.New() - v.SetConfigFile(defaultConfigFile) + v.BindPFlags(pflag.CommandLine) + v.SetEnvPrefix("gva") + v.BindEnv("configFile") // GVA_CONFIGFILE + + configFile := v.GetString("configFile") + if configFile == ""{ + configFile = defaultConfigFile + } + + v.SetConfigFile(configFile) err := v.ReadInConfig() if err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err))