follow

package module
v0.0.0-...-1120083 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 10 Imported by: 1

README

Go library to follow a file for changes; e.g. "tail -F".

There's a little example application in tail/main.go:

func main() {
	if len(os.Args) <= 1 {
		fmt.Println("need at least one filename")
		os.Exit(1)
	}

	f := follow.New()

	// Maximum time to retry opening the file after it goes away; -1 to keep
	// trying forever.
	f.Retry = -1

	// Install signal handler; any signal sent to this will reopen the file; you
	// can send something manually with:
	//    f.Reopen <- os.Interrupt
	signal.Notify(f.Reopen, syscall.SIGHUP)

	// Keep reading data in the background, sending it to the f.Data channel.
	go func() { log.Fatal(f.Start(context.Background(), os.Args[1])) }()

	for {
		data := <-f.Data
		if data.Err != nil {
			if data.Err == io.EOF {
				break
			}
			log.Fatal(data.Err)
		}
		fmt.Println("X", data)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Err   error
	Bytes []byte
}

func (Data) String

func (d Data) String() string

type Follower

type Follower struct {
	Data   chan Data      // Data read from the file.
	Ready  chan struct{}  // Closed if everything is set up.
	Reopen chan os.Signal // Send signal to reopen file.

	// Retry opening the file if it disappears for this period; this will
	// attempt to open the file every second.
	//
	// Default is 2s; set to -1 to retry forever.
	Retry time.Duration
	// contains filtered or unexported fields
}

func New

func New() Follower

func (*Follower) Start

func (f *Follower) Start(ctx context.Context, file string) error

Start following a file for changes.

func (Follower) Stop

func (f Follower) Stop()

Stop following a file for changes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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