Skip to content

Commit

Permalink
Merge pull request #27 from electric-saw/feat/add-config-topics
Browse files Browse the repository at this point in the history
feat: add list-configs of topics
  • Loading branch information
snakeice authored Aug 2, 2024
2 parents 9e36413 + ff2cd85 commit 0837327
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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

0 comments on commit 0837327

Please sign in to comment.