Skip to content

Commit

Permalink
cmd/kf: add --yaml flag to record show
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed May 9, 2024
1 parent 114eba3 commit 9886b03
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions cmd/kf/internal/cmdrecord/cmdrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/creachadair/keyfish/cmd/kf/config"
"github.com/creachadair/keyfish/kfdb"
"github.com/creachadair/keyfish/kflib"
yaml "gopkg.in/yaml.v3"
)

var Command = &command.C{
Expand Down Expand Up @@ -98,7 +99,8 @@ func runRecordAdd(env *command.Env, label string) error {
}

var showFlags struct {
All bool `flag:"a,Show all fields including secrets"`
All bool `flag:"a,Show all fields including secrets"`
YAML bool `flag:"yaml,Show value as YAML instead of JSON"`
}

// runRecordShow implements the "record show" subcommand.
Expand Down Expand Up @@ -130,12 +132,20 @@ func runRecordShow(env *command.Env, query string) error {
}
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(struct {
Q string `json:"query"`
I int `json:"index"`
R *kfdb.Record `json:"record"`
var encode func(any) error
if showFlags.YAML {
enc := yaml.NewEncoder(os.Stdout)
enc.SetIndent(3)
encode = yaml.NewEncoder(os.Stdout).Encode
} else {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
encode = enc.Encode
}
encode(struct {
Q string `json:"query" yaml:"query"`
I int `json:"index" yaml:"index"`
R *kfdb.Record `json:"record" yaml:"record"`
}{
Q: query,
I: res.Index,
Expand Down

0 comments on commit 9886b03

Please sign in to comment.