Merge pull request #40 from jehiah/env_parsing_40

When loading client-id/client-secret/cookie-secret from env variables, google_auth_proxy blows up.
This commit is contained in:
Jehiah Czebotar 2014-11-14 23:18:08 -05:00
commit 5199e0028b
3 changed files with 31 additions and 3 deletions

View File

@ -6,7 +6,9 @@ import (
"strings"
)
func LoadOptionsFromEnv(options interface{}, cfg map[string]interface{}) {
type EnvOptions map[string]interface{}
func (cfg EnvOptions) LoadEnvForStruct(options interface{}) {
val := reflect.ValueOf(options).Elem()
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {

26
env_options_test.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"os"
"testing"
"github.com/bmizerany/assert"
)
type envTest struct {
testField string `cfg:"target_field" env:"TEST_ENV_FIELD"`
}
func TestLoadEnvForStruct(t *testing.T) {
cfg := make(EnvOptions)
cfg.LoadEnvForStruct(&envTest{})
_, ok := cfg["target_field"]
assert.Equal(t, ok, false)
os.Setenv("TEST_ENV_FIELD", "1234abcd")
cfg.LoadEnvForStruct(&envTest{})
v := cfg["target_field"]
assert.Equal(t, v, "1234abcd")
}

View File

@ -48,14 +48,14 @@ func main() {
opts := NewOptions()
var cfg map[string]interface{}
cfg := make(EnvOptions)
if *config != "" {
_, err := toml.DecodeFile(*config, &cfg)
if err != nil {
log.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
}
}
LoadOptionsFromEnv(opts, cfg)
cfg.LoadEnvForStruct(opts)
options.Resolve(opts, flagSet, cfg)
err := opts.Validate()