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

feat: add list-configs of topics #27

Merged
merged 7 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 68 additions & 0 deletions pkg/cmd/cli/topic/config_topic.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/cmd/cli/topic/topic_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading