-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (251 loc) · 6.58 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/manzanit0/beetlectl/kafka"
"github.com/olekukonko/tablewriter"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "beetlectl",
Usage: "making (somewhat) easier to interact with Kafka",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "brokers",
Required: false,
Value: "localhost:9092",
Usage: "broker seeds for initial discovery",
},
},
Commands: []*cli.Command{
{
Name: "subscribe",
Usage: "subscribe to a topic",
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
fmt.Println("This command will start streaming all messages. To exit, press CTRL+C once.")
err = client.Subscribe(ctx.Context, ctx.Args().First())
if err != nil {
return err
}
return nil
},
},
{
Name: "send",
Usage: "Send a message to topic",
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
// Support passing the message as an argument
data := []byte(ctx.Args().Get(1))
if len(data) > 0 {
err = client.Send(ctx.Context, ctx.Args().First(), data)
if err != nil {
return err
}
return nil
}
// Support streaming the messages through STDIN.
reader := bufio.NewReader(os.Stdin)
for {
line, _, err := reader.ReadLine()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return fmt.Errorf("reading line from STDIN: %w", err)
}
log.Println("sending line")
err = client.Send(ctx.Context, ctx.Args().First(), line)
if err != nil {
return fmt.Errorf("send message: %w", err)
}
}
},
},
{
Name: "groups",
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "list groups",
Description: "list groups",
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
groups, err := client.ListGroups(ctx.Context)
if err != nil {
return err
}
if len(groups) == 0 {
fmt.Println("There are no groups!")
return nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "State", "Protocol"})
for _, g := range groups {
table.Append([]string{g.Group, g.GroupState, g.ProtocolType})
}
table.Render()
return nil
},
},
{
Name: "lag",
Usage: "check lag for a given consumer",
Description: "check lag for a given consumer",
Args: true,
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
memberLags, err := client.CalculateLag(ctx.Context, ctx.Args().First())
if err != nil {
return err
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Topic", "Partition", "Member ID","Partition Offset","Member Offset", "Lag"})
for _, lag := range memberLags {
table.Append([]string{
lag.Topic,
fmt.Sprint(lag.Partition),
lag.Member.MemberID,
fmt.Sprint(lag.End.Offset),
fmt.Sprint(lag.Commit.At),
fmt.Sprint(lag.Lag),
})
}
table.Render()
return nil
},
},
},
},
{
Name: "topics",
Usage: "interact with kafka topics",
Description: "interact with kafka topics",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "list all available topics",
Aliases: []string{"ls"},
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
topics, err := client.ListTopics(ctx.Context)
if err != nil {
return err
}
if len(topics) == 0 {
fmt.Println("There are no topics!")
return nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Partitions", "Retention Mins", "Max Message Bytes", "Replication"})
for _, t := range topics {
var retentionMillis, maxMessageBytes, replication string
for _, c := range t.Configs {
switch {
case c.Key == "retention.ms":
retentionMillis = *c.Value
case c.Key == "max.message.bytes":
maxMessageBytes = *c.Value
case c.Key == "min.insync.replicas":
replication = *c.Value
}
}
duration, err := time.ParseDuration(retentionMillis + "ms")
if err != nil {
return err
}
table.Append([]string{
*t.Topic.Topic,
fmt.Sprint(len(t.Topic.Partitions)),
fmt.Sprint(duration.Minutes()),
maxMessageBytes,
replication,
})
}
table.Render()
return nil
},
},
{
Name: "create",
Usage: "create a topic",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Required: true,
Aliases: []string{"n"},
Usage: "name of the topic",
},
&cli.StringFlag{
Name: "partitions",
Required: false,
Value: "1",
Aliases: []string{"p"},
Usage: "amount of partitions for the topic",
},
},
Action: func(ctx *cli.Context) error {
client, err := clientFromCtx(ctx)
if err != nil {
return err
}
defer client.Close()
name := ctx.String("name")
partitions, err := strconv.ParseInt(ctx.String("partitions"), 10, 32)
if err != nil {
return fmt.Errorf("partitions must be an integer!")
}
err = client.CreateTopic(ctx.Context, name, int32(partitions))
if err != nil {
return err
}
fmt.Println("Topic", name, "created!")
return nil
},
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func clientFromCtx(ctx *cli.Context) (kafka.Client, error) {
brokers := strings.Split(ctx.String("brokers"), ",")
client := kafka.New(kgo.SeedBrokers(brokers...))
err := client.Ping(ctx.Context)
if err != nil {
return nil, err
}
return client, nil
}