Эх сурвалжийг харах

配置方法文件 修改

fly 1 жил өмнө
parent
commit
7fa4d95d92
2 өөрчлөгдсөн 71 нэмэгдсэн , 14 устгасан
  1. 17 14
      config/config.go
  2. 54 0
      config/config_test.go

+ 17 - 14
config/config.go

@@ -7,27 +7,26 @@ import (
 type any interface{}
 
 var (
-	name       = "config"
-	configType = "yaml"
-	path       = "."
+	defaultName       = "config"
+	defaultConfigType = "yaml"
+	defaultPath       = "."
 )
 
-func init() {
-	Init()
-}
+//func init() {
+//	Init()
+//}
 
 func Init() {
 	//引入viper配置文件
-	viper.SetConfigName(name) //name := lib.AppName()
-	viper.SetConfigType(configType)
-	viper.AddConfigPath(path)
+	viper.SetConfigName(defaultName) //defaultName := lib.AppName()
+	viper.SetConfigType(defaultConfigType)
+	viper.AddConfigPath(defaultPath)
 }
 
-func SetConfigInfo(n, c, p string) {
-	name = n
-	configType = c
-	path = p
-	Init()
+func SetConfigInfo(name, suffix, path string) {
+	viper.SetConfigName(name) //defaultName := lib.AppName()
+	viper.SetConfigType(suffix)
+	viper.AddConfigPath(path)
 }
 
 func Load() error {
@@ -62,3 +61,7 @@ func GetStringSlice(module string, key string) []string {
 	return viper.GetStringSlice(module + "." + key)
 
 }
+
+func Set(module, key string, value any) {
+	viper.Set(module+"."+key, key)
+}

+ 54 - 0
config/config_test.go

@@ -0,0 +1,54 @@
+package config
+
+import (
+	"testing"
+)
+
+func TestInit(t *testing.T) {
+	tests := []struct {
+		name string
+	}{
+		// TODO: Add test cases.
+		{name: "t"},
+		{name: "t1"},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			Init()
+			t.Log(tt)
+			Store()
+		})
+	}
+}
+
+func TestSetConfigInfo(t *testing.T) {
+	type args struct {
+		name   string
+		config string
+		path   string
+	}
+	tests := []struct {
+		name string
+		args args
+	}{
+		// TODO: Add test cases.
+		{name: "t", args: args{
+			name:   "test1",
+			config: "ini",
+			path:   ".",
+		}},
+		{name: "t1", args: args{
+			name:   "test2",
+			config: "yaml",
+			path:   "./test/",
+		}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			t.Log(tt)
+			SetConfigInfo(tt.args.name, tt.args.config, tt.args.path)
+			Store()
+
+		})
+	}
+}