86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
package eve
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hpcloud/tail"
|
|
"github.com/radovskyb/watcher"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func Tail(file string) {
|
|
// https://godoc.org/github.com/hpcloud/tail#Config
|
|
t, err := tail.TailFile(
|
|
file,
|
|
tail.Config{
|
|
Follow: true,
|
|
})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
//totalLines := 0
|
|
//for _ = range t.Lines {
|
|
// totalLines ++
|
|
//}
|
|
|
|
//currentLine := 0
|
|
for line := range t.Lines {
|
|
//fmt.Printf("%v/%v", currentLine, totalLines)
|
|
//currentLine ++
|
|
//if currentLine > totalLines {
|
|
fmt.Printf("##%v##%v##\n", line.Time, line.Text)
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// sends newly created files for tail'ing
|
|
// WIP
|
|
func WatchPath(dir string) {
|
|
w := watcher.New()
|
|
|
|
w.FilterOps()
|
|
w.FilterOps(watcher.Create, watcher.Remove, watcher.Write)
|
|
|
|
//r := regexp.MustCompile("^abc$")
|
|
//w.AddFilterHook(watcher.RegexFilterHook(r, false))
|
|
|
|
// dispatch creation/removal events here
|
|
go func() {
|
|
for {
|
|
select {
|
|
case event := <-w.Event:
|
|
// fmt.Println(event)
|
|
fmt.Printf("%s (%s)\n", event.Path, event.Op)
|
|
case err := <-w.Error:
|
|
log.Fatalln(err)
|
|
case <-w.Closed:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err := w.Add(dir); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
for path, f := range w.WatchedFiles() {
|
|
fmt.Printf("%s: %s\n", path, f.Name())
|
|
}
|
|
|
|
fmt.Println("=== startup complete ===")
|
|
|
|
// go func() {
|
|
// w.Wait()
|
|
// w.TriggerEvent(watcher.Create, nil)
|
|
// }()
|
|
|
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
}
|