generated from jfrog/jfrog-cli-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_fetcher.go
57 lines (51 loc) · 1.04 KB
/
file_fetcher.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
package printer
import (
"github.com/hpcloud/tail"
"strings"
)
func newFileOpenMetricEntryFetcher(filename string) (*fileOpenMetricEntryFetcher, error) {
t, err := tail.TailFile(filename, tail.Config{
Follow: true,
ReOpen: true,
})
if err != nil {
return nil, err
}
fetcher := fileOpenMetricEntryFetcher{
tail: t,
entries: make(chan string),
}
go fetcher.fetch()
return &fetcher, nil
}
type fileOpenMetricEntryFetcher struct {
tail *tail.Tail
entries chan string
closed bool
}
func (f *fileOpenMetricEntryFetcher) fetch() {
entry := strings.Builder{}
for line := range f.tail.Lines {
if f.closed {
break
}
if line == nil {
continue
}
entry.WriteString(line.Text)
entry.WriteRune('\n')
if line.Text == "" || strings.HasPrefix(line.Text, "#") {
continue
}
f.entries <- entry.String()
entry.Reset()
}
}
func (f *fileOpenMetricEntryFetcher) Close() error {
f.closed = true
close(f.entries)
return f.tail.Stop()
}
func (f *fileOpenMetricEntryFetcher) Entries() <-chan string {
return f.entries
}