diff --git a/commands/describe/broker.go b/commands/describe/broker.go index e04cc86..370c053 100644 --- a/commands/describe/broker.go +++ b/commands/describe/broker.go @@ -1,7 +1,6 @@ package describe import ( - "encoding/json" "fmt" "regexp" "sort" @@ -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: @@ -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() diff --git a/commands/describe/cluster.go b/commands/describe/cluster.go index a4ddbbf..c2c97da 100644 --- a/commands/describe/cluster.go +++ b/commands/describe/cluster.go @@ -1,7 +1,6 @@ package describe import ( - "encoding/json" "errors" "fmt" "sort" @@ -9,7 +8,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/tabular" @@ -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: @@ -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 -} diff --git a/commands/describe/group.go b/commands/describe/group.go index 53a4e7c..a19f5e6 100644 --- a/commands/describe/group.go +++ b/commands/describe/group.go @@ -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" @@ -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: @@ -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, diff --git a/commands/describe/topic.go b/commands/describe/topic.go index 04dab0d..71b8a44 100644 --- a/commands/describe/topic.go +++ b/commands/describe/topic.go @@ -2,7 +2,6 @@ package describe import ( "bytes" - "encoding/json" "fmt" "sort" "strings" @@ -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" @@ -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: @@ -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 diff --git a/commands/list/group_offsets.go b/commands/list/group_offsets.go index aa372f7..3bbf63d 100644 --- a/commands/list/group_offsets.go +++ b/commands/list/group_offsets.go @@ -1,7 +1,6 @@ package list import ( - "encoding/json" "fmt" "regexp" "strconv" @@ -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" @@ -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: @@ -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 -} diff --git a/commands/list/groups.go b/commands/list/groups.go index 3fa9d37..41b9791 100644 --- a/commands/list/groups.go +++ b/commands/list/groups.go @@ -1,7 +1,6 @@ package list import ( - "encoding/json" "fmt" "regexp" "sort" @@ -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: @@ -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 -} diff --git a/commands/list/list.go b/commands/list/list.go index bed3b35..c2ab934 100644 --- a/commands/list/list.go +++ b/commands/list/list.go @@ -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) { @@ -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) -} diff --git a/commands/list/local_offsets.go b/commands/list/local_offsets.go index 311419d..bae569b 100644 --- a/commands/list/local_offsets.go +++ b/commands/list/local_offsets.go @@ -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" @@ -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: @@ -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 -} diff --git a/commands/list/local_topics.go b/commands/list/local_topics.go index a1cf723..40a76d4 100644 --- a/commands/list/local_topics.go +++ b/commands/list/local_topics.go @@ -1,7 +1,6 @@ package list import ( - "encoding/json" "fmt" "regexp" "sort" @@ -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: @@ -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 -} diff --git a/commands/list/topics.go b/commands/list/topics.go index 525dc25..ac8496d 100644 --- a/commands/list/topics.go +++ b/commands/list/topics.go @@ -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" @@ -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 @@ -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), @@ -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 } diff --git a/internal/output/output.go b/internal/output/output.go index 9ac22f9..b3f8b89 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -1,7 +1,10 @@ package output import ( + "encoding/json" "fmt" + + "github.com/xitonix/trubka/internal" ) func NewLines(count int) { @@ -9,3 +12,13 @@ func NewLines(count int) { fmt.Println() } } + +func PrintAsJson(data interface{}, style string, enableColor bool) error { + result, err := json.MarshalIndent(data, "", " ") + if err != nil { + return err + } + h := internal.NewJsonHighlighter(style, enableColor) + fmt.Println(string(h.Highlight(result))) + return nil +} diff --git a/kafka/topic.go b/kafka/topic.go index c09d1bb..a859835 100644 --- a/kafka/topic.go +++ b/kafka/topic.go @@ -3,9 +3,9 @@ package kafka import "fmt" type Topic struct { - Name string - NumberOfPartitions int32 - ReplicationFactor int16 + Name string `json:"name"` + NumberOfPartitions int32 `json:"number_of_partitions"` + ReplicationFactor int16 `json:"replication_factor"` } type TopicsByName []Topic