File tree 3 files changed +31
-3
lines changed
3 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 6
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
12
val := reflect .ValueOf (options ).Elem ()
11
13
typ := val .Type ()
12
14
for i := 0 ; i < typ .NumField (); i ++ {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -48,14 +48,14 @@ func main() {
48
48
49
49
opts := NewOptions ()
50
50
51
- var cfg map [ string ] interface {}
51
+ cfg := make ( EnvOptions )
52
52
if * config != "" {
53
53
_ , err := toml .DecodeFile (* config , & cfg )
54
54
if err != nil {
55
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
59
options .Resolve (opts , flagSet , cfg )
60
60
61
61
err := opts .Validate ()
You can’t perform that action at this time.
0 commit comments