From 8e99baedf2c3e433750c2f51681f3c9d15d412be Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 14 Feb 2015 11:24:17 +0100 Subject: First version --- envcfg.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 envcfg.go (limited to 'envcfg.go') diff --git a/envcfg.go b/envcfg.go new file mode 100644 index 0000000..b91ea13 --- /dev/null +++ b/envcfg.go @@ -0,0 +1,40 @@ +// Package envcfg provides environment variable mapping to structs. +// +// Can be used to read configuration parameters from the environment. +// +// Fields for which environment variables can be found are overwritten, otherwise they are left to their previous +// value. +// +// Can be used, for example, after gcfg to override settings provided in a configuration file. +package envcfg + +import ( + "errors" + "reflect" +) + +const ( + TAG = "env" + ABS_TAG = "absenv" + SEP = "_" +) + +type node struct { + parent *node + value *reflect.Value + properties *reflect.StructField +} + +var ErrInvalidConfigStruct = errors.New("invalid parameter: must map to a struct") + +func ReadInto(cfgStruct interface{}) (interface{}, []error) { + s := reflect.ValueOf(cfgStruct).Elem() + + if s.Kind() != reflect.Struct { + return nil, []error{ErrInvalidConfigStruct} + } + + _, errs := setStructFields(node{nil, &s, nil}) + + return cfgStruct, errs +} -- cgit v1.2.3