diff options
Diffstat (limited to 'envcfg.go')
-rw-r--r-- | envcfg.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/envcfg.go b/envcfg.go new file mode 100644 index 0000000..b91ea13 --- /dev/null +++ b/envcfg.go | |||
@@ -0,0 +1,40 @@ | |||
1 | // Package envcfg provides environment variable mapping to structs. | ||
2 | // | ||
3 | // Can be used to read configuration parameters from the environment. | ||
4 | // | ||
5 | // Fields for which environment variables can be found are overwritten, otherwise they are left to their previous | ||
6 | // value. | ||
7 | // | ||
8 | // Can be used, for example, after gcfg to override settings provided in a configuration file. | ||
9 | package envcfg | ||
10 | |||
11 | import ( | ||
12 | "errors" | ||
13 | "reflect" | ||
14 | ) | ||
15 | |||
16 | const ( | ||
17 | TAG = "env" | ||
18 | ABS_TAG = "absenv" | ||
19 | SEP = "_" | ||
20 | ) | ||
21 | |||
22 | type node struct { | ||
23 | parent *node | ||
24 | value *reflect.Value | ||
25 | properties *reflect.StructField | ||
26 | } | ||
27 | |||
28 | var ErrInvalidConfigStruct = errors.New("invalid parameter: must map to a struct") | ||
29 | |||
30 | func ReadInto(cfgStruct interface{}) (interface{}, []error) { | ||
31 | s := reflect.ValueOf(cfgStruct).Elem() | ||
32 | |||
33 | if s.Kind() != reflect.Struct { | ||
34 | return nil, []error{ErrInvalidConfigStruct} | ||
35 | } | ||
36 | |||
37 | _, errs := setStructFields(node{nil, &s, nil}) | ||
38 | |||
39 | return cfgStruct, errs | ||
40 | } | ||