Skip to content

Commit

Permalink
Factor out config
Browse files Browse the repository at this point in the history
  • Loading branch information
yannh committed Dec 27, 2021
1 parent 5f689d7 commit 06af444
Showing 1 changed file with 62 additions and 28 deletions.
90 changes: 62 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -46,38 +47,71 @@ func isFlagPassed(name string) bool {
return found
}

func realMain() int {
var err error

host := flag.String("host", "127.0.0.1", "Server host")
port := flag.Int("port", 6379, "Server port")
db := flag.Uint("db", 0, "only dump this database (default: all databases)")
filter := flag.String("filter", "*", "Key filter to use")
noscan := flag.Bool("noscan", false, "Use KEYS * instead of SCAN - for Redis <=2.8")
batchSize := flag.Int("batchSize", 1000, "HSET/RPUSH/SADD/ZADD only add 'batchSize' items at a time")
nWorkers := flag.Int("n", 10, "Parallel workers")
withTTL := flag.Bool("ttl", true, "Preserve Keys TTL")
output := flag.String("output", "resp", "Output type - can be resp or commands")
silent := flag.Bool("s", false, "Silent mode (disable logging of progress / stats)")

tls := flag.Bool("tls", false, "Establish a secure TLS connection")
caCert := flag.String("cacert", "", "CA Certificate file to verify with")
cert := flag.String("cert", "", "Private key file to authenticate with")
key := flag.String("key", "", "SSL private key file path")
type Config struct {
host string
port int
db *uint
filter string
noscan bool
batchSize int
nWorkers int
withTTL bool
output string
silent bool
tls bool
caCert string
cert string
key string
}

flag.Parse()
func FromFlags(progName string, args []string) (Config, error) {
c := Config{}

flags := flag.NewFlagSet(progName, flag.ExitOnError)
var buf bytes.Buffer
flags.SetOutput(&buf)

flags.StringVar(&c.host, "host", "127.0.0.1", "Server host")
flags.IntVar(&c.port, "port", 6379, "Server port")
flags.UintVar(c.db, "db", 0, "only dump this database (default: all databases)")
flags.StringVar(&c.filter, "filter", "*", "Key filter to use")
flags.BoolVar(&c.noscan, "noscan", false, "Use KEYS * instead of SCAN - for Redis <=2.8")
flags.IntVar(&c.batchSize, "batchSize", 1000, "HSET/RPUSH/SADD/ZADD only add 'batchSize' items at a time")
flags.IntVar(&c.nWorkers, "n", 10, "Parallel workers")
flags.BoolVar(&c.withTTL, "ttl", true, "Preserve Keys TTL")
flags.StringVar(&c.output, "output", "resp", "Output type - can be resp or commands")
flags.BoolVar(&c.silent, "s", false, "Silent mode (disable logging of progress / stats)")
flags.BoolVar(&c.tls, "tls", false, "Establish a secure TLS connection")
flags.StringVar(&c.caCert, "cacert", "", "CA Certificate file to verify with")
flags.StringVar(&c.cert, "cert", "", "Private key file to authenticate with")
flags.StringVar(&c.key, "key", "", "SSL private key file path")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [FILE OR FOLDER]...\n", progName)

flags.SetOutput(os.Stderr)
flags.PrintDefaults()
}

if !isFlagPassed("db") {
db = nil
c.db = nil
}

err := flags.Parse(args)
return c, err
}

func realMain() int {
var err error

c, _ := FromFlags(os.Args[0], os.Args[1:])

var tlshandler *redisdump.TlsHandler = nil
if isFlagPassed("tls") {
tlshandler = redisdump.NewTlsHandler(*caCert, *cert, *key)
if c.tls == true {
tlshandler = redisdump.NewTlsHandler(c.caCert, c.cert, c.key)
}

var serializer func([]string) string
switch *output {
switch c.output {
case "resp":
serializer = redisdump.RESPSerializer

Expand All @@ -97,29 +131,29 @@ func realMain() int {
defer func() {
close(progressNotifs)
wg.Wait()
if !(*silent) {
if !(c.silent) {
fmt.Fprint(os.Stderr, "\n")
}
}()

pl := newProgressLogger()
go func() {
for n := range progressNotifs {
if !(*silent) {
if !(c.silent) {
pl.drawProgress(os.Stderr, n.Db, n.Done)
}
}
wg.Done()
}()

logger := log.New(os.Stdout, "", 0)
if db == nil {
if err = redisdump.DumpServer(*host, *port, url.QueryEscape(redisPassword), tlshandler, *filter, *nWorkers, *withTTL, *batchSize, *noscan, logger, serializer, progressNotifs); err != nil {
if c.db == nil {
if err = redisdump.DumpServer(c.host, c.port, url.QueryEscape(redisPassword), tlshandler, c.filter, c.nWorkers, c.withTTL, c.batchSize, c.noscan, logger, serializer, progressNotifs); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
return 1
}
} else {
if err = redisdump.DumpDB(*host, *port, url.QueryEscape(redisPassword), tlshandler, uint8(*db), *filter, *nWorkers, *withTTL, *batchSize, *noscan, logger, serializer, progressNotifs); err != nil {
if err = redisdump.DumpDB(c.host, c.port, url.QueryEscape(redisPassword), tlshandler, uint8(*c.db), c.filter, c.nWorkers, c.withTTL, c.batchSize, c.noscan, logger, serializer, progressNotifs); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
return 1
}
Expand Down

0 comments on commit 06af444

Please sign in to comment.