-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
112 lines (87 loc) · 2.79 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
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/twmb/kcl/client"
"github.com/twmb/kcl/commands/admin"
"github.com/twmb/kcl/commands/admin/group"
"github.com/twmb/kcl/commands/admin/topic"
"github.com/twmb/kcl/commands/consume"
"github.com/twmb/kcl/commands/metadata"
"github.com/twmb/kcl/commands/misc"
"github.com/twmb/kcl/commands/myconfig"
"github.com/twmb/kcl/commands/produce"
"github.com/twmb/kcl/commands/transact"
)
// TODO remove cobra to remove ridiculous implicit "help" command from everything.
func main() {
root := &cobra.Command{
Use: "kcl",
Short: "Kafka Command Line command for commanding Kafka on the command line",
Long: `A Kafka command line interface.
kcl is a Kafka swiss army knife that aims to enable Kafka administration,
message producing, and message consuming. If Kafka supports it, kcl aims
to provide it.
For help about configuration, run:
kcl myconfig -h
If this is your first time running kcl, you can create a configuration with:
kcl myconfig create
Command completion is available at:
kcl misc gen-autocomplete
`,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
cl := client.New(root)
root.AddCommand(
consume.Command(cl),
produce.Command(cl),
metadata.Command(cl),
transact.Command(cl),
misc.Command(cl),
admin.Command(cl),
myconfig.Command(cl),
topic.Command(cl),
group.Command(cl),
)
allCommands(root, func(cmd *cobra.Command) {
// Since we print usage on error, there is no reason to also
// print an error on error.
cmd.SilenceErrors = true
// We do not want extra [flags] on every command.
cmd.DisableFlagsInUseLine = true
if cmd.HasParent() {
name := strings.Split(cmd.Use, " ")[0]
cmd.Example = strings.Replace(cmd.Example, name, cmd.Parent().CommandPath()+" "+name, -1)
}
})
root.SetUsageTemplate(usageTmpl)
if err := root.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func allCommands(root *cobra.Command, fn func(*cobra.Command)) {
for _, cmd := range root.Commands() {
allCommands(cmd, fn)
}
fn(root)
}
const usageTmpl = `USAGE:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
ALIASES:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
EXAMPLES:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
SUBCOMMANDS:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
FLAGS:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
GLOBAL FLAGS:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`