Skip to content

viper.SetConfigFile()  #1505

@zouxingyuks

Description

@zouxingyuks

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

Viper Version

1.15.0

Go Version

1.19.3

Config Source

Files

Format

YAML

Repl.it link

https://replit.com/@zouxingyuks/viper#main.go

Code reproducing the issue

package main

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	"os"
)

// 设置默认配置信息
var defaultConifg = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
  jacket: leather
  trousers: denim
age: 35
eyes : brown
beard: true
`)

func init() {
	parseConfig()
}
func main() {
	//在main函数中添加下列代码
	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		// 配置文件发生变更之后会调用的回调函数
		fmt.Println("Config file changed:", e.Name)
	})
	fmt.Println(viper.GetString("username"))
}
func parseConfig() {
	// 指定配置文件路径
	configPath := "./config.yaml"
	viper.SetConfigFile(configPath)
	if err := viper.ReadInConfig(); err != nil {
		// 配置文件出错
		//todo 设置日志输出
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			// 配置文件未找到错误;如果需要可以忽略
			os.Create(configPath)
			//初始化配置文件

			//todo 补充日志创建说明
		} else {
			// 配置文件被找到,但产生了另外的错误
			test1, test2 := err.(viper.ConfigFileNotFoundError)
			fmt.Println(test1, test2)
			panic("The configuration file was found, but another error was generated")
		}
	}

	// 配置文件找到并成功解析
}

Expected Behavior

When the configuration file does not exist, the code that creates the file should be executed

Actual Behavior

A panic statement was executed

Steps To Reproduce

创建main.go
package main

import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"os"
)

// 设置默认配置信息
var defaultConifg = []byte(`
Hacker: true
name: steve
hobbies:

  • skateboarding
  • snowboarding
  • go
    clothing:
    jacket: leather
    trousers: denim
    age: 35
    eyes : brown
    beard: true
    `)

func init() {
parseConfig()
}
func main() {
//在main函数中添加下列代码
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
// 配置文件发生变更之后会调用的回调函数
fmt.Println("Config file changed:", e.Name)
})
fmt.Println(viper.GetString("username"))
}
func parseConfig() {
// 指定配置文件路径
configPath := "./config.yaml"
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
// 配置文件出错
//todo 设置日志输出
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// 配置文件未找到错误;如果需要可以忽略
os.Create(configPath)
//初始化配置文件

		//todo 补充日志创建说明
	} else {
		// 配置文件被找到,但产生了另外的错误
		test1, test2 := err.(viper.ConfigFileNotFoundError)
		fmt.Println(test1, test2)
		panic("The configuration file was found, but another error was generated")
	}
}

// 配置文件找到并成功解析

}
执行,成功应创建config.yaml

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions