-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
/bin/ | ||
/pkg/ | ||
/src/ | ||
/ve/ | ||
/.test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/ActiveState/tail" | ||
"fmt" | ||
) | ||
|
||
type LogDriver struct { | ||
directory string | ||
tails []*tail.Tail | ||
} | ||
|
||
func NewLogDriver(directory string) (l LogDriver) { | ||
return LogDriver{directory, make([]*tail.Tail, 0, 0)} | ||
} | ||
|
||
func (l LogDriver) Tail(filepath string) (t *tail.Tail, err error) { | ||
t, err = tail.TailFile(filepath, tail.Config{Follow: true}) | ||
l.tails = append(l.tails, t) | ||
return t, err | ||
} | ||
|
||
func (l LogDriver) Start() { | ||
// when function completes, notify via the channel | ||
} | ||
|
||
func (l LogDriver) Stop() { | ||
for _, tail := range l.tails { | ||
err := tail.Stop() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
tail.Cleanup() | ||
} | ||
} | ||
|
||
func (l LogDriver) StopOnReceive(done <-chan bool) { | ||
<- done | ||
l.Stop() | ||
} | ||
|
||
/* | ||
defer func() { done <- true }() | ||
tail, err := LogDriver.tail() | ||
func tailFile(filename string, messages chan<- string, done chan<- bool) { | ||
// start tailing | ||
t, err := tail.TailFile(filename, tail.Config{Follow: true}) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
return | ||
} | ||
for line := range t.Lines { | ||
messages <- line.Text | ||
} | ||
err = t.Wait() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"github.com/ActiveState/tail" | ||
) | ||
|
||
type LogDriverTest struct { | ||
Name string | ||
path string | ||
*testing.T | ||
} | ||
|
||
// NOTE: This is me learning more of go by studying the tail | ||
// test cases. A bunch of this code is copied & paste across | ||
// from tail's test suite. | ||
func NewLogDriverTest(name string, t *testing.T) LogDriverTest { | ||
lt := LogDriverTest{name, ".test/" + name, t} | ||
err := os.MkdirAll(lt.path, os.ModeTemporary|0700) | ||
if err != nil { | ||
lt.Fatal(err) | ||
} | ||
return lt | ||
} | ||
|
||
func (lt LogDriverTest) CreateFile(name string, contents string) (path string) { | ||
err := ioutil.WriteFile(lt.path+"/"+name, []byte(contents), 0600) | ||
if err != nil { | ||
lt.Fatal(err) | ||
} | ||
return lt.path + "/" + name | ||
} | ||
|
||
func (lt LogDriverTest) AppendFile(name string, contents string) { | ||
f, err := os.OpenFile(lt.path+"/"+name, os.O_APPEND|os.O_WRONLY, 0600) | ||
if err != nil { | ||
lt.Fatal(err) | ||
} | ||
defer f.Close() | ||
_, err = f.WriteString(contents) | ||
if err != nil { | ||
lt.Fatal(err) | ||
} | ||
} | ||
|
||
func (lt LogDriverTest) AssertTailOutput(tail *tail.Tail, lines []string, done chan bool) { | ||
defer func () {done <- true}() | ||
|
||
for idx, line := range lines { | ||
tailedLine, ok := <-tail.Lines | ||
if !ok { | ||
// tail.Lines is closed and empty. | ||
err := tail.Err() | ||
if err != nil { | ||
lt.Fatalf("tail ended with error: %v", err) | ||
} | ||
lt.Fatalf("tail ended early; expecting more: %v", lines[idx:]) | ||
} | ||
if tailedLine == nil { | ||
lt.Fatalf("tail.Lines returned nil; not possible") | ||
} | ||
// Note: not checking .Err as the `lines` argument is designed | ||
// to match error strings as well. | ||
if tailedLine.Text != line { | ||
lt.Fatalf( | ||
"unexpected line/err from tail: "+ | ||
"expecting <<%s>>>, but got <<<%s>>>", | ||
line, tailedLine.Text) | ||
} | ||
} | ||
} | ||
|
||
func TestTail(t *testing.T) { | ||
|
||
lt := NewLogDriverTest("test_tail_file", t) | ||
filePath := lt.CreateFile("test.txt", "foo\n") | ||
|
||
ld := NewLogDriver("test_tail_file") | ||
tail, _ := ld.Tail(filePath) | ||
|
||
lt.AppendFile("test.txt", "bar\nbaz\n") | ||
done := make(chan bool) | ||
go lt.AssertTailOutput(tail, []string{"foo", "bar", "baz"}, done) | ||
ld.StopOnReceive(done) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.