forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
59 lines (47 loc) · 1.16 KB
/
list.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
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"github.com/Shopify/sarama"
)
func listCommand() command {
list := flag.NewFlagSet("list", flag.ExitOnError)
list.StringVar(&config.list.args.brokers, "brokers", "localhost:9092", "Comma separated list of brokers. Port defaults to 9092 when omitted.")
list.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage of list:")
list.PrintDefaults()
os.Exit(2)
}
return command{
flags: list,
parseArgs: func(args []string) {
list.Parse(args)
config.list.brokers = strings.Split(config.list.args.brokers, ",")
for i, b := range config.list.brokers {
if !strings.Contains(b, ":") {
config.list.brokers[i] = b + ":9092"
}
}
},
run: func(closer chan struct{}) {
client, err := sarama.NewClient(config.list.brokers, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create client err=%v\n", err)
os.Exit(1)
}
defer client.Close()
topics, err := client.Topics()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read topics err=%v\n", err)
os.Exit(1)
}
sort.Strings(topics)
for _, topic := range topics {
fmt.Println(topic)
}
},
}
}