-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
142 lines (135 loc) · 3.43 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
133
134
135
136
137
138
139
140
141
142
package envutils
import (
"bufio"
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
)
const (
tagName = `env`
defaultEnv = `.env`
)
func ParseConfig(input interface{}, opts ...Option) (err error) {
config := &Config{Filenames: []string{defaultEnv}}
for _, opt := range opts {
opt(config)
}
for _, filename := range config.Filenames {
err = loadEnv(filename)
if err != nil {
return err
}
}
return parseEnvironmentConfig(input)
}
func loadEnv(fileName string) error {
if fileName == "" {
log.Println("no files to load")
return nil
}
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
value := scanner.Text()
if value == "" {
continue
}
if !strings.Contains(value, "=") {
return fmt.Errorf("missing the necessary characters '='")
}
err := os.Setenv(value[0:strings.Index(value, "=")], value[strings.Index(value, "=")+1:])
if err != nil {
return err
}
}
return nil
}
func parseEnvironmentConfig(obj interface{}) error {
objT := reflect.TypeOf(obj)
objV := reflect.ValueOf(obj)
switch {
case isStruct(objT):
case isStructPtr(objT):
objT = objT.Elem()
objV = objV.Elem()
default:
return fmt.Errorf("%v must be a struct or a struct pointer", obj)
}
for i := 0; i < objT.NumField(); i++ {
field := objT.Field(i)
tag := field.Tag.Get(tagName)
if tag == "" {
continue
}
envValue := os.Getenv(tag)
fieldValue := reflect.ValueOf(obj).Elem()
switch field.Type.Kind() {
case reflect.String:
fieldValue.FieldByName(field.Name).SetString(envValue)
case reflect.Int:
i, err := strconv.Atoi(envValue)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetInt(int64(i))
case reflect.Int64:
i, err := strconv.ParseInt(envValue, 10, 64)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetInt(i)
case reflect.Int32:
i, err := strconv.ParseInt(envValue, 10, 32)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetInt(i)
case reflect.Int16:
i, err := strconv.ParseInt(envValue, 10, 16)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetInt(i)
case reflect.Int8:
i, err := strconv.ParseInt(envValue, 10, 8)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetInt(i)
case reflect.Bool:
b, err := strconv.ParseBool(envValue)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetBool(b)
case reflect.Float64:
f, err := strconv.ParseFloat(envValue, 64)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetFloat(f)
case reflect.Float32:
f, err := strconv.ParseFloat(envValue, 32)
if err != nil {
return fmt.Errorf("format conversion error. [%s]", tag)
}
fieldValue.FieldByName(field.Name).SetFloat(f)
default:
return fmt.Errorf("not support %s", field.Type.Kind().String())
}
}
return nil
}
func isStruct(t reflect.Type) bool {
return t.Kind() == reflect.Struct
}
func isStructPtr(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}