-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
70 lines (57 loc) · 1.73 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
package filewatcher
import (
"context"
"time"
)
func New(options ...option) ListenCloser {
var config configuration
Options.apply(options...)(&config)
return newSimpleWatcher(config)
}
func (singleton) Context(value context.Context) option {
return func(this *configuration) { this.context = value }
}
func (singleton) Filenames(values ...string) option {
return func(this *configuration) { this.filenames = values }
}
func (singleton) Interval(value time.Duration) option {
return func(this *configuration) { this.interval = value }
}
func (singleton) Notify(value func()) option {
return func(this *configuration) { this.notify = value }
}
func (singleton) Logger(value logger) option {
return func(this *configuration) { this.logger = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, item := range Options.defaults(options...) {
item(this)
}
}
}
func (singleton) defaults(options ...option) []option {
return append([]option{
Options.Context(context.Background()),
Options.Filenames(),
Options.Interval(time.Minute),
Options.Notify(func() {}),
Options.Logger(nop{}),
}, options...)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type configuration struct {
context context.Context
filenames []string
interval time.Duration
notify func()
logger logger
}
type option func(*configuration)
type singleton struct{}
var Options singleton
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type nop struct{}
func (nop) Printf(string, ...any) {}
func (nop) Listen() {}
func (nop) Close() error { return nil }