-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
248 lines (216 loc) · 6.5 KB
/
file.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package scli
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync/atomic"
"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v3"
)
// LiveUpdateOpt is type restriction of L in File[T, L].
// EnableLiveUpdate and DisableLiveUpdate are two types implemented LiveUpdateOpt.
// This interface SHOULD NOT be implemented by users.
type LiveUpdateOpt interface {
isWatched() bool
}
var (
_ Parse = &File[any, EnableLiveUpdate]{}
_ Parse = &File[any, DisableLiveUpdate]{}
_ ParseOnce = &MarkOnce[*File[any, EnableLiveUpdate]]{}
)
var (
_ LiveUpdateOpt = EnableLiveUpdate{}
_ LiveUpdateOpt = DisableLiveUpdate{}
)
// EnableLiveUpdate implements LiveUpdateOpt
type EnableLiveUpdate struct{}
func (EnableLiveUpdate) isWatched() bool { return true }
// DisableLiveUpdate implements LiveUpdateOpt
type DisableLiveUpdate struct{}
func (DisableLiveUpdate) isWatched() bool { return false }
// Load T from configuration file
// File[T, L] can be used with scli argument parser, or separately
// To enable live update, use File[T, scli.EnableLiveUpdate]
// To disable live update, use File[T, scli.DisableLiveUpdate]
type File[T any, L LiveUpdateOpt] struct {
// go vet will warn if user try to copy instance.
parsed atomic.Bool
// Explaining why f.t is type *T, not T.
// When live updating, other routine may still hold
// reference to f.t. If f.T is type T, data race might happen
//
// If f.t is type T
// x := f.Get()
// f.t is updated by another routine
// While updating f.t, if read x occurred, a concurrent read/write
// will cause race or panic!
//
// If f.t is type *T
// x := f.Get()
// Another routine updates a new CopyT value and replace f.t with
// pointer to it. Make sure Get() and Update() is does not occur
// concurrently. Get() will return *(new pointer) while the old
// pointer (variable x) still works
//
// File[T] does not introduce data race. if users hold two value
// from Get() (and T{*R}, two T value contains same *R), then
// read/write R concurrently, it's their problem. Because use T
// instead of File[T] will still race.
atomT atomic.Pointer[T]
t *T
// these are for live-update
liveUpdate L
events chan fsnotify.Event
}
// this is a generic unmarshal function
// json, yaml, toml all implemented this
type unmarshalFn func(data []byte, v any) error
// Parse data from file
func (f *File[T, L]) FromString(source string) error {
if !f.parsed.CompareAndSwap(false, true) {
// make sure this method is called only once
panic("File[T,L].FromString() is called more than once")
}
err := f.fromString(source)
if err != nil {
return err
}
// watchChange starts only once
// because FromString should be called only once
if f.liveUpdate.isWatched() {
f.events = make(chan fsnotify.Event, 8)
f.watchChange(source)
}
return nil
}
func (f *File[T, L]) fromString(source string) error {
content, err := os.ReadFile(source)
if err != nil {
return err
}
parseOrder := []unmarshalFn{
json.Unmarshal, yaml.Unmarshal, toml.Unmarshal,
}
if strings.HasSuffix(source, ".yaml") || strings.HasSuffix(source, ".yml") {
parseOrder = []unmarshalFn{yaml.Unmarshal}
} else if strings.HasSuffix(source, ".json") {
parseOrder = []unmarshalFn{json.Unmarshal}
} else if strings.HasSuffix(source, ".toml") {
parseOrder = []unmarshalFn{toml.Unmarshal}
}
value, err := parseByOrder[T](content, parseOrder)
if err != nil {
return err
}
if f.liveUpdate.isWatched() {
f.atomT.Store(&value)
} else {
f.t = &value
}
return nil
}
// Example returns an example of config-file input
// Marker for Parse
func (f *File[T, L]) Example() string {
return "config-file"
}
// Marker for ParseOnce
func (f *File[T, L]) statefulOrImpure() {}
// Get() returns the inner T instance
func (f *File[T, L]) Get() *T {
if f.liveUpdate.isWatched() {
t := f.atomT.Load()
return t
}
return f.t
}
func (f *File[T, L]) watchChange(filename string) {
configFile := filepath.Clean(filename)
configDir, _ := filepath.Split(configFile)
realConfigFile, _ := filepath.EvalSymlinks(filename)
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("failed to create watcher: %s", err)
os.Exit(1)
}
if err := watcher.Add(configDir); err != nil {
log.Printf("watch add conf dir err %v", err)
watcher.Close()
return
}
go func(watcher *fsnotify.Watcher) {
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok { // 'Events' channel is closed
return
}
currentConfigFile, _ := filepath.EvalSymlinks(filename)
// we only care about the config file with the following cases:
// 1 - if the config file was modified or created
// 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
if (filepath.Clean(event.Name) == configFile &&
(event.Has(fsnotify.Write) || event.Has(fsnotify.Create))) ||
(currentConfigFile != "" && currentConfigFile != realConfigFile) {
realConfigFile = currentConfigFile
err := f.fromString(filename)
if err != nil {
log.Printf("read config file error: %s", err) // TODO: better way of log
}
if f.liveUpdate.isWatched() {
select {
case f.events <- event:
default:
// if f.events blocks, discard this event
}
}
} else if filepath.Clean(event.Name) == configFile && event.Has(fsnotify.Remove) {
return
}
case err, ok := <-watcher.Errors:
if ok { // 'Errors' channel is not closed
log.Printf("watcher error: %s", err)
}
return
}
}
}(watcher)
}
// UpdateEvents() returns a channel of fsnotify.Events.
// An event will be send to this channel once the file changes.
func (f *File[T, L]) UpdateEvents() <-chan fsnotify.Event {
// Not using viper's onConfigChange callback because callbacks may run
// concurrently in a uncontrolled way. For example: when change comes,
// previous and this change's callback are running concurrently.
// Channels don't.
return f.events
}
type errList []error
func (el errList) Error() string {
ret := []string{}
for _, e := range el {
ret = append(ret, fmt.Sprintf("[%s]", e.Error()))
}
return strings.Join(ret, " ")
}
func parseByOrder[T any](
content []byte, parseOrder []unmarshalFn,
) (T, error) {
var t T
elist := []error{}
for _, unmarshal := range parseOrder {
err := unmarshal(content, &t)
if err == nil {
return t, nil
} else {
elist = append(elist, err)
}
}
return t, errList(elist)
}