From 00563ee5706ee4a715431040e4be426bd41abeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerardo=20Mart=C3=ADn=20Chaves?= Date: Tue, 10 Jan 2017 20:45:15 -0300 Subject: [PATCH] Add a tail-end flag When the tail-end flag is on the stdout and stderr files are followed starting at end of file instead of at the start. --- dockerize.go | 6 ++++-- tail.go | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/dockerize.go b/dockerize.go index 777b786..2c91259 100644 --- a/dockerize.go +++ b/dockerize.go @@ -52,6 +52,7 @@ var ( urls []url.URL waitFlag hostFlagsVar waitTimeoutFlag time.Duration + tailEndFlag bool dependencyChan chan struct{} ctx context.Context @@ -180,6 +181,7 @@ func main() { flag.Var(&headersFlag, "wait-http-header", "HTTP headers, colon separated. e.g \"Accept-Encoding: gzip\". Can be passed multiple times") flag.Var(&waitFlag, "wait", "Host (tcp/tcp4/tcp6/http/https/unix) to wait for before this container starts. Can be passed multiple times. e.g. tcp://db:5432") flag.DurationVar(&waitTimeoutFlag, "timeout", 10*time.Second, "Host wait timeout") + flag.BoolVar(&tailEndFlag, "tail-end", false, "Follow stderr and stdout from end of file") flag.Usage = usage flag.Parse() @@ -261,12 +263,12 @@ func main() { for _, out := range stdoutTailFlag { wg.Add(1) - go tailFile(ctx, out, poll, os.Stdout) + go tailFile(ctx, out, poll, os.Stdout, tailEndFlag) } for _, err := range stderrTailFlag { wg.Add(1) - go tailFile(ctx, err, poll, os.Stderr) + go tailFile(ctx, err, poll, os.Stderr, tailEndFlag) } wg.Wait() diff --git a/tail.go b/tail.go index c74a295..fcedbe8 100644 --- a/tail.go +++ b/tail.go @@ -9,13 +9,18 @@ import ( "golang.org/x/net/context" ) -func tailFile(ctx context.Context, file string, poll bool, dest *os.File) { +func tailFile(ctx context.Context, file string, poll bool, dest *os.File, end bool) { defer wg.Done() + seek := os.SEEK_SET + if end { + seek = os.SEEK_END + } t, err := tail.TailFile(file, tail.Config{ Follow: true, ReOpen: true, Poll: poll, Logger: tail.DiscardingLogger, + Location: &tail.SeekInfo{-0, seek}, }) if err != nil { log.Fatalf("unable to tail %s: %s", "foo", err)