使用相对于项目地址的配置文件路径以便于单元测试
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var Viper *viper.Viper
|
||||
var (
|
||||
_, b, _, _ = runtime.Caller(0)
|
||||
Root = filepath.Join(filepath.Dir(b), "..")
|
||||
Viper *viper.Viper
|
||||
)
|
||||
|
||||
func init() {
|
||||
//如果命令行参数中有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"
|
||||
}
|
||||
config_file := filepath.Join(Root, "data", "config.yaml")
|
||||
fmt.Println(config_file)
|
||||
|
||||
viper.SetConfigFile(config_file)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Println("读取配置文件失败", err)
|
||||
|
Reference in New Issue
Block a user