spf13 / viper

Go configuration with fangs
MIT License
26.93k stars 2.02k forks source link

fix: search config file error #1754

Closed amone-bit closed 2 months ago

amone-bit commented 8 months ago

There have two config files that have same file name and different extention. image And their contents follow. image image This is my code.That will produce a problem.I expect the file named "key_map.toml", but viper read the file named "key_map.json" image And that were some error information that viper gived. image So I update the "file.go" to fix the problem.And now work good. image

CLAassistant commented 8 months ago

CLA assistant check
All committers have signed the CLA.

github-actions[bot] commented 8 months ago

👋 Thanks for contributing to Viper! You are awesome! 🎉

A maintainer will take a look at your pull request 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, either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

sagikazarmark commented 6 months ago

There is a lot of confusion around how file searching works in Viper right now.

Technically, config type was not supposed to be used when looking for a file, only for encoding.

The problem with this PR is that it breaks loading files without extensions.

Here is a test that breaks with this PR:

    t.Run("find file without extension", func(t *testing.T) {
        fs := afero.NewMemMapFs()

        err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
        require.NoError(t, err)

        file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config"))
        require.NoError(t, err)

        _, err = file.WriteString(`key: value`)
        require.NoError(t, err)

        file.Close()

        v := New()

        v.SetFs(fs)
        v.AddConfigPath("/etc/viper")
        v.SetConfigType("yaml")

        err = v.ReadInConfig()
        require.NoError(t, err)

        assert.Equal(t, "value", v.Get("key"))
    })

I opened #1795 to start brainstorming on a better solution.