Skip to content

Commit

Permalink
Add dry-run and verbose flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Harrington committed Feb 8, 2022
1 parent 03d4610 commit a1dbb84
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ import (
"github.com/go-redis/redis/v8"
)

type logger bool

func (l logger) stdout(f string, a ...interface{}) {
if !l {
return
}
fmt.Fprintf(os.Stderr, f+"\n", a...)
}

func main() {
ctx := context.Background()
paramDryRun := flag.Bool("dryrun", false, "Only attempt a no-op PING command")
paramVerbose := flag.Bool("verbose", false, "Output more text")
paramFlushAll := flag.Bool("all", false, "Use FLUSHALL instead of FLUSHDB")
paramHost := flag.String("host", "localhost", "Hostname of primary Redis endpoint")
paramPort := flag.Int("port", 6379, "Port of Redis cluster")
Expand All @@ -19,6 +30,7 @@ func main() {
paramDB := flag.Int("db", 0, "Index of database to flush (use -all to flush all)")
flag.Parse()

log := logger(*paramVerbose)
if *paramFlushAll && *paramDB != 0 {
fmt.Fprintf(os.Stderr, "Use -all to target all databases, or -db to specify a specific database other than 0.")
return
Expand All @@ -30,17 +42,29 @@ func main() {
Password: *paramPassword,
DB: *paramDB,
})
log.stdout("Will connect to: %s", c.Options().Addr)
if *paramPassword != "" {
log.stdout("Using password for connection.")
}
switch {
case *paramDryRun:
log.stdout("Sending PING")
res = c.Ping(ctx)
case *paramFlushAll && *paramASync:
log.stdout("Sending FLUSHALL ASYNC")
res = c.FlushAllAsync(ctx)
case !*paramFlushAll && *paramASync:
log.stdout("Sending FLUSHDB ASYNC (database %d)", *paramDB)
res = c.FlushDBAsync(ctx)
case *paramFlushAll && !*paramASync:
log.stdout("Sending FLUSHALL")
res = c.FlushAll(ctx)
case !*paramFlushAll && !*paramASync:
log.stdout("Sending FLUSHDB (database %d)", *paramDB)
res = c.FlushDB(ctx)
}
if res.Err() != nil {
panic(res.Err())
}
log.stdout("Command result: %+v", res)
}

0 comments on commit a1dbb84

Please sign in to comment.