-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
135 lines (107 loc) · 2.99 KB
/
topic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"log/slog"
"os"
"github.com/IBM/sarama"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
// TODO: kafka-topics --config
func topicCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "topic",
Short: "topic",
Run: func(cmd *cobra.Command, args []string) {
},
}
cmd.AddCommand(topicListCmd())
cmd.AddCommand(topicCreateCmd())
cmd.AddCommand(topicDeleteCmd)
return cmd
}
func topicListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
RunE: func(cmd *cobra.Command, args []string) error {
clusterAdmin, err := newClusterAdmin()
if err != nil {
return fmt.Errorf("newClusterAdmin error: %w", err)
}
defer func() {
if err := clusterAdmin.Close(); err != nil {
slog.Error("clusterAdmin.Close failed", err)
}
}()
topics, err := clusterAdmin.ListTopics()
if err != nil {
return fmt.Errorf("clusterAdmin.ListTopics error: %w", err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Topic", "Number of Partitions", "Replication Factor"})
for k, v := range topics {
table.Append([]string{k, fmt.Sprintf("%d", v.NumPartitions), fmt.Sprintf("%d", v.ReplicationFactor)})
}
table.Render()
return nil
},
}
return cmd
}
func topicCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
topic := args[0]
clusterAdmin, err := newClusterAdmin()
if err != nil {
return fmt.Errorf("newClusterAdmin error: %w", err)
}
defer func() {
if err := clusterAdmin.Close(); err != nil {
slog.Error("clusterAdmin.Close failed", err)
}
}()
numPartitions, err := cmd.Flags().GetInt32("partitions")
if err != nil {
return fmt.Errorf("get partitions flag error: %w", err)
}
replicationFactor, err := cmd.Flags().GetInt16("replication-factor")
if err != nil {
return fmt.Errorf("get replication-factor flag error: %w", err)
}
err = clusterAdmin.CreateTopic(topic, &sarama.TopicDetail{
NumPartitions: numPartitions,
ReplicationFactor: replicationFactor,
}, false)
if err != nil {
return fmt.Errorf("clusterAdmin.CreateTopic error: %w", err)
}
return nil
},
}
cmd.Flags().Int32("partitions", 1, "The number of partitions for the topic")
cmd.Flags().Int16("replication-factor", 1, "The replication factor for each partition in the topic being created.")
return cmd
}
var topicDeleteCmd = &cobra.Command{
Use: "delete",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
topic := args[0]
clusterAdmin, err := newClusterAdmin()
if err != nil {
return fmt.Errorf("newClusterAdmin error: %w", err)
}
defer func() {
if err := clusterAdmin.Close(); err != nil {
slog.Error("clusterAdmin.Close failed", err)
}
}()
if err := clusterAdmin.DeleteTopic(topic); err != nil {
return fmt.Errorf("clusterAdmin.DeleteTopic error: %w", err)
}
return nil
},
}