From d42f41b7246a5f334d60d01138d0b7beb7a10011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20Crespo?= Date: Thu, 2 May 2024 05:03:10 +0000 Subject: [PATCH] Validate logger verbosity --- hack/ccp/internal/rootcmd/cmd.go | 14 ++++++++++++++ hack/ccp/main.go | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/hack/ccp/internal/rootcmd/cmd.go b/hack/ccp/internal/rootcmd/cmd.go index 7ab060bca..435638384 100644 --- a/hack/ccp/internal/rootcmd/cmd.go +++ b/hack/ccp/internal/rootcmd/cmd.go @@ -26,6 +26,20 @@ func (c *Config) RegisterFlags(fs *flag.FlagSet) { fs.BoolVar(&c.Debug, "debug", false, "Enable debug mode") } +func (c *Config) Validate() error { + const ( + minVerbosity = 0 + maxVerbosity = 10 + ) + if c.Verbosity < minVerbosity { + c.Verbosity = minVerbosity + } else if c.Verbosity > maxVerbosity { + c.Verbosity = maxVerbosity + } + + return nil +} + func (c *Config) Exec(context.Context, []string) error { return flag.ErrHelp } diff --git a/hack/ccp/main.go b/hack/ccp/main.go index 9f73f97ed..6c869b1d0 100644 --- a/hack/ccp/main.go +++ b/hack/ccp/main.go @@ -24,6 +24,11 @@ func main() { os.Exit(1) } + if err := rootConfig.Validate(); err != nil { + fmt.Fprintf(os.Stderr, "error during Validate: %v\n", err) + os.Exit(1) + } + if err := rootCommand.Run(context.Background()); err != nil { os.Exit(1) }