-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
70 lines (63 loc) · 1.96 KB
/
cmd.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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/go-redis/redis/v8"
)
type logger bool
func (l logger) stdout(f string, a ...interface{}) {
if !l {
return
}
fmt.Fprintf(os.Stderr, f+"\n", a...)
}
func main() {
ctx := context.Background()
paramDryRun := flag.Bool("dryrun", false, "Only attempt a no-op PING command")
paramVerbose := flag.Bool("verbose", false, "Output more text")
paramFlushAll := flag.Bool("all", false, "Use FLUSHALL instead of FLUSHDB")
paramHost := flag.String("host", "localhost", "Hostname of primary Redis endpoint")
paramPort := flag.Int("port", 6379, "Port of Redis cluster")
paramASync := flag.Bool("async", false, "Don't wait for completion")
paramPassword := flag.String("password", "", "Password for authentication, if enabled")
paramDB := flag.Int("db", 0, "Index of database to flush (use -all to flush all)")
flag.Parse()
log := logger(*paramVerbose)
if *paramFlushAll && *paramDB != 0 {
fmt.Fprintf(os.Stderr, "Use -all to target all databases, or -db to specify a specific database other than 0.")
return
}
var res *redis.StatusCmd
c := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", *paramHost, *paramPort),
Password: *paramPassword,
DB: *paramDB,
})
log.stdout("Will connect to: %s", c.Options().Addr)
if *paramPassword != "" {
log.stdout("Using password for connection.")
}
switch {
case *paramDryRun:
log.stdout("Sending PING")
res = c.Ping(ctx)
case *paramFlushAll && *paramASync:
log.stdout("Sending FLUSHALL ASYNC")
res = c.FlushAllAsync(ctx)
case !*paramFlushAll && *paramASync:
log.stdout("Sending FLUSHDB ASYNC (database %d)", *paramDB)
res = c.FlushDBAsync(ctx)
case *paramFlushAll && !*paramASync:
log.stdout("Sending FLUSHALL")
res = c.FlushAll(ctx)
case !*paramFlushAll && !*paramASync:
log.stdout("Sending FLUSHDB (database %d)", *paramDB)
res = c.FlushDB(ctx)
}
if res.Err() != nil {
panic(res.Err())
}
log.stdout("Command result: %+v", res)
}