Skip to content

Commit

Permalink
List local 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 c318403 commit 6f59427
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions commands/list/local_topics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package list

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

switch l.format {
case commands.ListFormat:
l.printAsList(localStore, false)
case commands.JsonFormat:
return l.printAsJson(localStore)
case commands.TableFormat:
l.printAsTable(localStore)
return l.printAsTable(localStore)
case commands.ListFormat:
return l.printAsList(localStore, false)
case commands.PlainTextFormat:
l.printAsList(localStore, true)
return l.printAsList(localStore, true)
}
return nil
}

func (l *listLocalTopics) printAsTable(store map[string][]string) {
func (l *listLocalTopics) printAsTable(store map[string][]string) error {
for env, topics := range store {
table := tabular.NewTable(l.globalParams.EnableColor, tabular.C(format.WithCount(env, len(topics))).Align(tabular.AlignLeft).MinWidth(60))
sort.Strings(topics)
Expand All @@ -73,9 +76,10 @@ func (l *listLocalTopics) printAsTable(store map[string][]string) {
table.Render()
output.NewLines(1)
}
return nil
}

func (l *listLocalTopics) printAsList(store map[string][]string, plain bool) {
func (l *listLocalTopics) printAsList(store map[string][]string, plain bool) error {
b := list.New(plain)
b.AsTree()
for env, topics := range store {
Expand All @@ -88,4 +92,15 @@ func (l *listLocalTopics) printAsList(store map[string][]string, plain bool) {
b.UnIntend()
}
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
}

0 comments on commit 6f59427

Please sign in to comment.