Skip to content

Commit

Permalink
TRUB-4: List topics with json format
Browse files Browse the repository at this point in the history
  • Loading branch information
xitonix committed Jul 1, 2020
1 parent 6f59427 commit 53c639b
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 127 deletions.
14 changes: 2 additions & 12 deletions commands/describe/broker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package describe

import (
"encoding/json"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -79,7 +78,8 @@ func (b *broker) run(_ *kingpin.ParseContext) error {

switch b.format {
case commands.JsonFormat:
return b.printAsJson(meta)
data := meta.ToJson(b.includeLogs, b.includeAPIVersions, b.includeZeroLogs)
return output.PrintAsJson(data, b.style, b.globalParams.EnableColor)
case commands.TableFormat:
return b.printAsTable(meta)
case commands.ListFormat:
Expand Down Expand Up @@ -147,16 +147,6 @@ func (b *broker) printAsTable(meta *kafka.BrokerMeta) error {
return nil
}

func (b *broker) printAsJson(meta *kafka.BrokerMeta) error {
result, err := json.MarshalIndent(meta.ToJson(b.includeLogs, b.includeAPIVersions, b.includeZeroLogs), "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(b.style, b.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}

func (b *broker) printLogsTable(logs []*kafka.LogFile) error {
for _, logFile := range logs {
sorted := logFile.SortByPermanentSize()
Expand Down
14 changes: 1 addition & 13 deletions commands/describe/cluster.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package describe

import (
"encoding/json"
"errors"
"fmt"
"sort"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/tabular"
Expand Down Expand Up @@ -67,7 +65,7 @@ func (c *cluster) run(_ *kingpin.ParseContext) error {

switch c.format {
case commands.JsonFormat:
return c.printAsJson(meta)
return output.PrintAsJson(meta, c.style, c.globalParams.EnableColor)
case commands.TableFormat:
return c.printAsTable(meta)
case commands.ListFormat:
Expand Down Expand Up @@ -132,13 +130,3 @@ func (c *cluster) printAsList(meta *kafka.ClusterMetadata, plain bool) error {

return nil
}

func (c *cluster) printAsJson(meta *kafka.ClusterMetadata) error {
result, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(c.style, c.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}
15 changes: 2 additions & 13 deletions commands/describe/group.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package describe

import (
"encoding/json"
"fmt"
"strings"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
Expand Down Expand Up @@ -58,7 +56,8 @@ func (c *group) run(_ *kingpin.ParseContext) error {

switch c.format {
case commands.JsonFormat:
return c.printAsJson(cgd)
data := cgd.ToJson(c.includeMembers)
return output.PrintAsJson(data, c.style, c.globalParams.EnableColor)
case commands.TableFormat:
return c.printAsTable(cgd)
case commands.ListFormat:
Expand Down Expand Up @@ -115,16 +114,6 @@ func (c *group) printAsTable(details *kafka.ConsumerGroupDetails) error {
return nil
}

func (c *group) printAsJson(details *kafka.ConsumerGroupDetails) error {
result, err := json.MarshalIndent(details.ToJson(c.includeMembers), "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(c.style, c.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}

func (c *group) printGroupDetails(details *kafka.ConsumerGroupDetails) {
fmt.Printf(" Name: %s\n Coordinator: %s\n State: %s\n Protocol: %s\nProtocol Type: %s",
details.Name,
Expand Down
14 changes: 1 addition & 13 deletions commands/describe/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package describe

import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
Expand All @@ -11,7 +10,6 @@ import (
"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
Expand Down Expand Up @@ -73,7 +71,7 @@ func (t *topic) run(_ *kingpin.ParseContext) error {

switch t.format {
case commands.JsonFormat:
return t.printAsJson(meta)
return output.PrintAsJson(meta, t.style, t.globalParams.EnableColor)
case commands.TableFormat:
return t.printAsTable(meta)
case commands.ListFormat:
Expand Down Expand Up @@ -162,16 +160,6 @@ func (t *topic) printAsTable(meta *kafka.TopicMetadata) error {
return nil
}

func (t *topic) printAsJson(meta *kafka.TopicMetadata) error {
result, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(t.style, t.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}

func (t *topic) brokersToList(brokers ...*kafka.Broker) string {
if len(brokers) == 1 {
return brokers[0].Host
Expand Down
14 changes: 2 additions & 12 deletions commands/list/group_offsets.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package list

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
Expand All @@ -11,6 +10,7 @@ import (

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
"github.com/xitonix/trubka/internal/output/format/tabular"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (g *groupOffset) run(_ *kingpin.ParseContext) error {

switch g.format {
case commands.JsonFormat:
return g.printAsJson(topics)
return output.PrintAsJson(topics.ToJson(), g.style, g.globalParams.EnableColor)
case commands.TableFormat:
return g.printAsTable(topics)
case commands.ListFormat:
Expand Down Expand Up @@ -132,13 +132,3 @@ func (g *groupOffset) printAsList(topics kafka.TopicPartitionOffset, plain bool)
}
return nil
}

func (g *groupOffset) printAsJson(topics kafka.TopicPartitionOffset) error {
result, err := json.MarshalIndent(topics.ToJson(), "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(g.style, g.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}
21 changes: 5 additions & 16 deletions commands/list/groups.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package list

import (
"encoding/json"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -69,7 +68,11 @@ func (c *groups) run(_ *kingpin.ParseContext) error {

switch c.format {
case commands.JsonFormat:
return c.printAsJson(groups)
data := make([]interface{}, len(groups))
for i, g := range groups {
data[i] = g.ToJson(false)
}
return output.PrintAsJson(data, c.style, c.globalParams.EnableColor)
case commands.TableFormat:
return c.printAsTable(groups)
case commands.ListFormat:
Expand Down Expand Up @@ -141,17 +144,3 @@ func (c *groups) printAsTable(groups []*kafka.ConsumerGroupDetails) error {
table.Render()
return nil
}

func (c *groups) printAsJson(groups []*kafka.ConsumerGroupDetails) error {
data := make([]interface{}, len(groups))
for i, g := range groups {
data[i] = g.ToJson(false)
}
result, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(c.style, c.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}
13 changes: 0 additions & 13 deletions commands/list/list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package list

import (
"github.com/dustin/go-humanize"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
)

func AddCommands(app *kingpin.Application, global *commands.GlobalParameters, kafkaParams *commands.KafkaParameters) {
Expand All @@ -16,14 +14,3 @@ func AddCommands(app *kingpin.Application, global *commands.GlobalParameters, ka
addLocalOffsetsSubCommand(parent, global, kafkaParams)
addLocalTopicsSubCommand(parent, global)
}

func highlightLag(input int64, colorEnabled bool) interface{} {
humanised := humanize.Comma(input)
if !colorEnabled {
return humanised
}
if input > 0 {
return internal.Yellow(humanised, true)
}
return internal.Green(humanised, true)
}
15 changes: 2 additions & 13 deletions commands/list/local_offsets.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package list

import (
"encoding/json"
"fmt"

"github.com/dustin/go-humanize"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
"github.com/xitonix/trubka/internal/output/format/tabular"
Expand Down Expand Up @@ -63,7 +62,7 @@ func (l *listLocalOffsets) run(_ *kingpin.ParseContext) error {

switch l.format {
case commands.JsonFormat:
return l.printAsJson(offsets)
return output.PrintAsJson(offsets.ToJson(), l.style, l.globalParams.EnableColor)
case commands.TableFormat:
return l.printAsTable(offsets)
case commands.ListFormat:
Expand Down Expand Up @@ -119,13 +118,3 @@ func (l *listLocalOffsets) printAsList(offsets kafka.PartitionOffset, plain bool
fmt.Printf("\nTotal Lag: %v", format.Warn(totalLag, l.globalParams.EnableColor, true))
return nil
}

func (l *listLocalOffsets) printAsJson(offsets kafka.PartitionOffset) error {
result, err := json.MarshalIndent(offsets.ToJson(), "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(l.style, l.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}
13 changes: 1 addition & 12 deletions commands/list/local_topics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package list

import (
"encoding/json"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (l *listLocalTopics) run(_ *kingpin.ParseContext) error {

switch l.format {
case commands.JsonFormat:
return l.printAsJson(localStore)
return output.PrintAsJson(localStore, l.style, l.globalParams.EnableColor)
case commands.TableFormat:
return l.printAsTable(localStore)
case commands.ListFormat:
Expand Down Expand Up @@ -94,13 +93,3 @@ func (l *listLocalTopics) printAsList(store map[string][]string, plain bool) err
b.Render()
return nil
}

func (l *listLocalTopics) printAsJson(store map[string][]string) error {
result, err := json.MarshalIndent(store, "", " ")
if err != nil {
return err
}
h := internal.NewJsonHighlighter(l.style, l.globalParams.EnableColor)
fmt.Println(string(h.Highlight(result)))
return nil
}
20 changes: 13 additions & 7 deletions commands/list/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
"github.com/xitonix/trubka/internal/output/format/tabular"
Expand Down Expand Up @@ -60,17 +61,20 @@ func (c *topics) run(_ *kingpin.ParseContext) error {
sort.Sort(kafka.TopicsByName(topics))

switch c.format {
case commands.ListFormat:
c.printAsList(topics, false)
case commands.JsonFormat:
return output.PrintAsJson(topics, c.style, c.globalParams.EnableColor)
case commands.TableFormat:
c.printAsTable(topics)
return c.printAsTable(topics)
case commands.ListFormat:
return c.printAsList(topics, false)
case commands.PlainTextFormat:
c.printAsList(topics, true)
return c.printAsList(topics, true)
default:
return nil
}
return nil
}

func (c *topics) printAsList(topics []kafka.Topic, plain bool) {
func (c *topics) printAsList(topics []kafka.Topic, plain bool) error {
b := list.New(plain)
b.SetTitle(format.WithCount("Topics", len(topics)))
var totalPartitions int64
Expand All @@ -81,9 +85,10 @@ func (c *topics) printAsList(topics []kafka.Topic, plain bool) {
caption := fmt.Sprintf("SUMMARY: %s partitions in %s topics", humanize.Comma(totalPartitions), humanize.Comma(int64(len(topics))))
b.SetCaption(caption)
b.Render()
return nil
}

func (c *topics) printAsTable(topics []kafka.Topic) {
func (c *topics) printAsTable(topics []kafka.Topic) error {
table := tabular.NewTable(c.globalParams.EnableColor,
tabular.C("Topic").Align(tabular.AlignLeft),
tabular.C("Number of Partitions").FAlign(tabular.AlignCenter),
Expand All @@ -98,4 +103,5 @@ func (c *topics) printAsTable(topics []kafka.Topic) {
}
table.AddFooter(fmt.Sprintf("Total: %s", humanize.Comma(int64(len(topics)))), humanize.Comma(totalPartitions), " ")
table.Render()
return nil
}
Loading

0 comments on commit 53c639b

Please sign in to comment.