Skip to content

Commit

Permalink
Validate logger verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed May 2, 2024
1 parent 03ecd69 commit d42f41b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions hack/ccp/internal/rootcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions hack/ccp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d42f41b

Please sign in to comment.