From a7a96310e0bfd3c95caf913d63f6429415809507 Mon Sep 17 00:00:00 2001 From: DLDInternet Date: Tue, 27 Sep 2016 19:45:23 -0400 Subject: [PATCH 1/3] Set the defaults BEFORE considering a --config CLI argument --- cobra/cmd/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 065c8bf4..78602fa8 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -57,12 +57,12 @@ func init() { // Read in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { // enable ability to specify config file via flag + viper.SetConfigName(".cobra") // name of config file (without extension) + viper.AddConfigPath("$HOME") // adding home directory as first search path + if cfgFile != "" { // enable ability to specify config file via flag viper.SetConfigFile(cfgFile) } - viper.SetConfigName(".cobra") // 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. From 78ca2b3bdd1dcd2204f28d31c7746ba01d21c86a Mon Sep 17 00:00:00 2001 From: DLDInternet Date: Tue, 27 Sep 2016 19:56:13 -0400 Subject: [PATCH 2/3] gofmt -d -s . --- cobra/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 78602fa8..560e8144 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -63,7 +63,7 @@ func initConfig() { viper.SetConfigFile(cfgFile) } - viper.AutomaticEnv() // read in environment variables that match + viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { From 59e2709e24e367307c5e5603f727ffb74a24ebf6 Mon Sep 17 00:00:00 2001 From: DLDInternet Date: Tue, 27 Sep 2016 20:13:54 -0400 Subject: [PATCH 3/3] Generated apps will need to inherit this fix --- cobra/cmd/init.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 13792f12..b1236583 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -205,13 +205,13 @@ func init() { {{ if .viper }} // initConfig reads in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { // enable ability to specify config file via flag + viper.SetConfigName(".{{ .appName }}") // name of config file (without extension) + viper.AddConfigPath("$HOME") // adding home directory as first search path + if cfgFile != "" { // enable ability to specify config file via flag viper.SetConfigFile(cfgFile) } - viper.SetConfigName(".{{ .appName }}") // name of config file (without extension) - viper.AddConfigPath("$HOME") // adding home directory as first search path - viper.AutomaticEnv() // read in environment variables that match + viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil {