From e9878aa4377bd5485adb2cfba95e6a8b139362dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Wed, 12 Apr 2023 15:35:33 +0800 Subject: [PATCH] config_test --- models/config.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/models/config.go b/models/config.go index 07949df..e6ca155 100644 --- a/models/config.go +++ b/models/config.go @@ -2,6 +2,7 @@ package models import ( "log" + "os" "github.com/spf13/viper" ) @@ -9,9 +10,13 @@ import ( var Viper *viper.Viper func init() { - viper.SetConfigName("config") // 设置配置文件的名字 - viper.SetConfigType("yaml") // 设置配置文件的格式 - viper.AddConfigPath("./data") // 设置配置文件所在的路径 + //如果命令行参数中有test,则使用测试环境的配置 + var config_file string = "./data/config.yaml" + if len(os.Args) > 1 && os.Args[1] == "test" { + log.Println("使用测试环境配置") + config_file = "./data/config_test.yaml" + } + viper.SetConfigFile(config_file) if err := viper.ReadInConfig(); err != nil { log.Println("读取配置文件失败", err) 生成配置文件()