You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A short example to briefly illustrate the problem:
package main
import (
"bytes""fmt""github.com/spf13/viper"
)
funcmain() {
dotenvExample:= []byte(`DATABASE_PORT=5432`)
viper.SetConfigType("env")
viper.ReadConfig(bytes.NewBuffer(dotenvExample))
viper.BindEnv("database.port", "DATABASE_PORT")
// I would expect 5432, but got <nil>fmt.Println(viper.Get("database.port"))
}
A cursory look at the source suggests that the v.env variable stores the mapping of key => ENV_VAR (i.e. database.port => DATABASE_PORT). A fast way to resolve this would be to search through the v.env variable when marshaling dotenv config file, and assign the v.env's key if the value matches.
However, doing this way introduce an ordering constraint: viper.BindEnv must happen before viper.ReadConfig, otherwise the v.env would not be populated correctly then.
Any suggestions on what's the best way to move forward on this?
The text was updated successfully, but these errors were encountered:
Personally, I would probably just load the dotenv file contents as environment variables. It's a simple solution and does not require huge changes with very little benefit in Viper.
I'm not sure I agree with the assumption that a dotenv file (parsed as file) should behave exactly like environment variables.
It's a file format from Viper's point of view, which means keys could be normalized (eg. using the string replacer), but binding is probably out of scope.
That being said, this could be a use case considered in a next version (#772) of the library.
For now, I would just set those variables as env vars from the file (pretty sure there are libraries for that).
A short example to briefly illustrate the problem:
A cursory look at the source suggests that the
v.env
variable stores the mapping ofkey => ENV_VAR
(i.e.database.port => DATABASE_PORT
). A fast way to resolve this would be to search through thev.env
variable when marshalingdotenv
config file, and assign thev.env
's key if the value matches.However, doing this way introduce an ordering constraint:
viper.BindEnv
must happen beforeviper.ReadConfig
, otherwise thev.env
would not be populated correctly then.Any suggestions on what's the best way to move forward on this?
The text was updated successfully, but these errors were encountered: