Skip to content
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

Multiple config paths doesn't seem to be working #104

Closed
aacanakin opened this issue Jul 31, 2015 · 6 comments
Closed

Multiple config paths doesn't seem to be working #104

aacanakin opened this issue Jul 31, 2015 · 6 comments

Comments

@aacanakin
Copy link

I've been using viper with single config path. However, when I add multiple files the second one could not be retrieved. Here's the code;

    viper.SetConfigType("toml")

    viper.SetConfigName("config")

    viper.AddConfigPath("./config.toml")
    viper.AddConfigPath("./messages/messages.toml")

    messages.SetDefaultLocale("en")

    err := viper.ReadInConfig()
    if err != nil {
        panic(fmt.Errorf("%s\n", err))
    }

    fmt.Println("messages value", viper.GetString("hello.world"))
    fmt.Println("config value http.host", viper.GetString("http.host"))

Here's the output

messages value
config value http.host 0.0.0.0
# messages.toml
[hello]
world = "Hello world"

And one strange error, if I comment out with line ./config.toml, the output is same!. I think viper is doing some caching and it's not invalidating.

@aacanakin aacanakin changed the title Multiple ConfigPath s doesn't seem to be working Multiple config paths doesn't seem to be working Jul 31, 2015
@didenko
Copy link
Collaborator

didenko commented Aug 1, 2015

I think what you are describing is a "works as intended" behaviour, as described in the README.md. Think about the AddConfigPath method not as a path of the config file, but a path to a config file.

You provide an ordered collection of search paths, where viper searches for a file with a name you specified in the SetConfigName call and a type you specified in the SetConfigType call. The first matching file found in specified paths is used. If no matching file (config.toml in your example) is found then an error is raised. You should not expect viper to choose messages.toml after calling viper.SetConfigName("config").

The reason your example works with ./config.toml is a bug #73 which will soon be fixed. What happens is that config.toml is not found in any of the paths you specified (because those paths are files, not directories), but as of today viper erroneously looks for config.toml in the current directory, which you have. The issue #73 is addressing exactly this kind of confusion.

Hope that makes sense.

@aacanakin
Copy link
Author

So, the right solution would be to make a directory and put all toml files to that directory and call AddConfigPath pointing that directory, right ?

@aacanakin
Copy link
Author

Ok, I tried as you suggested. I have conf and messages folders. Both folders have config.toml. I have the following code;

viper.AddConfigPath("./conf/")
viper.AddConfigPath("./messages/")

the conf/config.toml variables are detected but again, messages are not found by viper. Any solutions

@didenko
Copy link
Collaborator

didenko commented Aug 1, 2015

If the explanation below is not sufficient then it would be helpful if you can post the new version of the relevant code, config files content, and expected outcomes - similar to the original post.

But let's walk through it again. If your new code looks like this:

viper.SetConfigType("toml")

viper.SetConfigName("config")

viper.AddConfigPath("./conf")
viper.AddConfigPath("./messages")

messages.SetDefaultLocale("en")

err := viper.ReadInConfig()
if err != nil {
    panic(fmt.Errorf("%s\n", err))
}

fmt.Println("messages value", viper.GetString("hello.world"))
fmt.Println("config value http.host", viper.GetString("http.host"))

and you have both ./conf/config.toml and ./messages/config.toml in the current directory when you run the executable, then both hello.world and http.host values will come from the ./conf/config.toml file. If it does not have one of the values, then the corresponding GetString should error in absence of a set default in this code.

If you delete the ./conf/config.toml file, then both values are expected to be found from the ./messages/config.toml file or error in a similar fashion.

Please, note, that as mentioned in the README.md:

a single Viper instance only supports a single configuration file

@aacanakin
Copy link
Author

I finally got it right. I used viper.New() for each file in messages folder and default viper calls from config.toml. Thanks for the help!

@lengocgiang
Copy link

If you still have this problem. Let try using
viper.Reset() before calls viper config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants