diff options
author | Pacien TRAN-GIRARD | 2015-02-14 11:24:17 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-02-14 11:27:36 +0100 |
commit | 8e99baedf2c3e433750c2f51681f3c9d15d412be (patch) | |
tree | 3fa0ff3ea4823a408dadceb14fcd51ddc16c2aa8 /envcfg.go | |
parent | 8b1f88ce72ef8496879395e7f04b676e6a16e07a (diff) | |
download | go-envcfg-8e99baedf2c3e433750c2f51681f3c9d15d412be.tar.gz |
First version
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 | } | ||