Skip to content

Commit

Permalink
Adds -json flag to format output in JSON format.
Browse files Browse the repository at this point in the history
For compatibility with github.com/echojc/kp

Fixes #7.
  • Loading branch information
fgeller committed Feb 2, 2016
1 parent 46ce2f4 commit 8e0e1ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
Usage of kt:
-brokers string
Comma separated list of brokers. (default "localhost:9092")
-json
Print output in JSON format.
-offset string
Colon separated offsets where to start and end reading messages.
-topic string
Topic to consume.
$ kt -topic kt-test
Partition=0 Offset=0 Key= Value=Hello, World 0
Partition=0 Offset=1 Key= Value=Hallo, Welt
Partition=0 Offset=2 Key= Value=Bonjour, monde
Partition=0 Offset=0 Key= Message=Hello, World 0
Partition=0 Offset=1 Key= Message=Hallo, Welt
Partition=0 Offset=2 Key= Message=Bonjour, monde
^C2016/01/26 06:29:19 Received interrupt - shutting down...
$ kt -topic kt-test -offset 1:
Partition=0 Offset=1 Key= Value=Hallo, Welt
Partition=0 Offset=2 Key= Value=Bonjour, monde
Partition=0 Offset=1 Key= Message=Hallo, Welt
Partition=0 Offset=2 Key= Message=Bonjour, monde
^C2016/01/26 06:29:29 Received interrupt - shutting down...
$ kt -topic kt-test -offset 1:1
Partition=0 Offset=1 Key= Value=Hallo, Welt
Partition=0 Offset=1 Key= Message=Hallo, Welt
$

## Installation
Expand Down
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var config struct {
brokers []string
startOffset int64
endOffset int64
jsonOutput bool
}

func listenForInterrupt() chan struct{} {
Expand All @@ -34,8 +35,22 @@ func listenForInterrupt() chan struct{} {
}

func print(msg *sarama.ConsumerMessage) {

if config.jsonOutput {
fmt.Printf(
`{"partition":%v,"offset":%v,"key":%#v,"message":%#v}
`,
msg.Partition,
msg.Offset,
string(msg.Key),
string(msg.Value),
)

return
}

fmt.Printf(
"Partition=%v Offset=%v Key=%s Value=%s\n",
"Partition=%v Offset=%v Key=%s Message=%s\n",
msg.Partition,
msg.Offset,
msg.Key,
Expand All @@ -59,6 +74,7 @@ func parseArgs() {
flag.StringVar(&config.topic, "topic", "", "Topic to consume.")
flag.StringVar(&brokersString, "brokers", "localhost:9092", "Comma separated list of brokers.")
flag.StringVar(&offset, "offset", "", "Colon separated offsets where to start and end reading messages.")
flag.BoolVar(&config.jsonOutput, "json", false, "Print output in JSON format.")

flag.Parse()

Expand Down

0 comments on commit 8e0e1ba

Please sign in to comment.