From b6644fbe66ee5dc827ffa38293aeca5cadef428a Mon Sep 17 00:00:00 2001 From: Lluis Campos Date: Wed, 10 Jan 2024 20:30:48 +0100 Subject: [PATCH] feat: Add `--log-level` CLI flag and restore debug messages Previously the user had no control on what to log. This commit implements (re-implements?) the `--log-level` flag and partially restores the messages removed at 6be7792. Changelog: Title Ticket: None Signed-off-by: Lluis Campos --- cli/cli.go | 26 ++++++++++++++++++++++++-- cli/setup.go | 4 ++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index be3ab3c..a357e78 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -53,6 +53,7 @@ type runOptionsType struct { dataStore string conf.HttpConfig setupOptions setupOptionsType // Options for setup subcommand + logOptions logOptionsType // Options for logging } func ShowVersion() string { @@ -165,6 +166,13 @@ func SetupCLI(args []string) error { Name: "quiet", Usage: "Suppress informative prompts.", }, + &cli.StringFlag{ + Name: "log-level", + Aliases: []string{"l"}, + Usage: "Set logging `level`.", + Value: "warning", + Destination: &runOptions.logOptions.logLevel, + }, }, } @@ -178,6 +186,8 @@ func SetupCLI(args []string) error { func (runOptions *runOptionsType) commonCLIHandler( ctx *cli.Context) (*conf.MenderConfig, error) { + log.Debug("commonCLIHandler config file: ", runOptions.config) + // Handle config flags config, err := conf.LoadConfig( runOptions.config, runOptions.fallbackConfig) @@ -221,9 +231,11 @@ func (runOptions *runOptionsType) handleCLIOptions(ctx *cli.Context) error { // Check that user has permission to directories so that // the user doesn't have to perform the setup before raising // an error. + log.Debug("handleCLIOptions config file: ", runOptions.config) if err = checkWritePermissions(path.Dir(runOptions.config)); err != nil { return err } + log.Debug("handleCLIOptions dataStore file: ", runOptions.dataStore) if err = checkWritePermissions(runOptions.dataStore); err != nil { return err } @@ -245,9 +257,18 @@ func (runOptions *runOptionsType) setupCLIHandler(ctx *cli.Context) error { errMsgAmbiguousArgumentsGivenF, ctx.Args().First()) } - if !ctx.IsSet("log-level") { - log.SetLevel(log.WarnLevel) + + if ctx.Bool("quiet") { + log.SetLevel(log.ErrorLevel) + } else { + if lvl, err := log.ParseLevel(ctx.String("log-level")); err == nil { + log.SetLevel(lvl) + } else { + log.Warnf( + "Failed to parse set log level '%s'.", ctx.String("log-level")) + } } + if err := runOptions.setupOptions.handleImplicitFlags(ctx); err != nil { return err } @@ -330,6 +351,7 @@ func upgradeHelpPrinter(defaultPrinter func(w io.Writer, templ string, data inte } func checkWritePermissions(dir string) error { + log.Debug("Checking the permissions for: ", dir) _, err := os.Stat(dir) if os.IsNotExist(err) { err := os.MkdirAll(dir, 0755) diff --git a/cli/setup.go b/cli/setup.go index e981348..c40560e 100644 --- a/cli/setup.go +++ b/cli/setup.go @@ -54,6 +54,10 @@ type setupOptionsType struct { demoIntervals bool } +type logOptionsType struct { + logLevel string +} + // ------------------------------ Setup constants ------------------------------ const ( // state enum stateDeviceType = iota