File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,27 @@ import (
66 _ "gin-vue-admin/packfile"
77 "github.com/fsnotify/fsnotify"
88 "github.com/spf13/viper"
9+ "github.com/spf13/pflag"
910)
1011
1112const defaultConfigFile = "config.yaml"
1213
1314func init () {
15+ pflag .StringP ("configFile" ,"c" , "" , "choose config file." )
16+ pflag .Parse ()
17+
18+ // 优先级: 命令行 > 环境变量 > 默认值
1419 v := viper .New ()
15- v .SetConfigFile (defaultConfigFile )
20+ v .BindPFlags (pflag .CommandLine )
21+ v .SetEnvPrefix ("gva" )
22+ v .BindEnv ("configFile" ) // GVA_CONFIGFILE
23+
24+ configFile := v .GetString ("configFile" )
25+ if configFile == "" {
26+ configFile = defaultConfigFile
27+ }
28+
29+ v .SetConfigFile (configFile )
1630 err := v .ReadInConfig ()
1731 if err != nil {
1832 panic (fmt .Errorf ("Fatal error config file: %s \n " , err ))
You can’t perform that action at this time.
0 commit comments