From 4510f41a74d8de8be148f61c4cb310045d361726 Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Wed, 31 Jul 2024 15:52:48 -0300 Subject: [PATCH 1/6] feat: add list-configs of topics --- README.md | 24 +++++++++++ go.mod | 1 + go.sum | 3 ++ pkg/cmd/cli/topic/config_topic.go | 69 +++++++++++++++++++++++++++++++ pkg/cmd/cli/topic/topic_cmd.go | 1 + 5 files changed, 98 insertions(+) diff --git a/README.md b/README.md index adf0108..2863f50 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,31 @@ $ kafta topic list | topic2 | 6 | 3 | +-------------------------+------------+--------------------+ ``` +List configs topic + +``` +$ 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/go.mod b/go.mod index c04f9da..880ab1a 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,7 @@ require ( github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pierrec/lz4/v4 v4.1.17 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/go.sum b/go.sum index c4307a7..e846878 100644 --- a/go.sum +++ b/go.sum @@ -95,9 +95,12 @@ github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYt github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo/v2 v2.8.3 h1:RpbK1G8nWPNaCVFBWsOGnEQQGgASi6b8fxcWBvDYjxQ= github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754= github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= diff --git a/pkg/cmd/cli/topic/config_topic.go b/pkg/cmd/cli/topic/config_topic.go index c666515..fd25ad7 100644 --- a/pkg/cmd/cli/topic/config_topic.go +++ b/pkg/cmd/cli/topic/config_topic.go @@ -1 +1,70 @@ package topic + +import ( + "fmt" + "os" + "strings" + + "github.com/Shopify/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/olekukonko/tablewriter" + "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) + } + + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Config Name", "Config Value", "Source"}) + table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.Bold}) + table.SetColumnColor(tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{}) + + for _, config := range configs { + value := strings.TrimSpace(config.Value) + if value == "" { + value = "N/A" + } + + table.Append([]string{config.Name, value, config.Source.String()}) + } + + table.SetAutoFormatHeaders(false) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetColWidth(30) + + table.Render() + + 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 } From 3e3ff45dffd13b370d0fcdb50aa232a69ef1cbc5 Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Wed, 31 Jul 2024 16:33:39 -0300 Subject: [PATCH 2/6] feat: ajust readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2863f50..d776a9e 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ $ kafta topic list | topic2 | 6 | 3 | +-------------------------+------------+--------------------+ ``` -List configs topic +List configs of topics: ``` $ kafka topic list-configs my-topic From 0a81c142bb1aba5fc3db0f4bf881f974cf9c555e Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Wed, 31 Jul 2024 18:00:34 -0300 Subject: [PATCH 3/6] fix: use util.PrintTable to list-configs --- pkg/cmd/cli/topic/config_topic.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/cli/topic/config_topic.go b/pkg/cmd/cli/topic/config_topic.go index fd25ad7..bb58991 100644 --- a/pkg/cmd/cli/topic/config_topic.go +++ b/pkg/cmd/cli/topic/config_topic.go @@ -2,14 +2,15 @@ package topic import ( "fmt" - "os" + "sort" "strings" "github.com/Shopify/sarama" "github.com/electric-saw/kafta/internal/pkg/configuration" "github.com/electric-saw/kafta/internal/pkg/kafka" + "github.com/electric-saw/kafta/pkg/cmd/util" cmdutil "github.com/electric-saw/kafta/pkg/cmd/util" - "github.com/olekukonko/tablewriter" + "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" ) @@ -46,25 +47,24 @@ func (o *listConfigsOptions) run() error { return fmt.Errorf("failed to describe config for topic %s: %w", o.topic, err) } - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Config Name", "Config Value", "Source"}) - table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.Bold}) - table.SetColumnColor(tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{}) - + rows := []table.Row{} for _, config := range configs { value := strings.TrimSpace(config.Value) if value == "" { value = "N/A" } - table.Append([]string{config.Name, value, config.Source.String()}) + rows = append(rows, table.Row{config.Name, value, config.Source.String()}) } - table.SetAutoFormatHeaders(false) - table.SetAlignment(tablewriter.ALIGN_LEFT) - table.SetColWidth(30) + sort.Slice(rows, func(i, j int) bool { + return rows[i][0].(string) < rows[j][0].(string) + }) - table.Render() + util.PrintTable( + table.Row{"Config Name", "Config Value", "Source"}, + rows, + ) return nil } From 8ac8daf7e03c628cb2a8a1604652a78b55ff3b27 Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Wed, 31 Jul 2024 18:03:56 -0300 Subject: [PATCH 4/6] fix: remove pkg not used --- go.mod | 1 - go.sum | 3 --- 2 files changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 880ab1a..c04f9da 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,6 @@ require ( github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect - github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pierrec/lz4/v4 v4.1.17 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/go.sum b/go.sum index e846878..c4307a7 100644 --- a/go.sum +++ b/go.sum @@ -95,12 +95,9 @@ github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYt github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo/v2 v2.8.3 h1:RpbK1G8nWPNaCVFBWsOGnEQQGgASi6b8fxcWBvDYjxQ= github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754= github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= From 036e58da038c80585cdd9a7f4a706952e0856b97 Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Thu, 1 Aug 2024 11:50:34 -0300 Subject: [PATCH 5/6] fix: add github.com/IBM/sarama to list config topic --- pkg/cmd/cli/topic/config_topic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cli/topic/config_topic.go b/pkg/cmd/cli/topic/config_topic.go index bb58991..6744fc8 100644 --- a/pkg/cmd/cli/topic/config_topic.go +++ b/pkg/cmd/cli/topic/config_topic.go @@ -5,7 +5,7 @@ import ( "sort" "strings" - "github.com/Shopify/sarama" + "github.com/IBM/sarama" "github.com/electric-saw/kafta/internal/pkg/configuration" "github.com/electric-saw/kafta/internal/pkg/kafka" "github.com/electric-saw/kafta/pkg/cmd/util" From ff2cd852244e017c3d3214ed14d3150eb25097e4 Mon Sep 17 00:00:00 2001 From: Mario Valente Date: Thu, 1 Aug 2024 12:04:19 -0300 Subject: [PATCH 6/6] fix: remove duplicate importe --- pkg/cmd/cli/topic/config_topic.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cmd/cli/topic/config_topic.go b/pkg/cmd/cli/topic/config_topic.go index 6744fc8..3664992 100644 --- a/pkg/cmd/cli/topic/config_topic.go +++ b/pkg/cmd/cli/topic/config_topic.go @@ -8,7 +8,6 @@ import ( "github.com/IBM/sarama" "github.com/electric-saw/kafta/internal/pkg/configuration" "github.com/electric-saw/kafta/internal/pkg/kafka" - "github.com/electric-saw/kafta/pkg/cmd/util" cmdutil "github.com/electric-saw/kafta/pkg/cmd/util" "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" @@ -61,7 +60,7 @@ func (o *listConfigsOptions) run() error { return rows[i][0].(string) < rows[j][0].(string) }) - util.PrintTable( + cmdutil.PrintTable( table.Row{"Config Name", "Config Value", "Source"}, rows, )