-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
32 lines (26 loc) · 964 Bytes
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cnfg
import (
"fmt"
"os"
"reflect"
)
// UnmarshalENV copies environment variables into configuration values.
// This is useful for Docker users that find it easier to pass ENV variables
// than a specific configuration file. Uses reflection to find struct tags.
func UnmarshalENV(i interface{}, prefix string) (bool, error) {
return (&ENV{Pfx: prefix, Tag: ENVTag}).Unmarshal(i)
}
// Unmarshal parses and processes environment variables into the provided
// interface. Uses the Prefix and Tag name from the &ENV{} struct values.
func (e *ENV) Unmarshal(i interface{}) (bool, error) {
value := reflect.ValueOf(i)
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct {
return false, fmt.Errorf("can only unmarshal ENV into pointer to struct")
}
if e.Tag == "" {
e.Tag = ENVTag
}
// Save the current environment.
parse := &parser{Tag: e.Tag, Vals: MapEnvPairs(e.Pfx, os.Environ())}
return parse.Struct(value, e.Pfx)
}