-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
65 lines (54 loc) · 1.25 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"github.com/papertrail/go-tail/follower"
flag "github.com/spf13/pflag"
)
var (
number int64
follow bool
reopen bool
)
// "number" and "follow" are ignored for now, since the follower is the only behavior
// until reading the end of the file is implemented
func init() {
flag.Int64VarP(&number, "number", "n", 10, "how many lines to return")
flag.BoolVarP(&follow, "follow", "f", false, "follow file changes")
flag.BoolVarP(&reopen, "reopen", "F", false, "implies -f and re-opens moved / truncated files")
}
func main() {
flag.Parse()
if reopen {
follow = true
}
args := flag.Args()
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "A file must be provided, I don't follow stdin right now")
os.Exit(-1)
}
// printchan is an aggregate channel so that we can tail multiple files
printChan := make(chan follower.Line)
for _, f := range args {
t, err := follower.New(f, follower.Config{
Whence: io.SeekEnd,
Offset: 0,
Reopen: reopen,
})
if err != nil {
panic(err)
}
go func(t *follower.Follower) {
for l := range t.Lines() {
printChan <- l
}
if err := t.Err(); err != nil {
fmt.Println(err)
}
}(t)
}
for line := range printChan {
fmt.Println(line.String())
}
}