From a1dbb84e6ba6b438a7f4e74935fb27aa1d131781 Mon Sep 17 00:00:00 2001 From: Chris Harrington Date: Mon, 7 Feb 2022 18:09:05 -0600 Subject: [PATCH] Add dry-run and verbose flags --- cmd.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cmd.go b/cmd.go index 416c33e..8d146b3 100644 --- a/cmd.go +++ b/cmd.go @@ -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") @@ -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 @@ -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) }