forked from pampa/rutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (271 loc) · 6.56 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
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
package main
import (
"fmt"
"github.com/cheggaaa/pb"
"github.com/urfave/cli"
"io"
"os"
"time"
)
var r rutil
func main() {
app := cli.NewApp()
app.Usage = "a collection of command line redis utils"
app.Version = "0.1.2"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "host, s",
Value: "127.0.0.1",
Usage: "redis host",
},
cli.StringFlag{
Name: "auth, a",
Usage: "authentication password",
},
cli.IntFlag{
Name: "port, p",
Value: 6379,
Usage: "redis port",
},
}
app.Before = func(c *cli.Context) error {
r.Host = c.GlobalString("host")
r.Port = c.GlobalInt("port")
r.Auth = c.GlobalString("auth")
return nil
}
app.Commands = []cli.Command{
{
Name: "dump",
Usage: "dump redis database to a file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "keys, k",
Value: "*",
Usage: "keys pattern (passed to redis 'keys' command)",
},
cli.StringFlag{
Name: "match, m",
Usage: "regexp filter for key names",
},
cli.BoolFlag{
Name: "invert, v",
Usage: "invert match regexp",
},
cli.BoolFlag{
Name: "auto, a",
Usage: "make up a file name for the dump - redisYYYYMMDDHHMMSS.rdmp",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
auto := c.Bool("auto")
regex := c.String("match")
inv := c.Bool("invert")
var fileName string
if len(args) == 0 && auto == false {
fail("provide a file name or --auto")
} else if len(args) > 0 && auto == true {
fail("you can't provide a name and use --auto at the same time")
} else if len(args) == 1 && auto == false {
fileName = args[0]
} else if auto == true {
fileName = fmt.Sprintf("redis%s.rdmp", time.Now().Format("20060102150405"))
} else if len(args) > 1 {
fail("to many file names")
}
keys, keys_c := r.getKeys(c.String("keys"), regex, inv)
file, err := os.Create(fileName)
checkErr(err, "create "+fileName)
bar := pb.StartNew(keys_c)
totalBytes := r.writeHeader(file, keys_c)
expired := 0
keys_c = 0
for _, k := range keys {
bar.Increment()
var ok, kd = r.dumpKey(k)
if ok {
b := r.writeDump(file, kd)
totalBytes = totalBytes + b
keys_c += 1
} else {
expired += 1
}
}
file.Seek(0, 0)
r.writeHeader(file, keys_c)
bar.FinishPrint(fmt.Sprintf("file: %s, keys: %d, expired: %d, bytes: %d", fileName, keys_c, expired, totalBytes))
return nil
},
},
{
Name: "pipe",
Usage: "dump a redis database to stdout in a format compatible with | redis-cli --pipe",
Flags: []cli.Flag{
cli.StringFlag{
Name: "keys, k",
Value: "*",
Usage: "keys pattern (passed to redis 'keys' command)",
},
cli.StringFlag{
Name: "match, m",
Usage: "regexp filter for key names",
},
cli.BoolFlag{
Name: "invert, v",
Usage: "invert match regexp",
},
},
Action: func(c *cli.Context) error {
keys, _ := r.getKeys(c.String("keys"), c.String("match"), c.Bool("invert"))
for _, k := range keys {
ok, kd := r.dumpKey(k)
if ok {
genRespProto("RESTORE", kd.Key, kd.Pttl, kd.Dump)
}
}
return nil
},
},
{
Name: "restore",
Usage: "restore redis database from a file",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "dry-run, r",
Usage: "pretend to restore",
},
cli.BoolFlag{
Name: "flushdb, f",
Usage: "flush the database before restoring",
},
cli.BoolFlag{
Name: "delete, d",
Usage: "delete key before restoring",
},
cli.BoolFlag{
Name: "ignore, g",
Usage: "ignore BUSYKEY restore errors",
},
cli.BoolFlag{
Name: "stdin, i",
Usage: "read dump from STDIN",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
dry := c.Bool("dry-run")
flush := c.Bool("flushdb")
del := c.Bool("delete")
ignor := c.Bool("ignore")
stdin := c.Bool("stdin")
if flush && del {
fail("flush or delete?")
}
if len(args) == 0 && !stdin {
fail("no file name provided")
} else if len(args) > 0 && stdin {
fail("can't use --stdin with filename")
} else if len(args) > 1 {
fail("to many file names")
}
var file io.Reader
var fileName string
var err interface{}
if stdin {
fileName = "STDIN"
file = os.Stdin
} else {
fileName = args[0]
file, err = os.Open(fileName)
checkErr(err, "open r "+fileName)
}
hd := r.readHeader(file)
if dry == false && flush == true {
res := r.Client().Cmd("FLUSHDB")
checkErr(res.Err, "FLUSHDB")
}
bar := pb.StartNew(int(hd.Keys))
keys_c := 0
for i := uint64(0); i < hd.Keys; i++ {
bar.Increment()
_, d := r.readDump(file)
if dry == false {
if dry == false {
keys_c = keys_c + r.restoreKey(d, del, ignor)
}
}
}
bar.FinishPrint(fmt.Sprintf("file: %s, keys: %d", fileName, keys_c))
return nil
},
},
{
Name: "query",
Aliases: []string{"q"},
Usage: "query keys matching the pattern provided by --keys",
Flags: []cli.Flag{
cli.StringFlag{
Name: "keys, k",
Usage: "keys pattern (passed to redis 'keys' command)",
},
cli.StringFlag{
Name: "match, m",
Usage: "regexp filter for key names",
},
cli.BoolFlag{
Name: "invert, v",
Usage: "invert match regexp",
},
cli.BoolFlag{
Name: "delete",
Usage: "delete keys",
},
cli.BoolFlag{
Name: "print, p",
Usage: "print key values",
},
cli.StringSliceFlag{
Name: "field, f",
Usage: "hash fields to print (default all)",
},
cli.BoolFlag{
Name: "json, j",
Usage: "attempt to parse and pretty print strings as json",
},
},
Action: func(c *cli.Context) error {
pat := c.String("keys")
regex := c.String("match")
inv := c.Bool("invert")
del := c.Bool("delete")
prnt := c.Bool("print")
flds := c.StringSlice("field")
json := c.Bool("json")
if pat == "" {
fail("missing --keys pattern")
}
if del && prnt {
fail("can't use --delete and --print together")
}
if (del || !prnt) && (json || len(flds) > 0) {
fail("use --json and --field with --print")
}
keys, _ := r.getKeys(pat, regex, inv)
for i, k := range keys {
if prnt {
r.printKey(k, flds, json)
} else {
fmt.Printf("%d: %s\n", i+1, k)
if del == true {
res := r.Client().Cmd("DEL", k)
checkErr(res.Err, "DEL "+k)
}
}
}
return nil
},
},
}
app.Run(os.Args)
}