diff --git a/pkg/command/root.go b/pkg/command/root.go index 87243dc0d..6fea9cb44 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -33,6 +33,9 @@ import ( "github.com/vmware-tanzu/tanzu-plugin-runtime/plugin" ) +// verbose flag to set the log lovel +var verbose int + // NewRootCmd creates a root command. func NewRootCmd() (*cobra.Command, error) { //nolint: gocyclo,funlen var rootCmd = newRootCmd() @@ -42,6 +45,9 @@ func NewRootCmd() (*cobra.Command, error) { //nolint: gocyclo,funlen // Configure defined environment variables found in the config file cliconfig.ConfigureEnvVariables() + // Initialize the global verbose flag to set the log level verbosity (acceptable values 1 - 9) (default value 1) + rootCmd.PersistentFlags().IntVar(&verbose, "verbose", 1, "number for the log level verbosity (default value 1) (acceptable values 1 - 9)") + rootCmd.AddCommand( newVersionCmd(), newPluginCmd(), @@ -58,6 +64,7 @@ func NewRootCmd() (*cobra.Command, error) { //nolint: gocyclo,funlen newCEIPParticipationCmd(), newGenAllDocsCmd(), ) + if _, err := ensureCLIInstanceID(); err != nil { return nil, errors.Wrap(err, "failed to ensure CLI ID") } @@ -240,8 +247,41 @@ func newRootCmd() *cobra.Command { // silencing usage for now as we are getting double usage from plugins on errors SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - // Sets the verbosity of the logger if TANZU_CLI_LOG_LEVEL is set - setLoggerVerbosity() + // Manually parse the flags + for i := 0; i < len(args); i++ { + arg := args[i] + if arg == "-v" || arg == "--verbose" { + if i+1 < len(args) { + nextArg := args[i+1] + // Check if the next argument is another flag + if strings.HasPrefix(nextArg, "-") { + log.Errorf("Missing value for verbose flag") + } + // Try to convert the next argument to an integer + if v, err := strconv.Atoi(nextArg); err == nil { + verbose = v + } else { + log.Errorf("Invalid value for verbose flag: %v\n", err) + } + // Skip the next argument + i++ + } else { + log.Errorf("Missing value for verbose flag") + } + } + } + + // Sets the verbosity of the logger + setLoggerVerbosity(verbose) + + //TODO: Remove test logs + log.Info("I am level default") + log.V(1).Info("I am level 1") + log.V(2).Info("I am level 2") + log.V(3).Info("I am level 3") + log.V(4).Info("I am level 4") + log.V(5).Info("I am level 5") + log.V(6).Info("I am level 6") // Ensure mutual exclusion in current contexts just in case if any plugins with old // plugin-runtime sets k8s context as current when tanzu context is already set as current @@ -291,13 +331,22 @@ func newRootCmd() *cobra.Command { } // setLoggerVerbosity sets the verbosity of the logger if TANZU_CLI_LOG_LEVEL is set -func setLoggerVerbosity() { - // Configure the log level if env variable TANZU_CLI_LOG_LEVEL is set - logLevel := os.Getenv(log.EnvTanzuCLILogLevel) - if logLevel != "" { - logValue, err := strconv.ParseInt(logLevel, 10, 32) - if err == nil { - log.SetVerbosity(int32(logValue)) +func setLoggerVerbosity(verbosity int) { + // If verbose global flag is passed set the log level verbosity if not then check if env variable TANZU_CLI_LOG_LEVEL is set + if verbosity > 0 { + // Set the log level verbosity with the verbosity value + log.SetVerbosity(int32(verbosity)) + + // Set the TANZU_CLI_LOG_LEVEL env with the verbosity value + _ = os.Setenv(log.EnvTanzuCLILogLevel, strconv.Itoa(verbosity)) + } else { + // Configure the log level if env variable TANZU_CLI_LOG_LEVEL is set + logLevel := os.Getenv(log.EnvTanzuCLILogLevel) + if logLevel != "" { + logValue, err := strconv.ParseInt(logLevel, 10, 32) + if err == nil { + log.SetVerbosity(int32(logValue)) + } } } }