Skip to content

feature: list topics command #7

@SteadBytes

Description

@SteadBytes

It's often useful to be able to list all available topics. kcl could support this with a kcl topic list command:

kcl topic list
Topic   Partitions  Internal?
topic1  3           false
topic2  3           false
topic3  3           false

It could also include optional internal topic filtering. What do you think?

There's some potential complication with handling Kafka versions < 0.10.0 due to kmsg.MetadataRequest.Topics taking nil or and empty slice to indicate all topics but the gist of the command is something like:

func topicListCommand(cl *client.Client) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "list",
		Short: "List all topics (Kafka 0.10.0.0+)",
		Run: func(_ *cobra.Command, topics []string) {
			req := kmsg.NewMetadataRequest()
			kresp, err := cl.Client().Request(context.Background(), &req)
			out.MaybeDie(err, "unable to list topics: %v", err)
			resp := kresp.(*kmsg.MetadataResponse)
			if cl.AsJSON() {
				out.ExitJSON(resp.Topics)
			}
			tw := out.BeginTabWrite()
			defer tw.Flush()
			fmt.Fprintln(tw, "Topic\tPartitions\tInternal?")
			for _, topicResp := range resp.Topics {
				fmt.Fprintf(tw, "%s\t%d\t%t\n", topicResp.Topic, len(topicResp.Partitions), topicResp.IsInternal)
			}
		},
	}
	return cmd
}

Happy to submit a PR for implementation if desired 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions