-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvsnatch.go
115 lines (99 loc) · 2.62 KB
/
envsnatch.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
package envsnatch
import (
"fmt"
"github.com/joho/godotenv"
"os"
"reflect"
"strconv"
"strings"
)
type UnmarshalingErr struct {
Field string
Reason string
}
type EnvSnatch struct {
envMap map[string]string
UnmarshalingErrs []UnmarshalingErr
path string
fileName string
}
func NewEnvSnatch() (*EnvSnatch, error) {
return &EnvSnatch{}, nil
}
func (e *EnvSnatch) AddPath(path string) {
e.path = path
}
func (e *EnvSnatch) AddFileName(fileName string) {
e.fileName = fileName
}
func (e *EnvSnatch) Unmarshal(config interface{}) (*[]UnmarshalingErr, error) {
// Attempt to load .env file if path and fileName are provided
if e.path != "" && e.fileName != "" {
err := godotenv.Load(e.path + "/" + e.fileName)
if err != nil {
fmt.Println(".env not found, using environment variables instead")
}
}
e.loadEnvVars()
val := reflect.ValueOf(config).Elem()
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if field.CanSet() {
tag := typ.Field(i).Tag
tagParts := strings.Split(tag.Get("env"), ",")
envVar := tagParts[0]
// Check if 'optional' is part of the tag
optional := false
if len(tagParts) > 1 && tagParts[1] == "optional" {
optional = true
}
value, exists := e.envMap[envVar]
if !exists && !optional {
e.UnmarshalingErrs = append(e.UnmarshalingErrs, UnmarshalingErr{
Field: envVar,
Reason: "required",
})
continue
}
// Reflective type setting
if exists {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
field.SetInt(intValue)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if uintValue, err := strconv.ParseUint(value, 10, 64); err == nil {
field.SetUint(uintValue)
}
case reflect.Float32, reflect.Float64:
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
field.SetFloat(floatValue)
}
case reflect.Bool:
if boolValue, err := strconv.ParseBool(value); err == nil {
field.SetBool(boolValue)
}
default:
panic(fmt.Sprintf("Unhandled type: %s", field.Type()))
}
}
}
}
if len(e.UnmarshalingErrs) > 0 {
return &e.UnmarshalingErrs, fmt.Errorf("failed to unmarshal")
}
return nil, nil
}
func (e *EnvSnatch) loadEnvVars() {
e.envMap = make(map[string]string)
for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
if len(pair) == 2 {
e.envMap[pair[0]] = pair[1]
}
}
}