From 84268381736fe6606ab62c86eaeee628ccc8ea23 Mon Sep 17 00:00:00 2001 From: Rob O'Dwyer Date: Sat, 5 Sep 2015 14:39:05 -0700 Subject: [PATCH] make sure environment variables don't conflict for safety --- main.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 3096503..d68ebe7 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,15 @@ import ( "io/ioutil" "log" "os" + "strings" "time" "github.com/garyburd/redigo/redis" "github.com/namsral/flag" ) +const NAME string = "expiredis" + var ( verbose bool dryRun bool @@ -29,18 +32,21 @@ var ( ) func main() { - flag.BoolVar(&verbose, "verbose", false, "debug logging") - flag.BoolVar(&dryRun, "dry-run", false, "dry run, no destructive commands") - flag.StringVar(&url, "url", "redis://", "URI of Redis server (https://www.iana.org/assignments/uri-schemes/prov/redis)") - flag.StringVar(&pattern, "pattern", "*", "Pattern of keys to process") - flag.IntVar(&limit, "limit", 100, "Maximum number keys to process") - flag.IntVar(&count, "count", 100, "Keys to fetch in each batch") - flag.Int64Var(&delay, "delay", 0, "Delay in ms between batches") - flag.IntVar(&ttlSet, "set-ttl", 0, "Set TTL in seconds of matched keys") - flag.IntVar(&ttlSubtract, "subtract-ttl", 0, "Seconds to subtract from TTL of matched keys") - flag.BoolVar(&deleteKeys, "delete", false, "Delete matched keys") - flag.IntVar(&ttlMin, "ttl-min", 0, "Minimum TTL for a key to be processed. Use -1 to match no TTL.") - flag.Parse() + // config environment variables should be prefixed with "EXPIREDIS_" + fs := flag.NewFlagSetWithEnvPrefix(NAME, strings.ToUpper(NAME), flag.ExitOnError) + + fs.BoolVar(&verbose, "verbose", false, "debug logging") + fs.BoolVar(&dryRun, "dry-run", false, "dry run, no destructive commands") + fs.StringVar(&url, "url", "redis://", "URI of Redis server (https://www.iana.org/assignments/uri-schemes/prov/redis)") + fs.StringVar(&pattern, "pattern", "*", "Pattern of keys to process") + fs.IntVar(&limit, "limit", 100, "Maximum number keys to process") + fs.IntVar(&count, "count", 100, "Keys to fetch in each batch") + fs.Int64Var(&delay, "delay", 0, "Delay in ms between batches") + fs.IntVar(&ttlSet, "set-ttl", 0, "Set TTL in seconds of matched keys") + fs.IntVar(&ttlSubtract, "subtract-ttl", 0, "Seconds to subtract from TTL of matched keys") + fs.BoolVar(&deleteKeys, "delete", false, "Delete matched keys") + fs.IntVar(&ttlMin, "ttl-min", 0, "Minimum TTL for a key to be processed. Use -1 to match no TTL.") + fs.Parse(os.Args[1:]) logger.debug = log.New(os.Stderr, "[debug] ", log.LstdFlags) logger.info = log.New(os.Stderr, "[info] ", log.LstdFlags)