Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Nov 2, 2020
1 parent e9b23ba commit 71d4efd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
11 changes: 5 additions & 6 deletions _example/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ type serverCmd struct {
}

func (s serverCmd) Run(app *kong.Context, g *Globals, l *zap.SugaredLogger, reg *prometheus.Registry) error {
l.Infow("starting server", king.FlagMap(app).Rm(
l.Infow("starting server", king.FlagMap(app, regexp.MustCompile("token"), regexp.MustCompile("pw")).Rm(
"help", "version",
).Register(
app.Model.Name, reg,
).Redact(
regexp.MustCompile("token"),
regexp.MustCompile("pw"),
).List()...)

r := chi.NewRouter()
Expand All @@ -59,8 +56,10 @@ func (s serverCmd) Run(app *kong.Context, g *Globals, l *zap.SugaredLogger, reg

// Globals are the gloabal flags.
type Globals struct {
Debug bool `help:"enable debug output"`
Profiler profilerFlags `embed:"" prefix:"profiler-"`
Debug bool `help:"enable debug output"`
Profiler profilerFlags `embed:"" prefix:"profiler-"`
Version king.VersionFlag `help:"Show version information"`
Show king.ShowConfig `help:"Show config file used"`
}

type profilerFlags struct {
Expand Down
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
commit = "123456789"
)

const appName = "daeira"
const appName = "example"

func main() {
cli := cmd.CLI{}
Expand Down
8 changes: 5 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (
type ShowConfig bool

// BeforeApply is the actual show-config command.
func (s ShowConfig) BeforeApply(k *kong.Kong, vars kong.Vars) error {
fmt.Fprintln(k.Stderr, "Configuration files:")
w := tabwriter.NewWriter(k.Stderr, 0, 0, 1, ' ', 0)
func (s ShowConfig) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Fprintln(app.Stderr, "Configuration files:")
w := tabwriter.NewWriter(app.Stderr, 0, 0, 1, ' ', 0)

for _, file := range Configs(vars) {
filePath := kong.ExpandPath(file)
Expand All @@ -35,6 +35,8 @@ func (s ShowConfig) BeforeApply(k *kong.Kong, vars kong.Vars) error {

w.Flush()

app.Exit(0)

return nil
}

Expand Down
13 changes: 9 additions & 4 deletions king.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ func DefaultOptions(c Config) []kong.Option {
type Map map[string]interface{}

// FlagMap returns the flags and corresponding values from *kong.Context.
func FlagMap(ctx *kong.Context) Map {
//
// To prevent logging sensitive flag values it is possible to provide
// a list of regular expressions. Flag values of flag names that match are
// redacted by '*'.
func FlagMap(ctx *kong.Context, redactFlags ...*regexp.Regexp) Map {
m := Map{}
for _, f := range ctx.Flags() {
m[f.Name] = ctx.FlagValue(f)
}

m = m.redact(redactFlags...)

b := newBuildInfo("king_", ctx.Model.Vars())
if b != nil {
m[buildInfoKey] = b
Expand All @@ -85,9 +91,8 @@ func FlagMap(ctx *kong.Context) Map {
return m
}

// Redact redacts values get with '*', for keys that match a keyRegexp
// regular expression. This can be useful for redacting passwords, keys etc...
func (m Map) Redact(keyRegexp ...*regexp.Regexp) Map {
func (m Map) redact(keyRegexp ...*regexp.Regexp) Map {

r := redactor(keyRegexp)
nm := Map{}

Expand Down
8 changes: 2 additions & 6 deletions king_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestFlagMap(t *testing.T) {
ctx, err := parser.Parse([]string{"--help", "--override-config=will_be_redacted"})
require.NoError(t, err)

l := king.FlagMap(ctx).Rm("override-auto-env", "help").Add("version", "1.0", "commit", "123456789", "not_added").Redact(regexp.MustCompile("override-config")).List()
l := king.FlagMap(ctx, regexp.MustCompile("override-config")).Rm("override-auto-env", "help").Add("version", "1.0", "commit", "123456789", "not_added").List()
expected := []interface{}{
"commit",
"123456789",
Expand All @@ -217,7 +217,7 @@ func TestFlagMap(t *testing.T) {
assert.Equal(t, expected, l)

reg := prometheus.NewRegistry()
king.FlagMap(ctx).Add("pw", "123").Add("bool-flag", "true").Redact(regexp.MustCompile("pw")).Rm("help").Register("program", reg)
king.FlagMap(ctx, regexp.MustCompile("override-config")).Add("bool-flag", "true").Rm("help").Register("program", reg)

a, err := reg.Gather()
require.NoError(t, err)
Expand All @@ -236,11 +236,7 @@ func TestFlagMap(t *testing.T) {
`name:"name" value:"bool-flag"`,
`name:"program" value:"program"`,
`name:"value" value:"true"`,
`name:"name" value:"override-config"`,
`name:"program" value:"program"`,
`name:"value" value:"will_be_redacted"`,
}

sort.Strings(expectedLabels)
sort.Strings(labels)

Expand Down

0 comments on commit 71d4efd

Please sign in to comment.