From 6f59427a3f2cfa37de760fb43795d890a77f587f Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 1 Jul 2020 13:39:34 +1000 Subject: [PATCH] List local topics with Json format --- commands/list/local_topics.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/commands/list/local_topics.go b/commands/list/local_topics.go index 363225d..a1cf723 100644 --- a/commands/list/local_topics.go +++ b/commands/list/local_topics.go @@ -1,6 +1,7 @@ package list import ( + "encoding/json" "fmt" "regexp" "sort" @@ -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) @@ -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 { @@ -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 }