forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
177 lines (137 loc) · 3.71 KB
/
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package yiigo
import (
"errors"
"os"
"path"
"path/filepath"
"runtime/debug"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/joho/godotenv"
"go.uber.org/zap"
)
// EnvOnChangeFunc 配置文件发生改变时执行的方法
type EnvOnChangeFunc func(e fsnotify.Event)
type environment struct {
path string
watcher bool
eventFn EnvOnChangeFunc
}
// EnvOption 配置文件选项
type EnvOption func(e *environment)
// WithEnvFile 指定配置文件
func WithEnvFile(filename string) EnvOption {
return func(e *environment) {
if v := strings.TrimSpace(filename); len(v) != 0 {
e.path = filepath.Clean(v)
}
}
}
// WithEnvWatcher 监听配置文件
func WithEnvWatcher(fn EnvOnChangeFunc) EnvOption {
return func(e *environment) {
e.watcher = true
e.eventFn = fn
}
}
// LoadEnv 加载配置文件;如未指定,则默认加载当前目录下的.env文件
func LoadEnv(options ...EnvOption) {
env := &environment{path: ".env"}
for _, f := range options {
f(env)
}
filename, err := filepath.Abs(env.path)
if err != nil {
logger.Panic("err load env", zap.Error(err))
}
statEnvFile(filename)
if err := godotenv.Overload(filename); err != nil {
logger.Panic("err load env", zap.Error(err))
}
if env.watcher {
go watchEnvFile(filename, env.eventFn)
}
}
func statEnvFile(filename string) {
if _, err := os.Stat(filename); err == nil {
return
}
if err := os.MkdirAll(path.Dir(filename), 0775); err != nil {
return
}
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
if err != nil {
return
}
f.Close()
}
func watchEnvFile(filename string, fn EnvOnChangeFunc) {
defer func() {
if r := recover(); r != nil {
logger.Error("env watcher panic", zap.Any("error", r), zap.String("env_file", filename), zap.ByteString("stack", debug.Stack()))
}
}()
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Error("err env watcher", zap.Error(err))
return
}
defer watcher.Close()
done := make(chan error)
defer close(done)
go func() {
defer func() {
if r := recover(); r != nil {
logger.Error("env watcher panic", zap.Any("error", r), zap.String("env_file", filename), zap.ByteString("stack", debug.Stack()))
}
}()
realEnvFile, _ := filepath.EvalSymlinks(filename)
createOrWriteMask := fsnotify.Create | fsnotify.Write
for {
select {
case event, ok := <-watcher.Events:
if !ok {
done <- errors.New("channel(watcher.Events) is closed")
return
}
eventFile := filepath.Clean(event.Name)
if eventFile == filename {
// the env file was created or modified
if event.Op&createOrWriteMask != 0 {
if err := godotenv.Overload(filename); err != nil {
logger.Error("err env reload", zap.Error(err), zap.String("env_file", filename))
}
if fn != nil {
fn(event)
}
} else if event.Op&fsnotify.Remove != 0 {
logger.Warn("env file removed", zap.String("env_file", filename))
}
} else {
currentEnvFile, _ := filepath.EvalSymlinks(filename)
// the real filename to the env file changed (eg: k8s ConfigMap replacement)
if len(currentEnvFile) != 0 && currentEnvFile != realEnvFile {
realEnvFile = currentEnvFile
if err := godotenv.Overload(filename); err != nil {
logger.Error("err env reload", zap.Error(err), zap.String("env_file", filename))
}
if fn != nil {
fn(event)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
err = errors.New("channel(watcher.Errors) is closed")
}
done <- err
return
}
}
}()
if err = watcher.Add(path.Dir(filename)); err != nil {
done <- err
}
err = <-done
logger.Error("err env watcher", zap.Error(err), zap.String("env_file", filename))
}