tailreader

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 7 Imported by: 0

README

tailreader

tailreader is a Go package that implements an io.Reader that tails a file.

It is intended to be used for binary files that are being written to by another process. It does not implement any kind of "line" reading and thus is fully compatible with the io.Reader interface and agnostic to the file's content.

Installation

go get -u github.com/maurice2k/tailreader

Usage

package main

import (
	"io"
	"log"
	"time"

	"github.com/maurice2k/tailreader"
)

func main() {
	tr, err := tailreader.NewTailingReader(
		"/tmp/test/the-file-to-tail",
		tailreader.WithWaitForFile(true, 30*time.Second), // wait for file to appear, but only for 30 seconds
		tailreader.WithIdleTimeout(60*time.Second),       // close reader if no data is read for 60 seconds
		tailreader.WithCloseOnDelete(true),               // close reader if file is deleted
	)
	if err != nil {
		log.Fatal(err)
	}
	defer tr.Close()

	// if you don't want to wait within tr.Read() you can call tr.WaitForFile()
	// upfront and then start reading, which will block until the file is available
	// or the timeout is reached

	//err = tr.WaitForFile()
	//if err != nil {
	//	log.Fatal(err)
	//}

	buf := make([]byte, 1024)
	for {
		n, err := tr.Read(buf)

		if err == io.EOF {
			// there is no more data to read
			break
		} else if err != nil {
			log.Fatal(err)
		}

		log.Print(string(buf[:n]))
	}
}

License

tailreader is available under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = []Option{
	WithWaitForFile(true, 0),
	WithCloseOnDelete(false),
}
View Source
var ErrIdleTimeout = fmt.Errorf("idle timeout")
View Source
var ErrWaitTimeout = fmt.Errorf("wait for file timeout")

Functions

This section is empty.

Types

type Option

type Option func(opts *Options)

func WithCloseOnDelete

func WithCloseOnDelete(close bool) Option

func WithCloseOnTruncate

func WithCloseOnTruncate(close bool) Option

func WithIdleTimeout

func WithIdleTimeout(timeout time.Duration) Option

func WithTimeoutsAsEOF

func WithTimeoutsAsEOF(timeoutsAsEOF bool) Option

func WithWaitForFile

func WithWaitForFile(wait bool, timeout time.Duration) Option

type Options

type Options struct {
	// WaitForFile indicates whether the reader should wait for the file to be created
	// If this is set to false, Read will return an error if the file does not exist.
	//
	// This will also cause the reader to wait if the file is deleted at some point
	// and CloseOnDelete is set to false.
	WaitForFile bool

	// WaitForFileTimeout indicates how long the reader should wait for the file to be created
	// If this is set to 0, the reader will wait indefinitely.
	WaitForFileTimeout time.Duration

	// CloseOnDelete indicates whether the reader should be closed if the file is deleted
	CloseOnDelete bool

	// CloseOnTruncate indicates whether the reader should be closed if the file is truncated
	CloseOnTruncate bool

	// IdleTimeout indicates how long the reader should wait for new data before closing
	// If this is set to 0, the reader will wait indefinitely
	IdleTimeout time.Duration

	// Whether or not .Read() should return io.EOF if the wait for file or idle timeout is reached
	TreatTimeoutsAsEOF bool
}

type TailingReader

type TailingReader struct {
	// contains filtered or unexported fields
}

func NewTailingReader

func NewTailingReader(filePath string, options ...Option) (*TailingReader, error)

func (*TailingReader) Close

func (r *TailingReader) Close() error

func (*TailingReader) Read

func (r *TailingReader) Read(p []byte) (n int, err error)

func (*TailingReader) WaitForFile

func (r *TailingReader) WaitForFile() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL