From cb9cf9469070d9291b2023105297e489189f0865 Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Sun, 30 Jul 2017 01:19:17 -0600 Subject: [PATCH] Restore the use of cobra.OnInitialize in cobra executable Without cobra.OnInitialize, cobra does not see the --config command-line option and always tries to load $HOME/.cobra.yaml instead. --- cobra/cmd/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 1c5e690..19568f9 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -40,7 +40,7 @@ func Execute() { } func init() { - initViper() + cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") @@ -55,7 +55,7 @@ func init() { rootCmd.AddCommand(initCmd) } -func initViper() { +func initConfig() { if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile)