Skip to content

Commit

Permalink
Merge pull request #45 from bnea-engineering/feature/overwrite_redis_…
Browse files Browse the repository at this point in the history
…addr_with_env_variable

support REDIS_ADDR environment variable
  • Loading branch information
oliver006 authored Dec 2, 2016
2 parents bbff425 + b71c605 commit a410b50
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ These settings take precedence over any configurations provided by [environment

Name | Description
-------------------|------------
REDIS_ADDR | Address of Redis node(s)
REDIS_PASSWORD | Password to use when authenticating to Redis

### What's exported?
Expand Down
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
redisAddr = flag.String("redis.addr", "redis://localhost:6379", "Address of one or more redis nodes, separated by separator")
redisPassword = flag.String("redis.password", os.Getenv("REDIS_PASSWORD"), "Password for one or more redis nodes, separated by separator")
redisAddr = flag.String("redis.addr", getEnv("REDIS_ADDR", "redis://localhost:6379"), "Address of one or more redis nodes, separated by separator")
redisPassword = flag.String("redis.password", getEnv("REDIS_PASSWORD", ""), "Password for one or more redis nodes, separated by separator")
namespace = flag.String("namespace", "redis", "Namespace for metrics")
checkKeys = flag.String("check-keys", "", "Comma separated list of keys to export value and length/size")
separator = flag.String("separator", ",", "separator used to split redis.addr and redis.password into several elements.")
Expand Down Expand Up @@ -89,3 +89,12 @@ func main() {
log.Printf("Connecting to redis hosts: %#v", addrs)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}

// getEnv gets an environment variable from a given key and if it doesn't exist,
// returns defaultVal given.
func getEnv(key string, defaultVal string) string {
if envVal, ok := os.LookupEnv(key); ok {
return envVal
}
return defaultVal
}

0 comments on commit a410b50

Please sign in to comment.