Skip to content

Commit

Permalink
[misc] add custom verbosity level parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
shmel1k committed Sep 16, 2024
1 parent 377278b commit b890bb1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
15 changes: 2 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/ydb-platform/ydbops/cmd/maintenance"
"github.com/ydb-platform/ydbops/cmd/restart"
Expand Down Expand Up @@ -58,19 +57,9 @@ func NewRootCommand(
return err
}

logLevel := "info"
if roptions.Verbose {
logLevel = "debug"
}

lvc, err := zapcore.ParseLevel(logLevel)
if err != nil {
logger.Warn("Failed to set level")
return err
}
logLevelSetter.SetLevel(lvc)
logLevelSetter.SetLevel(roptions.VerbosityLevel.Level())

zap.S().Debugf("Current logging level enabled: %s", logLevel)
zap.S().Debugf("Current logging level enabled: %s", roptions.VerbosityLevel.Level())
return nil
},
RunE: cli.RequireSubcommand,
Expand Down
14 changes: 8 additions & 6 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type Description struct {
}

type BaseOptions struct {
Auth options.AuthOptions
GRPC options.GRPC
Verbose bool
ProfileFile string
ActiveProfile string
Auth options.AuthOptions
GRPC options.GRPC
VerbosityLevel VerbosityLevel
ProfileFile string
ActiveProfile string
}

func (o *BaseOptions) Validate() error {
Expand Down Expand Up @@ -61,7 +61,9 @@ func (o *BaseOptions) DefineFlags(fs *pflag.FlagSet) {
defaultProfileLocation,
"Path to config file with profile data in yaml format. Default: $HOME/ydb/ydbops/config/config.yaml")

fs.BoolVar(&o.Verbose, "verbose", false, "Switches log level from INFO to DEBUG")
o.VerbosityLevel = newVerbosityLevel()
fl := fs.VarPF(&o.VerbosityLevel, "verbose", "v", "Switches log level from INFO to DEBUG")
fl.NoOptDefVal = "false"

Check failure on line 66 in pkg/command/command.go

View workflow job for this annotation

GitHub Actions / run-lint / golangci-lint

string `false` has 2 occurrences, make it a constant (goconst)
}

func NewDescription(use, shortDescription, longDescription string) *Description {
Expand Down
34 changes: 34 additions & 0 deletions pkg/command/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package command

import (
"go.uber.org/zap/zapcore"
)

type VerbosityLevel struct {
level zapcore.Level
}

func (v *VerbosityLevel) String() string {
return "false"
}

func (v *VerbosityLevel) Set(_ string) error {
if v.level != zapcore.DebugLevel {
v.level--
}
return nil
}

func (v *VerbosityLevel) Type() string {
return "bool"
}

func (v *VerbosityLevel) Level() zapcore.Level {
return v.level
}

func newVerbosityLevel() VerbosityLevel {
return VerbosityLevel{
level: zapcore.WarnLevel,
}
}

0 comments on commit b890bb1

Please sign in to comment.