loading from multiple providers
tmzane opened this issue · 6 comments
Consider adding an option to load environment variables from multiple providers at once. It might be useful to merge variables from env.OS
and env.File
(if #8 is closed). It could be implemented as a wrapper around a slice of env.Provider
, which implements the interface itself. The API would be something like this:
// provider.go
func MultiProvider(ps ...Provider) Provider { ... }
// main.go
p := env.MultiProvider(
env.File(), // first, load available variables from .env file
env.OS, // then, if the same variables are provided by OS, override them
)
if err := env.LoadFrom(p, &cfg); err != nil {
// handle error
}
Another use case would be putting default values into env.Map
:
p := env.MultiProvider(
env.Map{"PORT":"8080"},
env.OS,
)
if err := env.LoadFrom(p, &cfg); err != nil {
// handle error
}
@junk1tm I would like to work on this as part of Hacktoberfest 2022
@amoghrajesh sure, go ahead 👍 Let me know if you have any questions
@junk1tm Not very sure on what logic we are looking in // provider.go func MultiProvider(ps ...Provider) Provider { ... }
I was thinking of something like:
var allProviders []Provider
func MultiProvider(ps ...Provider) []Provider {
for _, p := range ps {
allProviders = append(allProviders, p)
}
return allProviders
}
I think it's still relevant, we could use env.OS
combined with env.Map
as an alternative defaults provider, please see my second comment. Also, just like said, it could be a future proof for new env.Provider
implementations
Talking about code, let's start with something like this:
func MultiProvider(ps ...Provider) Provider { return providers(ps) }
type providers []Provider
func (ps providers) LookupEnv(key string) (string, bool) {
// implement me
}