Skip to content

Commit

Permalink
Fix environment variables not working when config file is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Aikens committed Jul 27, 2018
1 parent 89d347a commit 0c2f3cd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ func initConfig() {
viper.AddConfigPath(fmt.Sprintf("%s/algolia-hugo", xdg.ConfigHome()))
viper.SetConfigName("algolia-hugo")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
_ = viper.ReadInConfig()

viper.AutomaticEnv() // bind to environment variables that match key names

// Unmarshal the config into the config variable
_ = viper.Unmarshal(&config)

// If the environment variables exist, we need to get the values and set the config
config.AlgoliaAPIKey = viper.GetString("algolia_api_key")
config.AlgoliaAppID = viper.GetString("algolia_app_id")
config.AlgoliaIndexName = viper.GetString("algolia_index_name")

if config.Verbose {
log.WithField("config", viper.ConfigFileUsed()).Info("Loaded config")
}
Expand Down

0 comments on commit 0c2f3cd

Please sign in to comment.