From 0c2f3cd7091056b996ee37c421d0e7ab79b8f364 Mon Sep 17 00:00:00 2001 From: Patrick Aikens Date: Thu, 26 Jul 2018 21:39:57 -0400 Subject: [PATCH] Fix environment variables not working when config file is missing --- cmd/root.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6f62b6c..3f0c8f3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") }