From 111cb9852987c5cc5ed526396dc630f07fab5876 Mon Sep 17 00:00:00 2001 From: Alexander Brandstedt Date: Thu, 8 Dec 2022 13:56:00 +0100 Subject: [PATCH 1/2] adding pretty print flag to topic ls to allow for easier integration with tools like jq --- cmd/kaf/topic.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/kaf/topic.go b/cmd/kaf/topic.go index 9ad07e37..8807bc5c 100644 --- a/cmd/kaf/topic.go +++ b/cmd/kaf/topic.go @@ -19,6 +19,7 @@ var ( replicasFlag int16 noHeaderFlag bool compactFlag bool + prettyJsonFlag bool ) func init() { @@ -37,6 +38,8 @@ func init() { createTopicCmd.Flags().BoolVar(&compactFlag, "compact", false, "Enable topic compaction") lsTopicsCmd.Flags().BoolVar(&noHeaderFlag, "no-headers", false, "Hide table headers") + lsTopicsCmd.Flags().BoolVar(&prettyJsonFlag, "pretty-json", false, "pretty print json instead of table") + topicsCmd.Flags().BoolVar(&noHeaderFlag, "no-headers", false, "Hide table headers") updateTopicCmd.Flags().Int32VarP(&partitionsFlag, "partitions", "p", int32(-1), "Number of partitions") updateTopicCmd.Flags().StringVar(&partitionAssignmentsFlag, "partition-assignments", "", "Partition Assignments. Optional. If set in combination with -p, an assignment must be provided for each new partition. Example: '[[1,2,3],[1,2,3]]' (JSON Array syntax) assigns two new partitions to brokers 1,2,3. If used by itself, a reassignment must be provided for all partitions.") @@ -143,7 +146,7 @@ var lsTopicsCmd = &cobra.Command{ sortedTopics := make( []struct { - name string + name string `json:"name"` sarama.TopicDetail }, len(topics)) @@ -157,17 +160,28 @@ var lsTopicsCmd = &cobra.Command{ sort.Slice(sortedTopics, func(i int, j int) bool { return sortedTopics[i].name < sortedTopics[j].name }) + if prettyJsonFlag { + jsonBytes, err := json.MarshalIndent(sortedTopics, "", " ") + if err != nil { + cmd.PrintErrln(err) + return + } + fmt.Println(string(jsonBytes)) - w := tabwriter.NewWriter(outWriter, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags) + } else { + w := tabwriter.NewWriter(outWriter, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags) - if !noHeaderFlag { - fmt.Fprintf(w, "NAME\tPARTITIONS\tREPLICAS\t\n") - } + if !noHeaderFlag { + fmt.Fprintf(w, "NAME\tPARTITIONS\tREPLICAS\t\n") + } + + for _, topic := range sortedTopics { + fmt.Fprintf(w, "%v\t%v\t%v\t\n", topic.name, topic.NumPartitions, topic.ReplicationFactor) + } + w.Flush() - for _, topic := range sortedTopics { - fmt.Fprintf(w, "%v\t%v\t%v\t\n", topic.name, topic.NumPartitions, topic.ReplicationFactor) } - w.Flush() + }, } From dd7ce1ef52716ab3c35498c3711c6ddec30d94b8 Mon Sep 17 00:00:00 2001 From: Alexander Brandstedt Date: Thu, 8 Dec 2022 14:03:01 +0100 Subject: [PATCH 2/2] remove json strict annotation and change name to Name to tell json marchall to include Name in marchall --- cmd/kaf/topic.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/kaf/topic.go b/cmd/kaf/topic.go index 8807bc5c..4a4beb1e 100644 --- a/cmd/kaf/topic.go +++ b/cmd/kaf/topic.go @@ -146,19 +146,19 @@ var lsTopicsCmd = &cobra.Command{ sortedTopics := make( []struct { - name string `json:"name"` + Name string sarama.TopicDetail }, len(topics)) i := 0 for name, topic := range topics { - sortedTopics[i].name = name + sortedTopics[i].Name = name sortedTopics[i].TopicDetail = topic i++ } sort.Slice(sortedTopics, func(i int, j int) bool { - return sortedTopics[i].name < sortedTopics[j].name + return sortedTopics[i].Name < sortedTopics[j].Name }) if prettyJsonFlag { jsonBytes, err := json.MarshalIndent(sortedTopics, "", " ") @@ -176,7 +176,7 @@ var lsTopicsCmd = &cobra.Command{ } for _, topic := range sortedTopics { - fmt.Fprintf(w, "%v\t%v\t%v\t\n", topic.name, topic.NumPartitions, topic.ReplicationFactor) + fmt.Fprintf(w, "%v\t%v\t%v\t\n", topic.Name, topic.NumPartitions, topic.ReplicationFactor) } w.Flush()