From dcda1bbeece113b015f25c08b481171f67dae84e Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 3 Sep 2019 12:30:53 +0200 Subject: [PATCH] initialize logging --- cmd/create.go | 13 ++++--------- cmd/delete.go | 4 ++-- cmd/get.go | 4 ++-- cmd/root.go | 25 +++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index f6ece80f..7c43e4a5 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -22,7 +22,7 @@ THE SOFTWARE. package cmd import ( - "fmt" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -30,15 +30,10 @@ import ( // createCmd represents the create command var createCmd = &cobra.Command{ Use: "create", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Create a resource.", + Long: `Create a resource.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("create called") + log.Debugln("create called") }, } diff --git a/cmd/delete.go b/cmd/delete.go index 8b9cb4da..1b72bdb8 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -22,7 +22,7 @@ THE SOFTWARE. package cmd import ( - "fmt" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -38,7 +38,7 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("delete called") + log.Debugln("delete called") }, } diff --git a/cmd/get.go b/cmd/get.go index a443d3ce..e115f565 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -22,7 +22,7 @@ THE SOFTWARE. package cmd import ( - "fmt" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -38,7 +38,7 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("get called") + log.Debugln("get called") }, } diff --git a/cmd/root.go b/cmd/root.go index 7e8d813e..3dc65716 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,8 +22,10 @@ THE SOFTWARE. package cmd import ( - "github.com/spf13/cobra" "os" + "strings" + + "github.com/spf13/cobra" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" @@ -32,6 +34,7 @@ import ( ) var cfgFile string +var debugLogging bool // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -53,16 +56,18 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) + cobra.OnInitialize(initLogging) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, // will be global for your application. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k3d.yaml)") + rootCmd.PersistentFlags().BoolVar(&debugLogging, "verbose", false, "Enable verbose output (debug logging)") // Cobra also supports local flags, which will only run // when this action is called directly. - rootCmd.Flags().BoolP("verbose", "v", false, "Enable verbose output (debug level logs)") + // rootCmd.Flags().BoolP("nope", "n", false, "nope description") } // initConfig reads in config file and ENV variables if set. @@ -90,3 +95,19 @@ func initConfig() { log.Debugln("Using config file:", viper.ConfigFileUsed()) } } + +// initLogging initializes the logger +func initLogging() { + if debugLogging { + log.SetLevel(log.DebugLevel) + } else { + switch logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL")); logLevel { + case "DEBUG": + log.SetLevel(log.DebugLevel) + case "WARN": + log.SetLevel(log.WarnLevel) + default: + log.SetLevel(log.InfoLevel) + } + } +}