-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.go
74 lines (65 loc) · 1.79 KB
/
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
package main
import (
"context"
"fmt"
"os"
"sync"
"github.com/samber/lo"
"github.com/urfave/cli/v2"
)
func appAction(cCtx *cli.Context) error {
argsWithoutProg := cCtx.Args().Slice()
lenghtOfArgsWithoutProg := len(argsWithoutProg)
if lenghtOfArgsWithoutProg == 0 {
fmt.Println("No key patterns provided")
os.Exit(1)
}
addr := fmt.Sprintf("%s:%s", host, port)
fileConfig := getRedisConfig("redisdel.conf")
redisServerConfig := RedisServerConfigs{
Address: addr,
}
if redisServerConfig.Address == ":" {
redisServerConfig.Address = fileConfig.ServerConfigs.Address
}
redisConfig := RedisConfig{
Username: username,
Password: password,
ServerConfigs: &redisServerConfig,
SentinelConfigs: fileConfig.SentinelConfigs,
}
if redisConfig.Username == "" {
redisConfig.Username = fileConfig.Username
}
if redisConfig.Password == "" {
redisConfig.Password = fileConfig.Password
}
client := newRedisClient(redisConfig)
if client == nil {
fmt.Println("Failed to initialise redis client")
os.Exit(1)
}
matchedKeysChans := make([]chan []string, lenghtOfArgsWithoutProg)
for i := range matchedKeysChans {
matchedKeysChans[i] = make(chan []string)
}
for i, pattern := range argsWithoutProg {
go getMatchingKeys(pattern, matchedKeysChans[i], client)
}
for i := range matchedKeysChans {
keys := <-matchedKeysChans[i]
fmt.Printf("Found %d keys for pattern %s", len(keys), argsWithoutProg[i])
chunkedKeys := lo.Chunk(keys, 1000)
var wg sync.WaitGroup
for batch, chunk := range chunkedKeys {
wg.Add(1)
go func(chunk []string, batch int) {
defer wg.Done()
client.Del(context.Background(), chunk...)
fmt.Printf("Deleted batch %d of %d for %s", batch, len(chunkedKeys), argsWithoutProg[i])
}(chunk, batch)
}
wg.Wait()
}
return nil
}