-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(cmd/influxd): parse log-level CLI opts directly to correct type #20196
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/pflag" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type levelValue zapcore.Level | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this definition just a way to declare methods on the zapcore.Level type? Nice trick! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I think) yes, specifically so we can redefine methods like |
||
|
||
func newLevelValue(val zapcore.Level, p *zapcore.Level) *levelValue { | ||
*p = val | ||
return (*levelValue)(p) | ||
} | ||
|
||
func (l *levelValue) String() string { | ||
return zapcore.Level(*l).String() | ||
} | ||
func (l *levelValue) Set(s string) error { | ||
var level zapcore.Level | ||
if err := level.Set(s); err != nil { | ||
return fmt.Errorf("unknown log level; supported levels are debug, info, warn, error") | ||
} | ||
*l = levelValue(level) | ||
return nil | ||
} | ||
|
||
func (l *levelValue) Type() string { | ||
return "Log-Level" | ||
} | ||
|
||
// LevelVar defines a zapcore.Level flag with specified name, default value, and usage string. | ||
// The argument p points to a zapcore.Level variable in which to store the value of the flag. | ||
func LevelVar(fs *pflag.FlagSet, p *zapcore.Level, name string, value zapcore.Level, usage string) { | ||
LevelVarP(fs, p, name, "", value, usage) | ||
} | ||
|
||
// LevelVarP is like LevelVar, but accepts a shorthand letter that can be used after a single dash. | ||
func LevelVarP(fs *pflag.FlagSet, p *zapcore.Level, name, shorthand string, value zapcore.Level, usage string) { | ||
fs.VarP(newLevelValue(value, p), name, shorthand, usage) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Opt is a single command-line option | ||
|
@@ -274,6 +275,19 @@ func BindOptions(v *viper.Viper, cmd *cobra.Command, opts []Opt) { | |
if s := v.GetString(envVar); s != "" { | ||
_ = (*destP).DecodeFromString(v.GetString(envVar)) | ||
} | ||
case *zapcore.Level: | ||
var l zapcore.Level | ||
if o.Default != nil { | ||
l = o.Default.(zapcore.Level) | ||
} | ||
if hasShort { | ||
LevelVarP(flagset, destP, o.Flag, string(o.Short), l, o.Desc) | ||
} else { | ||
LevelVar(flagset, destP, o.Flag, l, o.Desc) | ||
} | ||
if s := v.GetString(envVar); s != "" { | ||
_ = (*destP).Set(v.GetString(envVar)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block is mostly boilerplate, following the same pattern as the preceding |
||
default: | ||
// if you get a panic here, sorry about that! | ||
// anyway, go ahead and make a PR and add another type. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This boilerplate file is pretty much identical to
kit/cli/idflag.go
, but withzapcore.Level
instead ofinfluxdb.ID
.