From f461ddc97d20e38b7ca3a1775e07d3d279f5f1c7 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Thu, 30 May 2013 13:18:46 -0700 Subject: [PATCH] support Follow=false --- CHANGES.md | 1 + tail.go | 7 +++---- tail_test.go | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a1654d5..6ec6799 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ * Fix potential race condition when reopening the file (issue 5) * Fix potential blocking of `tail.Stop` (issue 4) * Fix uncleaned up ChangeEvents goroutines after calling tail.Stop +* Support Follow=false # Feb, 2013 diff --git a/tail.go b/tail.go index e6d611c..1966c7f 100644 --- a/tail.go +++ b/tail.go @@ -58,10 +58,6 @@ func TailFile(filename string, config Config) (*Tail, error) { panic("cannot set ReOpen without Follow.") } - if !config.Follow { - panic("Follow=false is not supported.") - } - t := &Tail{ Filename: filename, Lines: make(chan *Line), @@ -160,6 +156,9 @@ func (tail *Tail) tailFileSync() { tail.sendLine(line) } case io.EOF: + if !tail.Follow { + return + } // When EOF is reached, wait for more data to become // available. Wait strategy is based on the `tail.watcher` // implementation (inotify or polling). diff --git a/tail_test.go b/tail_test.go index 7535761..888d180 100644 --- a/tail_test.go +++ b/tail_test.go @@ -66,6 +66,20 @@ func TestLocationFull(_t *testing.T) { tail.Stop() } +func TestLocationFullDontFollow(_t *testing.T) { + t := NewTailTest("location-full-dontfollow", _t) + t.CreateFile("test.txt", "hello\nworld\n") + tail := t.StartTail("test.txt", Config{Follow: false, Location: -1}) + go t.VerifyTailOutput(tail, []string{"hello", "world"}) + + // Add more data only after reasonable delay. + <-time.After(100 * time.Millisecond) + t.AppendFile("test.txt", "more\ndata\n") + <-time.After(100 * time.Millisecond) + + tail.Stop() +} + func TestLocationEnd(_t *testing.T) { t := NewTailTest("location-end", _t) t.CreateFile("test.txt", "hello\nworld\n")