From 50e0d1104df29905aabd0b9bdec7b06819120cca Mon Sep 17 00:00:00 2001 From: Will Norris Date: Sun, 9 Jun 2019 21:02:21 +0000 Subject: [PATCH] allow using environment vars for configuration fixes #151 --- README.md | 7 ++++++ cmd/imageproxy/main.go | 2 ++ go.mod | 4 +++ third_party/envy/LICENSE | 21 ++++++++++++++++ third_party/envy/README.md | 2 ++ third_party/envy/envy.go | 50 ++++++++++++++++++++++++++++++++++++++ third_party/envy/go.mod | 3 +++ 7 files changed, 89 insertions(+) create mode 100644 third_party/envy/LICENSE create mode 100644 third_party/envy/README.md create mode 100644 third_party/envy/envy.go create mode 100644 third_party/envy/go.mod diff --git a/README.md b/README.md index 496866d..2c0ae91 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,13 @@ you want to use a different caching implementation, it's probably easiest to just make a copy of `cmd/imageproxy/main.go` and customize it to fit your needs... it's a very simple command. +### Environment Variables ### + +All configuration flags have equivalent environment variables of the form +`IMAGEPROXY_$NAME`. For example, an on-disk cache could be configured by calling + + IMAGEPROXY_CACHE="/tmp/imageproxy" imageproxy + ## Deploying ## In most cases, you can follow the normal procedure for building a deploying any diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index b560e87..6ea1095 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -33,6 +33,7 @@ import ( "github.com/garyburd/redigo/redis" "github.com/gregjones/httpcache/diskcache" rediscache "github.com/gregjones/httpcache/redis" + "github.com/jamiealquiza/envy" "github.com/peterbourgon/diskv" "willnorris.com/go/imageproxy" "willnorris.com/go/imageproxy/internal/gcscache" @@ -60,6 +61,7 @@ func init() { } func main() { + envy.Parse("IMAGEPROXY") flag.Parse() p := imageproxy.NewProxy(nil, cache.Cache) diff --git a/go.mod b/go.mod index d131b6c..518e320 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc github.com/grpc-ecosystem/grpc-gateway v1.8.5 // indirect github.com/hashicorp/golang-lru v0.5.1 // indirect + github.com/jamiealquiza/envy v1.1.0 github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c // indirect github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect @@ -36,3 +37,6 @@ require ( // temporary fix to https://github.com/golang/lint/issues/436 which still seems to be a problem replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8f45f776aaf1 + +// local copy of envy package without cobra support +replace github.com/jamiealquiza/envy => ./third_party/envy diff --git a/third_party/envy/LICENSE b/third_party/envy/LICENSE new file mode 100644 index 0000000..b70b651 --- /dev/null +++ b/third_party/envy/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Jamie Alquiza + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/third_party/envy/README.md b/third_party/envy/README.md new file mode 100644 index 0000000..c4829e9 --- /dev/null +++ b/third_party/envy/README.md @@ -0,0 +1,2 @@ +envy is a copy of https://github.com/jamiealquiza/envy without the cobra +support. diff --git a/third_party/envy/envy.go b/third_party/envy/envy.go new file mode 100644 index 0000000..2f44213 --- /dev/null +++ b/third_party/envy/envy.go @@ -0,0 +1,50 @@ +// Package envy automatically exposes environment +// variables for all of your flags. +package envy + +import ( + "flag" + "fmt" + "os" + "strings" +) + +// Parse takes a prefix string and exposes environment variables +// for all flags in the default FlagSet (flag.CommandLine) in the +// form of PREFIX_FLAGNAME. +func Parse(p string) { + update(p, flag.CommandLine) +} + +// update takes a prefix string p and *flag.FlagSet. Each flag +// in the FlagSet is exposed as an upper case environment variable +// prefixed with p. Any flag that was not explicitly set by a user +// is updated to the environment variable, if set. +func update(p string, fs *flag.FlagSet) { + // Build a map of explicitly set flags. + set := map[string]interface{}{} + fs.Visit(func(f *flag.Flag) { + set[f.Name] = nil + }) + + fs.VisitAll(func(f *flag.Flag) { + // Create an env var name + // based on the supplied prefix. + envVar := fmt.Sprintf("%s_%s", p, strings.ToUpper(f.Name)) + envVar = strings.Replace(envVar, "-", "_", -1) + + // Update the Flag.Value if the + // env var is non "". + if val := os.Getenv(envVar); val != "" { + // Update the value if it hasn't + // already been set. + if _, defined := set[f.Name]; !defined { + fs.Set(f.Name, val) + } + } + + // Append the env var to the + // Flag.Usage field. + f.Usage = fmt.Sprintf("%s [%s]", f.Usage, envVar) + }) +} diff --git a/third_party/envy/go.mod b/third_party/envy/go.mod new file mode 100644 index 0000000..6a28d1b --- /dev/null +++ b/third_party/envy/go.mod @@ -0,0 +1,3 @@ +module . + +go 1.12