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

增加 配置文件是否存在

fly 1 жил өмнө
parent
commit
fb377c5a1a
2 өөрчлөгдсөн 27 нэмэгдсэн , 2 устгасан
  1. 13 0
      config/config.go
  2. 14 2
      config/object.go

+ 13 - 0
config/config.go

@@ -58,3 +58,16 @@ func GetStringSlice(module string, key string) []string {
 func Set(module, key string, value any) {
 	viper.Set(module+"."+key, value)
 }
+
+func IsExistConfigFile() bool {
+	if err := viper.ReadInConfig(); err != nil {
+		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
+			// 配置文件未找到
+			return true
+		} else {
+			// 其他错误
+			return false
+		}
+	}
+	return false
+}

+ 14 - 2
config/object.go

@@ -57,9 +57,21 @@ func (c *config) GetFloat(module, key string) float64 {
 }
 
 func (c *config) GetStringSlice(module string, key string) []string {
-	return viper.GetStringSlice(module + "." + key)
+	return c.Viper.GetStringSlice(module + "." + key)
 }
 
 func (c *config) Set(module, key string, value any) {
-	viper.Set(module+"."+key, value)
+	c.Viper.Set(module+"."+key, value)
+}
+func (c *config) IsExistConfigFile() bool {
+	if err := c.Viper.ReadInConfig(); err != nil {
+		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
+			// 配置文件未找到
+			return true
+		} else {
+			// 其他错误
+			return false
+		}
+	}
+	return false
 }