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

Can I tell viper.Unmarshal to take environment variables into account? #212

Closed
aeneasr opened this issue Jul 25, 2016 · 8 comments
Closed

Comments

@aeneasr
Copy link
Contributor

aeneasr commented Jul 25, 2016

This is my viper config:

    if cfgFile != "" {
        // enable ability to specify config file via flag
        viper.SetConfigFile(cfgFile)
    }

    path := absPathify("$HOME")
    if _, err := os.Stat(filepath.Join(path, ".hydra.yml")); err != nil {
        _, _ = os.Create(filepath.Join(path, ".hydra.yml"))
    }

    viper.SetConfigType("yaml")
    viper.SetConfigName(".hydra") // name of config file (without extension)
    viper.AddConfigPath("$HOME")  // adding home directory as first search path
    viper.AutomaticEnv()          // read in environment variables that match

    // If a config file is found, read it in.
    if err := viper.ReadInConfig(); err != nil {
        fmt.Printf(`Config file not found because "%s"`, err)
        fmt.Println("")
    }

I also have a bunch of environment varibales, like CLIENT_ID or DATABASE_URL. Right now I'm setting those via:

    if databaseURL, ok := viper.Get("DATABASE_URL").(string); ok {
        c.DatabaseURL = databaseURL
    }

Is it possible to tell viper.Unmarshal to use the environment variables in addition to the config file?

@aeneasr
Copy link
Contributor Author

aeneasr commented Jul 29, 2016

yes, i need to use BindEnv and write the keys upper case in mapstructure:...

@aeneasr aeneasr closed this as completed Jul 29, 2016
@postcert
Copy link

postcert commented Aug 2, 2016

I was able to get it working without BindEnv or setting the mapstructure tags uppercase.
Though I am calling ReadInConfig prior to AutomaticEnv. Then after unmarshaling into the config struct of course.

Give it a try, repeated BindEnv calls sounds very tedious.

@aeneasr
Copy link
Contributor Author

aeneasr commented Aug 2, 2016

That worked for me too!

@Ehekatl
Copy link

Ehekatl commented Aug 18, 2016

@postcert did't get it work here, can u give some sample code ? many thx

@Ehekatl
Copy link

Ehekatl commented Aug 18, 2016

I found that unmarshal env variable only work when the key is also in config file...not sure if this is bug

@postcert
Copy link

postcert commented Aug 18, 2016

Yes, it only works when already present in the config file. Still discovering these edge cases.

@astoliarov
Copy link

Hi! Is it working?

@sagikazarmark
Copy link
Collaborator

Please see and follow the issue above. ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants