Replies: 1 comment
|
You can use multiple v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath("~/.config/demo")
v.AddConfigPath("~/.") // fallback path
if err := v.ReadInConfig(); err != nil {
// handle missing config
}
If you need two completely different filenames (not just different paths), you'll need two viper instances and merge them: v1 := viper.New()
v1.SetConfigName("config")
v1.AddConfigPath("~/.config/demo")
v2 := viper.New()
v2.SetConfigName(".demo")
v2.AddConfigPath("~/.")
if err := v1.ReadInConfig(); err != nil {
if err := v2.ReadInConfig(); err != nil {
return fmt.Errorf("no config found in either location")
}
// use v2
} else {
// use v1
}For merging configs from multiple files, |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I have a question about best practices for loading multiple configuration files with different names. First, I want to load a config file at
~/.config/demo/config.yamland if that file doesn't exist, I want to fall back to~/.demo.yaml. How can I solve this since I can only assign a name withviper.SetConfigName(“config”)which is loadedAll reactions