Skip to content

Commit bff2a30

Browse files
committed
consume: print headers if --raw is not set
1 parent f858639 commit bff2a30

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cmd/kaf/consume.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package main
22

33
import (
4+
"encoding/binary"
45
"fmt"
56
"os"
67
"sync"
78

9+
"strconv"
10+
11+
"text/tabwriter"
12+
813
"github.com/birdayz/sarama"
914
"github.com/hokaccha/go-prettyjson"
1015
"github.com/spf13/cobra"
@@ -97,6 +102,30 @@ var consumeCmd = &cobra.Command{
97102
if err == nil {
98103
dataToDisplay = formatted
99104
}
105+
106+
w := tabwriter.NewWriter(os.Stderr, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)
107+
108+
if len(msg.Headers) > 0 {
109+
fmt.Fprintf(w, "Headers:\n")
110+
}
111+
112+
for _, hdr := range msg.Headers {
113+
var hdrValue string
114+
// Try to detect azure eventhub-specific encoding
115+
switch hdr.Value[0] {
116+
case 161:
117+
hdrValue = string(hdr.Value[2 : 2+hdr.Value[1]])
118+
case 131:
119+
hdrValue = strconv.FormatUint(binary.BigEndian.Uint64(hdr.Value[1:9]), 10)
120+
default:
121+
hdrValue = string(hdr.Value)
122+
}
123+
124+
fmt.Fprintf(w, "\tKey: %v\tValue:%v\n", string(hdr.Key), hdrValue)
125+
126+
}
127+
w.Flush()
128+
100129
}
101130

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

0 commit comments

Comments
 (0)