Skip to content

Commit

Permalink
consume: print headers if --raw is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
birdayz committed Nov 22, 2018
1 parent f858639 commit bff2a30
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/kaf/consume.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"encoding/binary"
"fmt"
"os"
"sync"

"strconv"

"text/tabwriter"

"github.com/birdayz/sarama"
"github.com/hokaccha/go-prettyjson"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,6 +102,30 @@ var consumeCmd = &cobra.Command{
if err == nil {
dataToDisplay = formatted
}

w := tabwriter.NewWriter(os.Stderr, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)

if len(msg.Headers) > 0 {
fmt.Fprintf(w, "Headers:\n")
}

for _, hdr := range msg.Headers {
var hdrValue string
// Try to detect azure eventhub-specific encoding
switch hdr.Value[0] {
case 161:
hdrValue = string(hdr.Value[2 : 2+hdr.Value[1]])
case 131:
hdrValue = strconv.FormatUint(binary.BigEndian.Uint64(hdr.Value[1:9]), 10)
default:
hdrValue = string(hdr.Value)
}

fmt.Fprintf(w, "\tKey: %v\tValue:%v\n", string(hdr.Key), hdrValue)

}
w.Flush()

}

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

0 comments on commit bff2a30

Please sign in to comment.