-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (79 loc) · 2.41 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
package main
import (
"context"
_ "embed"
"fmt"
"log"
"os"
"time"
"github.com/alexflint/go-arg"
)
//go:embed VERSION
var version string
var description string = "" +
"-- redis-tool\n\n" +
"Tool to iterate redis keys in an efficient and safe manner\n" +
"source: https://github.com/bcap/redis-tool\n"
type Args struct {
Count *CountArgs `arg:"subcommand:count" help:"Counts keys based on a key name pattern"`
Print *PrintArgs `arg:"subcommand:print" help:"Prints keys names based on a key name pattern"`
Dump *DumpArgs `arg:"subcommand:dump" help:"Prints keys and theyir values based on a key name pattern"`
Delete *DeleteArgs `arg:"subcommand:delete" help:"Deletes keys based on a key name pattern"`
RedisAddress string `arg:"-a,--address,required" help:"redis server address. Eg: localhost:6379"`
Cluster bool `arg:"-c,--cluster" help:"connect in cluster mode"`
}
func (*Args) Version() string {
return version
}
func (*Args) Description() string {
return description
}
func main() {
args := Args{}
parser := arg.MustParse(&args)
if parser.Subcommand() == nil {
parser.Fail("no command provided")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := NewUnifiedClient(ctx, args.RedisAddress)
failOnErr(err)
checkClusterModeInconsistency(ctx, args, client)
switch {
case args.Count != nil:
err = CountCmd(ctx, parser, client, args)
case args.Print != nil:
err = PrintCmd(ctx, parser, client, args)
case args.Dump != nil:
err = DumpCmd(ctx, parser, client, args)
case args.Delete != nil:
err = DeleteCmd(ctx, parser, client, args)
}
failOnErr(err)
}
func checkClusterModeInconsistency(ctx context.Context, args Args, client UnifiedClient) {
if client.IsCluster && !args.Cluster {
log.Printf(""+
severeWarning+" Node %s is a member of a cluster, but cluster mode (-c|--cluster) is NOT enabled. "+
"Commands will either fail or be local to this particular node. Waiting 5 seconds before continuing",
args.RedisAddress,
)
select {
case <-time.After(5 * time.Second):
case <-ctx.Done():
}
client.Cluster = nil
client.IsCluster = false
return
}
if !client.IsCluster && args.Cluster {
log.Printf(mildWarning+" Cluster mode enabled but %s is not a cluster", args.RedisAddress)
return
}
}
func failOnErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}
}