-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
viper.SetConfigFile() #1505
Comments
👋 Thanks for reporting! A maintainer will take a look at your issue shortly. 👀 In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 📣 If you've already given us your feedback, you can still help by spreading the news, https://twitter.com/sagikazarmark/status/1306904078967074816 Thank you! ❤️ |
I'm having a similar issue, and I believe the problem is from the call to the func main() {
v := viper.New()
v.SetConfigFile("./config.yaml")
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("here")
fmt.Println(err)
fmt.Println(reflect.TypeOf(err))
} else {
fmt.Println("there")
fmt.Println(err)
fmt.Println(reflect.TypeOf(err))
}
os.Exit(1)
}
os.Exit(0)
} output:
However, the documentation suggests doing this, in which case the error works as expected: func main() {
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("here")
fmt.Println(err)
fmt.Println(reflect.TypeOf(err))
} else {
fmt.Println("there")
fmt.Println(err)
fmt.Println(reflect.TypeOf(err))
}
os.Exit(1)
}
os.Exit(0)
} output:
Note that I manually redacted the full path So I believe using the second example will work in your case. However, I'm still confused by the fact that setting the config path explicitly gives a different error if the file is absent than setting paths for viper to look for the config. Is there a reason for this difference in behavior? Viper version & Go versionMy go.mod file
EDIT: I've been looking a bit through the viper code to try and understand what the issue is. The call Now, the call to
This returns the same error as fs.Open() if it can't open the file, which I assume is I don't know if this is expected behavior, but if it is I would at least provide a sample usage of the |
Hey @Ozoniuss, this part of your concerns was raised in #1491. The maintainers have not responded to that issue regarding whether it is expected behavior or not. If it is expected behavior, the documentation can be updated to better reflect this. If it is not expected behavior, one change we could implement would be to modify the |
Please see my comment in #1491 |
Preflight Checklist
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
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:
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)
//初始化配置文件
}
执行,成功应创建config.yaml
Additional Information
No response
The text was updated successfully, but these errors were encountered: