-
Notifications
You must be signed in to change notification settings - Fork 22
/
redis-cli.go
474 lines (404 loc) · 8.71 KB
/
redis-cli.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package main
import (
"flag"
"fmt"
"os"
"path"
"regexp"
"strconv"
"strings"
"github.com/holys/goredis"
"github.com/peterh/liner"
)
var (
hostname = flag.String("h", getEnv("REDIS_HOST", "127.0.0.1"), "Server hostname")
port = flag.String("p", getEnv("REDIS_PORT", "6379"), "Server server port")
socket = flag.String("s", "", "Server socket. (overwrites hostname and port)")
dbn = flag.Int("n", 0, "Database number(default 0)")
auth = flag.String("a", "", "Password to use when connecting to the server")
outputRaw = flag.Bool("raw", false, "Use raw formatting for replies")
showWelcome = flag.Bool("welcome", false, "show welcome message, mainly for web usage via gotty")
)
var (
line *liner.State
historyPath = path.Join(os.Getenv("HOME"), ".gorediscli_history") // $HOME/.gorediscli_history
mode int
client *goredis.Client
)
// output
const (
stdMode = iota
rawMode
)
func main() {
flag.Parse()
if *outputRaw {
mode = rawMode
} else {
mode = stdMode
}
// Start interactive mode when no command is provided
if flag.NArg() == 0 {
repl()
}
noninteractive(flag.Args())
}
func getEnv(key string, defaultValue string) string {
value, found := os.LookupEnv(key)
if !found {
return defaultValue
}
return value
}
// Read-Eval-Print Loop
func repl() {
line = liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
setCompletionHandler()
loadHistory()
defer saveHistory()
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
prompt := ""
cliConnect()
if *showWelcome {
showWelcomeMsg()
}
for {
addr := addr()
if *dbn > 0 && *dbn < 16 {
prompt = fmt.Sprintf("%s[%d]> ", addr, *dbn)
} else {
prompt = fmt.Sprintf("%s> ", addr)
}
cmd, err := line.Prompt(prompt)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
cmds := reg.FindAllString(cmd, -1)
if len(cmds) == 0 {
continue
} else {
appendHistory(cmds)
cmd := strings.ToLower(cmds[0])
if cmd == "help" || cmd == "?" {
printHelp(cmds)
} else if cmd == "quit" || cmd == "exit" {
saveHistory()
line.Close()
os.Exit(0)
} else if cmd == "clear" {
println("Please use Ctrl + L instead")
} else if cmd == "connect" {
reconnect(cmds[1:])
} else if cmd == "mode" {
switchMode(cmds[1:])
} else {
cliSendCommand(cmds)
}
}
}
}
func appendHistory(cmds []string) {
// make a copy of cmds
cloneCmds := make([]string, len(cmds))
for i, cmd := range cmds {
cloneCmds[i] = cmd
}
// for security reason, hide the password with ******
if len(cloneCmds) == 2 && strings.ToLower(cloneCmds[0]) == "auth" {
cloneCmds[1] = "******"
}
if len(cloneCmds) == 4 && strings.ToLower(cloneCmds[0]) == "connect" {
cloneCmds[3] = "******"
}
line.AppendHistory(strings.Join(cloneCmds, " "))
}
func cliSendCommand(cmds []string) {
cliConnect()
if len(cmds) == 0 {
return
}
args := make([]interface{}, len(cmds[1:]))
for i := range args {
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
}
cmd := strings.ToLower(cmds[0])
if cmd == "monitor" {
respChan := make(chan interface{})
stopChan := make(chan struct{})
err := client.Monitor(respChan, stopChan)
if err != nil {
fmt.Printf("(error) %s\n", err.Error())
return
}
for {
select {
case mr := <-respChan:
printReply(0, mr, mode)
fmt.Printf("\n")
case <-stopChan:
fmt.Println("Error: Server closed the connection")
return
}
}
}
r, err := client.Do(cmd, args...)
if err == nil && strings.ToLower(cmd) == "select" {
*dbn, _ = strconv.Atoi(cmds[1])
}
if err != nil {
fmt.Printf("(error) %s", err.Error())
} else {
if cmd == "info" {
printInfo(r)
} else {
printReply(0, r, mode)
}
}
fmt.Printf("\n")
}
func cliConnect() {
if client == nil {
addr := addr()
client = goredis.NewClient(addr, "")
client.SetMaxIdleConns(1)
sendPing(client)
sendSelect(client, *dbn)
sendAuth(client, *auth)
}
}
func reconnect(args []string) {
if len(args) < 2 {
fmt.Println("(error) invalid connect arguments. At least provides host and port.")
return
}
h := args[0]
p := args[1]
var auth string
if len(args) > 2 {
auth = args[2]
}
if h != "" && p != "" {
addr := fmt.Sprintf("%s:%s", h, p)
client = goredis.NewClient(addr, "")
}
if err := sendPing(client); err != nil {
return
}
// change prompt
hostname = &h
port = &p
if auth != "" {
err := sendAuth(client, auth)
if err != nil {
return
}
}
fmt.Printf("connected %s:%s successfully \n", h, p)
}
func switchMode(args []string) {
if len(args) != 1 {
fmt.Println("invalid args. Should be MODE [raw|std]")
return
}
m := strings.ToLower(args[0])
if m != "raw" && m != "std" {
fmt.Println("invalid args. Should be MODE [raw|std]")
return
}
switch m {
case "std":
mode = stdMode
case "raw":
mode = rawMode
}
return
}
func addr() string {
var addr string
if len(*socket) > 0 {
addr = *socket
} else {
addr = fmt.Sprintf("%s:%s", *hostname, *port)
}
return addr
}
func noninteractive(args []string) {
cliSendCommand(args)
}
func printInfo(reply interface{}) {
switch reply := reply.(type) {
case []byte:
fmt.Printf("%s", reply)
//some redis proxies don't support this command.
case goredis.Error:
fmt.Printf("(error) %s", string(reply))
}
}
func printReply(level int, reply interface{}, mode int) {
switch mode {
case stdMode:
printStdReply(level, reply)
case rawMode:
printRawReply(level, reply)
default:
printStdReply(level, reply)
}
}
func printStdReply(level int, reply interface{}) {
switch reply := reply.(type) {
case int64:
fmt.Printf("(integer) %d", reply)
case string:
fmt.Printf("%s", reply)
case []byte:
fmt.Printf("%q", reply)
case nil:
fmt.Printf("(nil)")
case goredis.Error:
fmt.Printf("(error) %s", string(reply))
case []interface{}:
for i, v := range reply {
if i != 0 {
fmt.Printf("%s", strings.Repeat(" ", level*4))
}
s := fmt.Sprintf("%d) ", i+1)
fmt.Printf("%-4s", s)
printStdReply(level+1, v)
if i != len(reply)-1 {
fmt.Printf("\n")
}
}
default:
fmt.Printf("Unknown reply type: %+v", reply)
}
}
func printRawReply(level int, reply interface{}) {
switch reply := reply.(type) {
case int64:
fmt.Printf("%d", reply)
case string:
fmt.Printf("%s", reply)
case []byte:
fmt.Printf("%s", reply)
case nil:
// do nothing
case goredis.Error:
fmt.Printf("%s\n", string(reply))
case []interface{}:
for i, v := range reply {
if i != 0 {
fmt.Printf("%s", strings.Repeat(" ", level*4))
}
printRawReply(level+1, v)
if i != len(reply)-1 {
fmt.Printf("\n")
}
}
default:
fmt.Printf("Unknown reply type: %+v", reply)
}
}
func printGenericHelp() {
msg :=
`redis-cli
Type: "help <command>" for help on <command>
`
fmt.Println(msg)
}
func printCommandHelp(arr []string) {
fmt.Println()
fmt.Printf("\t%s %s \n", arr[0], arr[1])
fmt.Printf("\tGroup: %s \n", arr[2])
fmt.Println()
}
func printHelp(cmds []string) {
args := cmds[1:]
if len(args) == 0 {
printGenericHelp()
} else if len(args) > 1 {
fmt.Println()
} else {
cmd := strings.ToUpper(args[0])
for i := 0; i < len(helpCommands); i++ {
if helpCommands[i][0] == cmd {
printCommandHelp(helpCommands[i])
}
}
}
}
func sendSelect(client *goredis.Client, index int) {
if index == 0 {
// do nothing
return
}
if index > 16 || index < 0 {
index = 0
fmt.Println("index out of range, should less than 16")
}
_, err := client.Do("SELECT", index)
if err != nil {
fmt.Printf("%s\n", err.Error())
}
}
func sendAuth(client *goredis.Client, passwd string) error {
if passwd == "" {
// do nothing
return nil
}
resp, err := client.Do("AUTH", passwd)
if err != nil {
fmt.Printf("(error) %s\n", err.Error())
return err
}
switch resp := resp.(type) {
case goredis.Error:
fmt.Printf("(error) %s\n", resp.Error())
return resp
}
return nil
}
func sendPing(client *goredis.Client) error {
_, err := client.Do("PING")
if err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
return nil
}
func setCompletionHandler() {
line.SetCompleter(func(line string) (c []string) {
for _, i := range helpCommands {
if strings.HasPrefix(i[0], strings.ToUpper(line)) {
c = append(c, i[0])
}
}
return
})
}
func loadHistory() {
if f, err := os.Open(historyPath); err == nil {
line.ReadHistory(f)
f.Close()
}
}
func saveHistory() {
if f, err := os.Create(historyPath); err != nil {
fmt.Printf("Error writing history file: %s", err.Error())
} else {
line.WriteHistory(f)
f.Close()
}
}
func showWelcomeMsg() {
welcome := `
Welcome to redis-cli online.
You can switch to different redis instance with the CONNECT command.
Usage: CONNECT host port [auth]
Switch output mode with MODE command.
Usage: MODE [std | raw]
`
fmt.Println(welcome)
}