From e8f6b8e03d3781bcda5d0d27766b502a6adc6a66 Mon Sep 17 00:00:00 2001 From: Tim Park Date: Thu, 17 Jan 2019 10:24:46 -0800 Subject: [PATCH] Switch on flags in PersistentPreRunE where they have parsed values (#14) --- cmd/root.go | 26 ++++++++++++++------------ main.go | 1 - 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4565f5e..19b9fca 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" homedir "github.com/mitchellh/go-homedir" @@ -17,22 +16,25 @@ var rootCmd = &cobra.Command{ Use: "fab", Short: "Scalable GitOps for Kubernetes clusters", Long: "Scalable GitOps for Kubernetes clusters", - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + + PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { + verbose := cmd.Flag("verbose").Value.String() + + if verbose == "true" { + log.SetLevel(log.DebugLevel) + } else { + log.SetLevel(log.InfoLevel) + } + + return nil + }, } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - verbose, _ := rootCmd.PersistentFlags().GetBool("verbose") - - if verbose { - log.SetLevel(log.DebugLevel) - } - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + log.Error(err) os.Exit(1) } } @@ -40,7 +42,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().Bool("verbose", true, "Verbose output logs") + rootCmd.PersistentFlags().Bool("verbose", false, "Use verbose output logs") } // initConfig reads in config file and ENV variables if set. diff --git a/main.go b/main.go index 7bfcd15..b170d5a 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ func main() { formatter.FullTimestamp = true log.SetFormatter(formatter) - log.SetLevel(log.InfoLevel) cmd.Execute() }