git.iko.soy/typeable/oauth2-proxy
on master help sign in
test/fix environment var parsing
1c5a01cJehiah Czebotar
env_options.go+3−1
@@ -6,7 +6,9 @@ import (
6 "strings"
6 "strings"
7)
7)
8
8
9−func LoadOptionsFromEnv(options interface{}, cfg map[string]interface{}) {
9+type EnvOptions map[string]interface{}
10+
11+func (cfg EnvOptions) LoadEnvForStruct(options interface{}) {
10 val := reflect.ValueOf(options).Elem()
12 val := reflect.ValueOf(options).Elem()
11 typ := val.Type()
13 typ := val.Type()
12 for i := 0; i < typ.NumField(); i++ {
14 for i := 0; i < typ.NumField(); i++ {
env_options_test.gonew file+26
@@ -0,0 +1,26 @@
1+package main
2+
3+import (
4+ "os"
5+ "testing"
6+
7+ "github.com/bmizerany/assert"
8+)
9+
10+type envTest struct {
11+ testField string `cfg:"target_field" env:"TEST_ENV_FIELD"`
12+}
13+
14+func TestLoadEnvForStruct(t *testing.T) {
15+
16+ cfg := make(EnvOptions)
17+ cfg.LoadEnvForStruct(&envTest{})
18+
19+ _, ok := cfg["target_field"]
20+ assert.Equal(t, ok, false)
21+
22+ os.Setenv("TEST_ENV_FIELD", "1234abcd")
23+ cfg.LoadEnvForStruct(&envTest{})
24+ v := cfg["target_field"]
25+ assert.Equal(t, v, "1234abcd")
26+}
main.go+2−2
@@ -48,14 +48,14 @@ func main() {
48
48
49 opts := NewOptions()
49 opts := NewOptions()
50
50
51− var cfg map[string]interface{}
51+ cfg := make(EnvOptions)
52 if *config != "" {
52 if *config != "" {
53 _, err := toml.DecodeFile(*config, &cfg)
53 _, err := toml.DecodeFile(*config, &cfg)
54 if err != nil {
54 if err != nil {
55 log.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
55 log.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
56 }
56 }
57 }
57 }
58− LoadOptionsFromEnv(opts, cfg)
58+ cfg.LoadEnvForStruct(opts)
59 options.Resolve(opts, flagSet, cfg)
59 options.Resolve(opts, flagSet, cfg)
60
60
61 err := opts.Validate()
61 err := opts.Validate()