-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
208 lines (167 loc) · 5.68 KB
/
args.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
package kafka
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
// Argument struct
type Argument struct {
Brokers []string
Topic string
Command Command
ShowVersion bool
Help func()
Message []byte
Verbose bool
Auth bool
Username string
Password string
}
type brokerList []string
func (s *brokerList) String() string {
return fmt.Sprintf("%v", *s)
}
func (s *brokerList) Set(value string) error {
*s = strings.Split(value, ",")
return nil
}
// ParseArgument function
func ParseArgument() (*Argument, error) {
var (
brokers brokerList
topic string
message string
showVersion bool
verbose bool
auth bool
)
argument := &Argument{}
// sub command
publishCommand := flag.NewFlagSet("pub", flag.ExitOnError)
subscribeCommand := flag.NewFlagSet("sub", flag.ExitOnError)
publishCommand.Var(&brokers, "b", "kafka brokers (you can add multiple brokers using separated comma eg: -b localhost:9091,localhost:9092 ..)")
publishCommand.Var(&brokers, "broker", "kafka brokers (you can add multiple brokers using separated comma eg: -b localhost:9091,localhost:9092 ..)")
publishCommand.StringVar(&topic, "t", "", "kafka topic")
publishCommand.StringVar(&topic, "topic", "", "kafka topic")
publishCommand.StringVar(&message, "m", "", "message to publish")
publishCommand.StringVar(&message, "message", "", "message to publish")
publishCommand.BoolVar(&verbose, "V", false, "verbose mode (if true log will appear otherwise no)")
publishCommand.BoolVar(&auth, "auth", false, "set username and password prompt")
subscribeCommand.Var(&brokers, "b", "kafka brokers (you can add multiple brokers using separated comma eg: -b localhost:9091,localhost:9092 ..)")
subscribeCommand.Var(&brokers, "broker", "you can add multiple brokers using separated comma eg: -b localhost:9091,localhost:9092 ..)")
subscribeCommand.StringVar(&topic, "t", "", "kafka topic")
subscribeCommand.StringVar(&topic, "topic", "", "kafka topic")
subscribeCommand.BoolVar(&verbose, "V", false, "verbose mode (if true log will appear otherwise no)")
subscribeCommand.BoolVar(&auth, "auth", false, "set username and password prompt")
flag.BoolVar(&showVersion, "version", false, "show version")
flag.Usage = func() {
fmt.Println()
fmt.Printf("---------------------- Kafka CLI (version %s) ----------------------\n", Version)
fmt.Println("------------------------------------------------------------------------------")
fmt.Println()
fmt.Println(`publish usage : kafka-cli pub -broker localhost:9092 -topic my-topic -message "hello world"`)
fmt.Println()
fmt.Println(`publish with auth usage : kafka-cli pub -broker localhost:9092 -topic my-topic -message "hello world" -auth`)
fmt.Println("------------------------------------------------------------------------------")
fmt.Println(`subscribe usage : kafka-cli sub -broker localhost:9092 -topic my-topic`)
fmt.Println()
fmt.Println(`subscribe with auth usage : kafka-cli sub -broker localhost:9092 -topic my-topic -auth`)
fmt.Println("------------------------------------------------------------------------------")
fmt.Println("sub command either pub (publish) or sub (subsriber)")
fmt.Println("-b | -broker : kafka brokers (you can add multiple brokers using separated by comma eg: -b localhost:9091,localhost:9092 ..)")
fmt.Println("-t | -topic : kafka topic")
fmt.Println("-h : show help")
fmt.Println("-version : show version")
fmt.Println("-V : verbose mode")
fmt.Println("-auth : prompt sasl auth")
fmt.Println("------------------------------------------------------------------------------")
}
flag.Parse()
if len(os.Args) < 2 {
argument.Help = flag.Usage
return argument, ErrorRequiredOneArgument
}
if !strings.Contains(os.Args[1], "version") {
switch os.Args[1] {
case "pub":
publishCommand.Parse(os.Args[2:])
case "sub":
subscribeCommand.Parse(os.Args[2:])
default:
argument.Help = flag.Usage
return argument, ErrorInvalidCommand
}
}
// publish command parsed
if publishCommand.Parsed() {
if len(brokers) <= 0 {
argument.Help = flag.Usage
return argument, ErrorRequiredOneBroker
}
if len(topic) <= 0 {
argument.Help = flag.Usage
return argument, ErrorRequiredTopicName
}
if len(message) <= 0 {
argument.Help = flag.Usage
return argument, ErrorPubRequredMessage
}
for _, broker := range brokers {
if broker == "" {
continue
}
argument.Brokers = append(argument.Brokers, strings.Trim(broker, " "))
}
argument.Topic = topic
argument.Message = []byte(message)
argument.Command = CommandFromString(os.Args[1])
}
// subscribe command parsed
if subscribeCommand.Parsed() {
if len(brokers) <= 0 {
argument.Help = flag.Usage
return argument, ErrorRequiredOneBroker
}
if len(topic) <= 0 {
argument.Help = flag.Usage
return argument, ErrorRequiredTopicName
}
for _, broker := range brokers {
if broker == "" {
continue
}
argument.Brokers = append(argument.Brokers, strings.Trim(broker, " "))
}
argument.Topic = topic
argument.Command = CommandFromString(os.Args[1])
}
if auth {
var (
authFields = []string{
"username: ",
"password: ",
}
responses []string
)
authReader := bufio.NewReader(os.Stdin)
for _, authField := range authFields {
fmt.Printf("%s", authField)
response, err := authReader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(1)
}
response = strings.TrimSpace(response)
responses = append(responses, response)
}
fmt.Println()
argument.Username = responses[0]
argument.Password = responses[1]
}
argument.ShowVersion = showVersion
argument.Verbose = verbose
argument.Auth = auth
return argument, nil
}