Use pyed/tailer instead of hpcloud/tail

This commit is contained in:
pyed 2017-04-02 11:13:13 +03:00
parent c01767d342
commit 6c72760e84

32
main.go
View File

@ -13,7 +13,7 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/hpcloud/tail" "github.com/pyed/tailer"
"github.com/pyed/transmission" "github.com/pyed/transmission"
"gopkg.in/telegram-bot-api.v4" "gopkg.in/telegram-bot-api.v4"
) )
@ -211,16 +211,7 @@ func init() {
// if we got a transmission log file, monitor it for torrents completion to notify upon them. // if we got a transmission log file, monitor it for torrents completion to notify upon them.
if TransLogFile != "" { if TransLogFile != "" {
go func() { go func() {
ft, err := tail.TailFile(TransLogFile, tail.Config{ ft := tailer.RunFileTailer(TransLogFile, false, nil)
Location: &tail.SeekInfo{0, 2}, // ignore previous log lines
Follow: true, // as the -f in tail -f
MustExist: true, // if you can't find the file, don't wait for it to be created
Logger: logger, // log to our logger
})
if err != nil {
logger.Printf("[ERROR] tailing transmission log: %s", err)
return
}
// [2017-02-22 21:00:00.898] File-Name State changed from "Incomplete" to "Complete" (torrent.c:2218) // [2017-02-22 21:00:00.898] File-Name State changed from "Incomplete" to "Complete" (torrent.c:2218)
const ( const (
@ -229,16 +220,23 @@ func init() {
end = len(` State changed from "Incomplete" to "Complete" (torrent.c:2218)`) end = len(` State changed from "Incomplete" to "Complete" (torrent.c:2218)`)
) )
for line := range ft.Lines { for {
if strings.Contains(line.Text, substring) { select {
case line := <-ft.Lines():
if strings.Contains(line, substring) {
// if we don't have a chatID continue // if we don't have a chatID continue
if chatID == 0 { if chatID == 0 {
continue continue
} }
msg := fmt.Sprintf("Completed: %s", line.Text[start:len(line.Text)-end]) msg := fmt.Sprintf("Completed: %s", line[start:len(line)-end])
send(msg, chatID, false) send(msg, chatID, false)
} }
case err := <-ft.Errors():
logger.Printf("[ERROR] tailing transmission log: %s", err)
return
}
} }
}() }()
} }
@ -317,7 +315,7 @@ func main() {
go head(update, tokens[1:]) go head(update, tokens[1:])
case "tail", "/tail", "ta", "/ta": case "tail", "/tail", "ta", "/ta":
go tailf(update, tokens[1:]) go tail(update, tokens[1:])
case "downs", "/downs", "dl", "/dl": case "downs", "/downs", "dl", "/dl":
go downs(update) go downs(update)
@ -522,8 +520,8 @@ func head(ud tgbotapi.Update, tokens []string) {
} }
// tailf lists the last 5 or n torrents // tail lists the last 5 or n torrents
func tailf(ud tgbotapi.Update, tokens []string) { func tail(ud tgbotapi.Update, tokens []string) {
var ( var (
n = 5 // default to 5 n = 5 // default to 5
err error err error