Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print topics in sub command topic ls to allow for better integration with jq #238

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions cmd/kaf/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
replicasFlag int16
noHeaderFlag bool
compactFlag bool
prettyJsonFlag bool
)

func init() {
Expand All @@ -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.")
Expand Down Expand Up @@ -143,31 +146,42 @@ var lsTopicsCmd = &cobra.Command{

sortedTopics := make(
[]struct {
name string
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, "", " ")
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()

},
}

Expand Down