diff --git a/README.md b/README.md index c4596a3..3bf3bf6 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,31 @@ $ kafta topic list | topic2 | 6 | 3 | +-------------------------+------------+--------------------+ ``` +List configs of topics: + +``` +$ kafka topic list-configs my-topic + ++-----------------------------------------+---------------------+--------------+ +| Config Name | Config Value | Source | ++-----------------------------------------+---------------------+--------------+ +| compression.type | producer | Default | +| leader.replication.throttled.replicas | N/A | Default | +| min.insync.replicas | 1 | StaticBroker | +| segment.jitter.ms | 0 | Default | +| cleanup.policy | delete | Default | +| flush.ms | 9223372036854775807 | Default | +| follower.replication.throttled.replicas | N/A | Default | +| segment.bytes | 1073741824 | Default | +| retention.ms | 172800000 | Topic | +| flush.messages | 9223372036854775807 | Default | +| delete.retention.ms | 86400000 | Default | +| segment.ms | 604800000 | Default | +| message.timestamp.difference.max.ms | 9223372036854775807 | Default | +| segment.index.bytes | 10485760 | Default | ++-----------------------------------------+---------------------+--------------+ +``` ## Consumer Group List all consumers, run: diff --git a/pkg/cmd/cli/topic/config_topic.go b/pkg/cmd/cli/topic/config_topic.go index c666515..3664992 100644 --- a/pkg/cmd/cli/topic/config_topic.go +++ b/pkg/cmd/cli/topic/config_topic.go @@ -1 +1,69 @@ package topic + +import ( + "fmt" + "sort" + "strings" + + "github.com/IBM/sarama" + "github.com/electric-saw/kafta/internal/pkg/configuration" + "github.com/electric-saw/kafta/internal/pkg/kafka" + cmdutil "github.com/electric-saw/kafta/pkg/cmd/util" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" +) + +type listConfigsOptions struct { + config *configuration.Configuration + topic string +} + +func NewCmdListConfigs(config *configuration.Configuration) *cobra.Command { + options := &listConfigsOptions{config: config} + cmd := &cobra.Command{ + Use: "list-configs TOPIC", + Short: "List all configurations for a topic", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + options.topic = args[0] + cmdutil.CheckErr(options.run()) + }, + } + + return cmd +} + +func (o *listConfigsOptions) run() error { + conn := kafka.MakeConnection(o.config) + defer conn.Close() + + resource := sarama.ConfigResource{ + Name: o.topic, + Type: sarama.TopicResource, + } + configs, err := conn.Admin.DescribeConfig(resource) + if err != nil { + return fmt.Errorf("failed to describe config for topic %s: %w", o.topic, err) + } + + rows := []table.Row{} + for _, config := range configs { + value := strings.TrimSpace(config.Value) + if value == "" { + value = "N/A" + } + + rows = append(rows, table.Row{config.Name, value, config.Source.String()}) + } + + sort.Slice(rows, func(i, j int) bool { + return rows[i][0].(string) < rows[j][0].(string) + }) + + cmdutil.PrintTable( + table.Row{"Config Name", "Config Value", "Source"}, + rows, + ) + + return nil +} diff --git a/pkg/cmd/cli/topic/topic_cmd.go b/pkg/cmd/cli/topic/topic_cmd.go index c9b3257..df10699 100644 --- a/pkg/cmd/cli/topic/topic_cmd.go +++ b/pkg/cmd/cli/topic/topic_cmd.go @@ -18,6 +18,7 @@ func NewCmdTopic(config *configuration.Configuration) *cobra.Command { cmd.AddCommand(NewCmdDescribeTopic(config)) cmd.AddCommand(NewCmdConfigUpdateTopic(config)) cmd.AddCommand(NewCmdConfigResetTopic(config)) + cmd.AddCommand(NewCmdListConfigs(config)) return cmd }