-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
130 lines (107 loc) · 2.43 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
package main
import (
"fmt"
"io"
"log"
"os"
"sync"
"github.com/yannh/redis-dump-go/pkg/config"
"github.com/yannh/redis-dump-go/pkg/redisdump"
)
type progressLogger struct {
stats map[uint8]int
}
func newProgressLogger() *progressLogger {
return &progressLogger{
stats: map[uint8]int{},
}
}
func (p *progressLogger) drawProgress(to io.Writer, db uint8, nDumped int) {
if _, ok := p.stats[db]; !ok && len(p.stats) > 0 {
// We switched database, write to a new line
fmt.Fprintf(to, "\n")
}
p.stats[db] = nDumped
if nDumped == 0 {
return
}
fmt.Fprintf(to, "\rDatabase %d: %d element dumped", db, nDumped)
}
func realMain() int {
var err error
c, outBuf, err := config.FromFlags(os.Args[0], os.Args[1:])
if outBuf != "" {
out := os.Stderr
errCode := 1
if c.Help {
out = os.Stdout
errCode = 0
}
fmt.Fprintln(out, outBuf)
return errCode
}
if err != nil {
fmt.Fprintf(os.Stderr, "failed parsing command line: %s\n", err.Error())
return 1
}
var tlshandler *redisdump.TlsHandler = nil
if c.Tls == true {
tlshandler, err = redisdump.NewTlsHandler(c.CaCert, c.Cert, c.Key, c.Insecure)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
}
var serializer func([]string) string
switch c.Output {
case "resp":
serializer = redisdump.RESPSerializer
case "commands":
serializer = redisdump.RedisCmdSerializer
default:
log.Fatalf("Failed parsing parameter flag: can only be resp or json")
}
redisPassword := os.Getenv("REDISDUMPGO_AUTH")
progressNotifs := make(chan redisdump.ProgressNotification)
var wg sync.WaitGroup
wg.Add(1)
defer func() {
close(progressNotifs)
wg.Wait()
if !(c.Silent) {
fmt.Fprint(os.Stderr, "\n")
}
}()
pl := newProgressLogger()
go func() {
for n := range progressNotifs {
if !(c.Silent) {
pl.drawProgress(os.Stderr, n.Db, n.Done)
}
}
wg.Done()
}()
logger := log.New(os.Stdout, "", 0)
var db = new(uint8)
// If the user passed a db as parameter, we only dump that db
if c.Db >= 0 {
*db = uint8(c.Db)
} else {
db = redisdump.AllDBs
}
s := redisdump.Host{
Host: c.Host,
Port: c.Port,
Username: c.Username,
Password: redisPassword,
TlsHandler: tlshandler,
}
if err = redisdump.DumpServer(s, db, c.Filter, c.NWorkers, c.WithTTL, c.BatchSize, c.Noscan, logger, serializer, progressNotifs); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
return 1
}
return 0
}
func main() {
os.Exit(realMain())
}