go-simpler / env

🔍 Load environment variables into a config struct

Home Page:https://go-simpler.org/env

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loading from multiple providers

tmzane opened this issue · comments

commented

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
}
commented

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

commented

@amoghrajesh sure, go ahead 👍 Let me know if you have any questions

@junk1tm we have closed #8. Is this still relevant?
If not now, this fix could be a future proof in case more types come in

@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
}
commented

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
}