/
config.go
132 lines (119 loc) · 3.48 KB
/
config.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package environment
import (
"os"
"reflect"
"strconv"
"strings"
"github.com/beaconsoftwarellc/gadget/errors"
"github.com/beaconsoftwarellc/gadget/stringutil"
)
// NoS3EnvVar is the environment variable to set when you so not want to try and pull from S3.
const NoS3EnvVar = "NO_S3_ENV_VARS"
// Process takes a Specification that describes the configuration for the application
// only attributes tagged with `env:""` will be imported from the environment
// Example Specification:
// type Specification struct {
// DatabaseURL string `env:"DATABASE_URL"`
// ServiceID string `env:"SERVICE_ID,optional"`
// }
// Supported options: optional
func Process(config interface{}) error {
return ProcessMap(config, GetEnvMap())
}
// ProcessMap is the same as Process except that the environment variable map is supplied instead of retrieved
func ProcessMap(config interface{}, envVars map[string]string) error {
val := reflect.ValueOf(config)
if val.Kind() != reflect.Ptr {
return NewInvalidSpecificationError()
}
val = val.Elem()
if val.Kind() != reflect.Struct {
return NewInvalidSpecificationError()
}
bucket := NewBucket()
_, noS3 := envVars[NoS3EnvVar]
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typ := val.Type().Field(i)
envTag, envOptions := stringutil.ParseTag(typ.Tag.Get("env"))
if "" == envTag {
continue
}
s3Bucket, s3Item := stringutil.ParseTag(typ.Tag.Get("s3"))
if "" != s3Bucket && !noS3 {
s3Env := bucket.Get(s3Bucket, s3Item[0], envTag)
if nil != s3Env {
switch t := typ.Type.Kind(); t {
case reflect.String:
valueField.SetString(s3Env.(string))
case reflect.Int:
valueField.SetInt(int64(s3Env.(float64)))
default:
return UnsupportedDataTypeError{Type: t, Field: typ.Name}
}
continue
}
}
env := envVars[envTag]
if "" == env {
if !envOptions.Contains("optional") {
return MissingEnvironmentVariableError{Tag: envTag, Field: typ.Name}
}
continue
}
switch t := typ.Type.Kind(); t {
case reflect.String:
valueField.SetString(env)
case reflect.Int:
parsed, err := strconv.Atoi(env)
if err != nil {
return errors.New("%s while converting %s", err.Error(), envTag)
}
valueField.SetInt(int64(parsed))
default:
return UnsupportedDataTypeError{Type: t, Field: typ.Name}
}
}
return nil
}
// Push the passed specification object onto the environment, note that these changes
// will not live past the life of this process.
func Push(config interface{}) error {
val := reflect.ValueOf(config)
if val.Kind() != reflect.Ptr {
return NewInvalidSpecificationError()
}
val = val.Elem()
if val.Kind() != reflect.Struct {
return NewInvalidSpecificationError()
}
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typ := val.Type().Field(i)
envTag, _ := stringutil.ParseTag(typ.Tag.Get("env"))
if "" == envTag {
continue
}
var value string
switch t := typ.Type.Kind(); t {
case reflect.String:
value = valueField.String()
case reflect.Int:
value = strconv.Itoa(int(valueField.Int()))
default:
return UnsupportedDataTypeError{Type: t, Field: typ.Name}
}
os.Setenv(envTag, value)
}
return nil
}
// GetEnvMap returns a map of all environment variables to their values
func GetEnvMap() map[string]string {
raw := os.Environ() //format "key=val"
filtered := make(map[string]string, len(raw))
for _, keyval := range raw {
parts := strings.SplitN(keyval, "=", 2)
filtered[parts[0]] = parts[1]
}
return filtered
}