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

Unclear doc #1777

Open
jsumners opened this issue Mar 3, 2024 · 2 comments
Open

Unclear doc #1777

jsumners opened this issue Mar 3, 2024 · 2 comments
Labels
kind/bug Something isn't working

Comments

@jsumners
Copy link

jsumners commented Mar 3, 2024

viper/README.md

Line 144 in 5870123

*NOTE [since 1.6]:* You can also have a file without an extension and specify the format programmatically. For those configuration files that lie in the home of the user without any extension like `.bashrc`

That line of the readme is a bit confusing. It took navigating the git blame history to find 9cd5712 and reviewing the commit in context for me to understand that it is saying a file named ~/.foo can be loaded as YAML when viper.SetConfigType("yaml") is used.

I was looking for a way to support the following file path possibilities:

  • /etc/foo/config.yaml
  • $HOME/.foo
  • $HOME/.foo.yaml
  • ./.foo
  • ./.foo.yaml

It's still not clear to me if I can support that list.

@jsumners jsumners added the kind/bug Something isn't working label Mar 3, 2024
Copy link

github-actions bot commented Mar 3, 2024

👋 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,
either by sharing the above link or telling people about this on Twitter:

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

Thank you! ❤️

@sagikazarmark
Copy link
Collaborator

@jsumners I don't think it's possible with the current implementation. I agree it's confusing, not very well explained and evolved in the wrong way over time.

I opened #1795 with a proposal for a better file searching API.

Here is how it would work in your case:

finder := locafero.Finder{
	Paths: []string{".", "$HOME", "/etc/foo"},
	Names: []string{".foo", ".foo.yaml", "config.yaml"},
	Type:  locafero.FileTypeFile,
}

v := viper.New(viper.WithFinder(finder))

The above is still not perfect though since it would allow results like $HOME/config.yaml.

I opened sagikazarmark/locafero#26 to address that. Considering that proposal, here is how it would roughly look like:

finder := locafero.AggregateFinders(
	locafero.Finder{
		Paths: []string{".", "$HOME"},
		Names: []string{".foo", ".foo.yaml"},
		Type:  locafero.FileTypeFile,
	},
	locafero.Finder{
		Paths: []string{"/etc/foo"},
		Names: []string{"config.yaml"},
		Type:  locafero.FileTypeFile,
	},
)

v := viper.New(viper.WithFinder(finder))

You could potentially play with supporting multiple file types if you wanted to.

What do you think? Would this suffice your needs? (Your feedback would be very helpful as I just started to work on this solution, so I'm looking to validate this new API)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants