-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
95 lines (81 loc) · 2.18 KB
/
option.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
package follow
import (
"time"
"github.com/kei2100/follow/posfile"
)
type option struct {
rotatedFilePathPatterns []string
positionFile posfile.PositionFile
readFromHead bool
optionFollowRotate
}
type optionFollowRotate struct {
detectRotateDelay time.Duration
followRotate bool
watchRotateInterval time.Duration
}
// OptionFunc let you change follow.Reader behavior.
type OptionFunc func(o *option)
// Default values
const (
DefaultDetectRotateDelay = 5 * time.Second
DefaultFollowRotate = true
DefaultReadFromHead = false
DefaultWatchRotateInterval = 100 * time.Millisecond
)
func (o *option) apply(opts ...OptionFunc) {
o.detectRotateDelay = DefaultDetectRotateDelay
o.followRotate = DefaultFollowRotate
o.readFromHead = DefaultReadFromHead
o.watchRotateInterval = DefaultWatchRotateInterval
for _, fn := range opts {
fn(o)
}
}
// WithRotatedFilePathPatterns let you change rotatedFilePathPatterns
func WithRotatedFilePathPatterns(globPatterns []string) OptionFunc {
return func(o *option) {
o.rotatedFilePathPatterns = globPatterns
}
}
// WithPositionFile let you change positionFile
func WithPositionFile(positionFile posfile.PositionFile) OptionFunc {
return func(o *option) {
o.positionFile = positionFile
}
}
// WithPositionFilePath let you change positionFile
func WithPositionFilePath(path string) (OptionFunc, error) {
if path == "" {
return WithPositionFile(nil), nil
}
pf, err := posfile.Open(path)
if err != nil {
return nil, err
}
return WithPositionFile(pf), nil
}
// WithDetectRotateDelay let you change detectRotateDelay
func WithDetectRotateDelay(v time.Duration) OptionFunc {
return func(o *option) {
o.detectRotateDelay = v
}
}
// WithFollowRotate let you change followRotate
func WithFollowRotate(follow bool) OptionFunc {
return func(o *option) {
o.followRotate = follow
}
}
// WithReadFromHead let you change readFromHead
func WithReadFromHead(v bool) OptionFunc {
return func(o *option) {
o.readFromHead = v
}
}
// WithWatchRotateInterval let you change watchRotateInterval
func WithWatchRotateInterval(v time.Duration) OptionFunc {
return func(o *option) {
o.watchRotateInterval = v
}
}