From d404c53e884b1718c4bb16085629abb1a5c8e374 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Thu, 12 Jan 2017 14:09:05 +0530 Subject: [PATCH] fixes config flag behaviour. related to #374 #375 --- cobra/cmd/init.go | 8 +++++--- cobra/cmd/root.go | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 13792f12..3c3658e2 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -205,14 +205,16 @@ 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.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 + // Checking for the cfgFile after setting default paths. + if cfgFile != "" { // enable ability to specify config file via flag + viper.SetConfigFile(cfgFile) + } + // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 065c8bf4..52ca4fb0 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -57,14 +57,16 @@ func init() { // Read in config file and ENV variables if set. func initConfig() { - 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 + // Checking for the cfgFile after setting default paths. + if cfgFile != "" { // enable ability to specify config file via flag + viper.SetConfigFile(cfgFile) + } + // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed())