Skip to content

Commit

Permalink
pretty-print only if --pretty is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
birdayz committed Nov 20, 2018
1 parent e2f4052 commit d8f8073
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cmd/kaf/consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"github.com/spf13/cobra"
)

var offsetFlag string
var (
offsetFlag string
prettyPrint bool
)

func init() {
rootCmd.AddCommand(consumeCmd)
consumeCmd.Flags().StringVar(&offsetFlag, "offset", "oldest", "Offset to start consuming. Possible values: oldest, newest. Default: newest")
consumeCmd.Flags().BoolVar(&prettyPrint, "pretty", false, "Pretty print output if possible, e.g. for JSON.")
}

var consumeCmd = &cobra.Command{
Expand Down Expand Up @@ -65,9 +69,12 @@ var consumeCmd = &cobra.Command{
for msg := range pc.Messages() {

dataToDisplay := msg.Value
formatted, err := prettyjson.Format(msg.Value)
if err == nil {
dataToDisplay = formatted

if prettyPrint {
formatted, err := prettyjson.Format(msg.Value)
if err == nil {
dataToDisplay = formatted
}
}

if msg.Key != nil && len(msg.Key) > 0 {
Expand Down

0 comments on commit d8f8073

Please sign in to comment.