-
Notifications
You must be signed in to change notification settings - Fork 1
/
kenv.go
123 lines (112 loc) · 2.73 KB
/
kenv.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
package kenv
import (
"bufio"
"errors"
"fmt"
"os"
"reflect"
"strings"
"sync"
"github.com/kamalshkeir/kstrct"
)
var envFile = false
func Load(envFiles ...string) {
var wg sync.WaitGroup
if len(envFiles) == 0 {
Load(".env")
}
wg.Add(len(envFiles))
envFile = true
for _, env := range envFiles {
go func(env string) {
defer wg.Done()
f, err := os.OpenFile(env, os.O_RDONLY, os.ModePerm)
if err != nil {
envFile = false
fmt.Println(err)
return
}
defer f.Close()
r := bufio.NewScanner(f)
for r.Scan() {
if r.Text()[0] == '#' {
continue
}
key, val, found := strings.Cut(r.Text(), "=")
if !found {
continue
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
err := os.Setenv(key, val)
if err != nil {
continue
}
}
}(env)
}
wg.Wait()
}
// FillStructFromEnv fill the struct from env
func Fill(structure interface{}) error {
if !envFile {
fmt.Println("env: the file wasn't loaded. The environment value would be reading from os or set by default")
}
inputType := reflect.TypeOf(structure)
if inputType != nil {
if inputType.Kind() == reflect.Ptr {
if inputType.Elem().Kind() == reflect.Struct {
return fillStructFromEnv(reflect.ValueOf(structure).Elem())
} else {
return errors.New("env: element is not pointer to struct")
}
}
}
return errors.New("env: invalid structure")
}
// fillStructFromEnv sets a reflected struct fields with the equivalent OS environment variables.
func fillStructFromEnv(s reflect.Value) error {
errored := []string{}
for i := 0; i < s.NumField(); i++ {
if t, exist := s.Type().Field(i).Tag.Lookup("kenv"); exist {
// tag exist
tag := t
defau := ""
required := false
if strings.Contains(t, "|") {
sp := strings.Split(t, "|")
if len(sp) == 2 {
tag = sp[0]
defau = sp[1]
}
} else {
required = true
}
if osv := os.Getenv(strings.TrimSpace(tag)); osv != "" {
kstrct.SetReflectFieldValue(s.Field(i), osv)
} else {
if !required {
fmt.Printf("env: %s don't set. %s will set by default \n", tag, tag)
kstrct.SetReflectFieldValue(s.Field(i), defau)
} else {
errored = append(errored, t)
}
}
} else if s.Type().Field(i).Type.Kind() == reflect.Struct {
// tag not exist, check if struct
if err := fillStructFromEnv(s.Field(i)); err != nil {
return err
}
} else if s.Type().Field(i).Type.Kind() == reflect.Ptr {
if !s.Field(i).IsZero() && s.Field(i).Elem().Type().Kind() == reflect.Struct {
if err := fillStructFromEnv(s.Field(i).Elem()); err != nil {
return err
}
}
}
}
if len(errored) > 0 {
return errors.New(strings.Join(errored, ",") + " required and has not been found")
}
return nil
}