allow using environment vars for configuration

fixes #151
This commit is contained in:
Will Norris 2019-06-09 21:02:21 +00:00
parent dfcfda52de
commit 50e0d1104d
7 changed files with 89 additions and 0 deletions

21
third_party/envy/LICENSE vendored Normal file
View file

@ -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.

2
third_party/envy/README.md vendored Normal file
View file

@ -0,0 +1,2 @@
envy is a copy of https://github.com/jamiealquiza/envy without the cobra
support.

50
third_party/envy/envy.go vendored Normal file
View file

@ -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)
})
}

3
third_party/envy/go.mod vendored Normal file
View file

@ -0,0 +1,3 @@
module .
go 1.12