allow gotail to accept multiple files
also rename cmd/tail to cmd/gotail
This commit is contained in:
parent
048bbf8933
commit
507783a4a0
|
@ -0,0 +1 @@
|
||||||
|
gotail
|
|
@ -0,0 +1,4 @@
|
||||||
|
default: gotail
|
||||||
|
|
||||||
|
gotail: *.go ../../*.go
|
||||||
|
GOPATH=~/as/logyard go build
|
|
@ -4,10 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"logyard/tail"
|
"logyard/tail"
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var samplefile = "/tmp/test"
|
|
||||||
|
|
||||||
func args2config() tail.Config {
|
func args2config() tail.Config {
|
||||||
config := tail.Config{Follow: true}
|
config := tail.Config{Follow: true}
|
||||||
flag.IntVar(&config.Location, "n", 0, "tail from the last Nth location")
|
flag.IntVar(&config.Location, "n", 0, "tail from the last Nth location")
|
||||||
|
@ -21,7 +20,25 @@ func args2config() tail.Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
t, err := tail.TailFile(samplefile, args2config())
|
config := args2config()
|
||||||
|
if flag.NFlag() < 1 {
|
||||||
|
fmt.Println("need one or more files as arguments")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan bool)
|
||||||
|
for _, filename := range flag.Args() {
|
||||||
|
go tailFile(filename, config, done)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, _ = range flag.Args() {
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tailFile(filename string, config tail.Config, done chan bool) {
|
||||||
|
defer func() { done <- true }()
|
||||||
|
t, err := tail.TailFile(filename, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
|
@ -1 +0,0 @@
|
||||||
tail
|
|
|
@ -1,4 +0,0 @@
|
||||||
default: tail
|
|
||||||
|
|
||||||
tail: *.go
|
|
||||||
GOPATH=~/as/logyard go build
|
|
3
tail.go
3
tail.go
|
@ -99,9 +99,8 @@ func (tail *Tail) reopen() error {
|
||||||
tail.file, err = os.Open(tail.Filename)
|
tail.file, err = os.Open(tail.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
log.Printf("Waiting for the file to appear...")
|
log.Printf("Waiting for %s to appear...", tail.Filename)
|
||||||
err := tail.watcher.BlockUntilExists()
|
err := tail.watcher.BlockUntilExists()
|
||||||
log.Println(err)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err)
|
return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue