From 980cdedcc43f2961bc31263ef7d07f1ca946b22c Mon Sep 17 00:00:00 2001 From: Hugo Fonseca Date: Tue, 14 Dec 2021 00:35:10 +0000 Subject: [PATCH] Allow using environment variables to set options --- README.md | 20 +++++++++---------- cmd/asynqmon/main.go | 47 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 48873a8..52ea7b5 100644 --- a/README.md +++ b/README.md @@ -83,16 +83,16 @@ Here's the available flags: _Note_: Use `--redis-url` to specify address, db-number, and password with one flag value; Alternatively, use `--redis-addr`, `--redis-db`, and `--redis-password` to specify each value. -| Flag | Description | Default | -| ------------------------------- | ------------------------------------------------------------------- | ---------------- | -| `--port`(int) | port number to use for web ui server | 8080 | -| `---redis-url`(string) | URL to redis server | "" | -| `--redis-addr`(string) | address of redis server to connect to | "127.0.0.1:6379" | -| `--redis-db`(int) | redis database number | 0 | -| `--redis-password`(string) | password to use when connecting to redis server | "" | -| `--redis-cluster-nodes`(string) | comma separated list of host:port addresses of cluster nodes | "" | -| `--redis-tls`(string) | server name for TLS validation used when connecting to redis server | "" | -| `--redis-insecure-tls`(bool) | disable TLS certificate host checks | false | +| Flag | Env | Description | Default | +| ------------------------------- | --------------------- | ------------------------------------------------------------------- | ---------------- | +| `--port`(int) | `PORT` | port number to use for web ui server | 8080 | +| `---redis-url`(string) | `REDIS_URL` | URL to redis server | "" | +| `--redis-addr`(string) | `REDIS_ADDR` | address of redis server to connect to | "127.0.0.1:6379" | +| `--redis-db`(int) | `REDIS_DB` | redis database number | 0 | +| `--redis-password`(string) | `REDIS_PASSWORD` | password to use when connecting to redis server | "" | +| `--redis-cluster-nodes`(string) | `REDIS_CLUSTER_NODES` | comma separated list of host:port addresses of cluster nodes | "" | +| `--redis-tls`(string) | `REDIS_TLS` | server name for TLS validation used when connecting to redis server | "" | +| `--redis-insecure-tls`(bool) | `REDIS_INSECURE_TLS` | disable TLS certificate host checks | false | ### Examples diff --git a/cmd/asynqmon/main.go b/cmd/asynqmon/main.go index 9f67135..3733a57 100644 --- a/cmd/asynqmon/main.go +++ b/cmd/asynqmon/main.go @@ -6,6 +6,8 @@ import ( "fmt" "log" "net/http" + "os" + "strconv" "strings" "time" @@ -31,16 +33,16 @@ var ( ) func init() { - flag.IntVar(&flagPort, "port", 8080, "port number to use for web ui server") - flag.StringVar(&flagRedisAddr, "redis-addr", "127.0.0.1:6379", "address of redis server to connect to") - flag.IntVar(&flagRedisDB, "redis-db", 0, "redis database number") - flag.StringVar(&flagRedisPassword, "redis-password", "", "password to use when connecting to redis server") - flag.StringVar(&flagRedisTLS, "redis-tls", "", "server name for TLS validation used when connecting to redis server") - flag.StringVar(&flagRedisURL, "redis-url", "", "URL to redis server") - flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", false, "disable TLS certificate host checks") - flag.StringVar(&flagRedisClusterNodes, "redis-cluster-nodes", "", "comma separated list of host:port addresses of cluster nodes") - flag.IntVar(&flagMaxPayloadLength, "max-payload-length", 200, "maximum number of utf8 characters printed in the payload cell in the Web UI") - flag.IntVar(&flagMaxResultLength, "max-result-length", 200, "maximum number of utf8 characters printed in the result cell in the Web UI") + flag.IntVar(&flagPort, "port", getEnvOrDefaultInt("PORT", 8080), "port number to use for web ui server") + flag.StringVar(&flagRedisAddr, "redis-addr", getEnvDefaultString("REDIS_ADDR", "127.0.0.1:6379"), "address of redis server to connect to") + flag.IntVar(&flagRedisDB, "redis-db", getEnvOrDefaultInt("REDIS_DB", 0), "redis database number") + flag.StringVar(&flagRedisPassword, "redis-password", getEnvDefaultString("REDIS_PASSWORD", ""), "password to use when connecting to redis server") + flag.StringVar(&flagRedisTLS, "redis-tls", getEnvDefaultString("REDIS_TLS",""), "server name for TLS validation used when connecting to redis server") + flag.StringVar(&flagRedisURL, "redis-url", getEnvDefaultString("REDIS_URL", ""), "URL to redis server") + flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", getEnvOrDefaultBool("REDIS_INSECURE_TLS", false), "disable TLS certificate host checks") + flag.StringVar(&flagRedisClusterNodes, "redis-cluster-nodes", getEnvDefaultString("REDIS_CLUSTER_NODES", ""), "comma separated list of host:port addresses of cluster nodes") + flag.IntVar(&flagMaxPayloadLength, "max-payload-length", getEnvOrDefaultInt("MAX_PAYLOAD_LENGTH", 200), "maximum number of utf8 characters printed in the payload cell in the Web UI") + flag.IntVar(&flagMaxResultLength, "max-result-length", getEnvOrDefaultInt("MAX_RESULT_LENGTH", 200), "maximum number of utf8 characters printed in the result cell in the Web UI") } // TODO: Write test and refactor this code. @@ -144,3 +146,28 @@ func truncate(s string, limit int) string { } return s } + +func getEnvDefaultString(key, def string) string { + v := os.Getenv(key) + if (v == "") { + return def + } + + return v +} + +func getEnvOrDefaultInt(key string, def int) int { + v, err := strconv.Atoi(os.Getenv(key)) + if (err != nil) { + return def + } + return v +} + +func getEnvOrDefaultBool(key string, def bool) bool { + v, err := strconv.ParseBool(os.Getenv(key)) + if (err != nil) { + return def + } + return v +} \ No newline at end of file