|
@@ -1,32 +1,35 @@
|
|
package core
|
|
package core
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "flag"
|
|
"fmt"
|
|
"fmt"
|
|
"gin-vue-admin/global"
|
|
"gin-vue-admin/global"
|
|
_ "gin-vue-admin/packfile"
|
|
_ "gin-vue-admin/packfile"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/spf13/viper"
|
|
"github.com/spf13/viper"
|
|
- "github.com/spf13/pflag"
|
|
|
|
|
|
+ "os"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+var config string
|
|
|
|
+
|
|
const defaultConfigFile = "config.yaml"
|
|
const defaultConfigFile = "config.yaml"
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
- pflag.StringP("configFile","c", "", "choose config file.")
|
|
|
|
- pflag.Parse()
|
|
|
|
-
|
|
|
|
- // 优先级: 命令行 > 环境变量 > 默认值
|
|
|
|
- v := viper.New()
|
|
|
|
- v.BindPFlags(pflag.CommandLine)
|
|
|
|
- v.SetEnvPrefix("gva")
|
|
|
|
- v.BindEnv("configFile") // GVA_CONFIGFILE
|
|
|
|
-
|
|
|
|
- configFile := v.GetString("configFile")
|
|
|
|
- if configFile == ""{
|
|
|
|
- configFile = defaultConfigFile
|
|
|
|
|
|
+ flag.StringVar(&config, "c", "", "choose config file.")
|
|
|
|
+ flag.Parse()
|
|
|
|
+ if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
|
|
|
|
+ if configEnv := os.Getenv(global.GVA_CONFIG.System.ConfigEnv); configEnv == "" {
|
|
|
|
+ config = defaultConfigFile
|
|
|
|
+ fmt.Printf("您正在使用config的默认值,config的路径为%v\n", defaultConfigFile)
|
|
|
|
+ } else {
|
|
|
|
+ config = configEnv
|
|
|
|
+ fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%v\n", config)
|
|
}
|
|
}
|
|
-
|
|
|
|
- v.SetConfigFile(configFile)
|
|
|
|
|
|
+ v := viper.New()
|
|
|
|
+ v.SetConfigFile(config)
|
|
err := v.ReadInConfig()
|
|
err := v.ReadInConfig()
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|