filewatcher

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: MIT Imports: 6 Imported by: 0

README

filewatcher-go

Pub/sub + fsnotify based filewatcher

Documentation

Overview

Example
dir, err := os.MkdirTemp("", "example")
if err != nil {
	log.Fatal(err)
}
defer os.RemoveAll(dir)

notifier, err := filewatcher.NewFSNotify()
if err != nil {
	log.Fatal(err)
}
watcher := filewatcher.New(notifier, pubsub.NewInprocess())

dstFile := filepath.Join(dir, "fsnotify.txt")

go func() { // Test scenario.
	sleep := func() { time.Sleep(1 * time.Second) }

	sleep() // No events.
	if err := os.WriteFile(dstFile, []byte("hello"), 0666); err != nil {
		log.Fatal(err)
	}
	sleep() // 2 events: CREATE + WRITE
	if err := os.Remove(dstFile); err != nil {
		log.Fatal(err)
	}
	sleep() // 1 event: REMOVE
	watcher.Close()
}()

it := watcher.MustWatch(dstFile)
defer it.Close()
for it.Next(context.Background()) {
	name := filepath.Base(it.Text())
	fmt.Printf("NAME: %q; ERROR: %v\n", name, it.Err())
	if errors.Is(it.Err(), context.DeadlineExceeded) {
		break
	} else if it.Err() != nil {
		continue
	}
	if b, err := os.ReadFile(it.Text()); os.IsNotExist(err) {
		fmt.Println("NO FILE")
	} else if err != nil {
		fmt.Println("OTHER ERROR")
	} else {
		fmt.Printf("CONTENT: %q\n", b)
	}
}
Output:

NAME: "fsnotify.txt"; ERROR: <nil>
CONTENT: "hello"
NAME: "fsnotify.txt"; ERROR: <nil>
CONTENT: "hello"
NAME: "fsnotify.txt"; ERROR: <nil>
NO FILE

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("file watcher is closed")

Functions

This section is empty.

Types

type FSNotify

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

FSNotify implements fsnotify watcher.

func NewFSNotify

func NewFSNotify() (FSNotify, error)

func (FSNotify) Close

func (fsn FSNotify) Close() error

Close stop the watcher, releases allocated resources and waits for underlying go routines to complete.

func (FSNotify) Watch

func (fsn FSNotify) Watch(name string, pub Publisher) error

type FileWatcher

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

FileWatcher listens to file changes. Close method should be called to free allocated resources.

func New

func New(watcher WatchCloser, be pubsub.Backend) FileWatcher

New returns new file watcher ready to observe file changes. Close method should be called when this instance isn't needed anymore to free resources.

func (FileWatcher) Close

func (fw FileWatcher) Close() error

Close stops the watcher and frees allocated resources.

func (FileWatcher) MustWatch

func (fw FileWatcher) MustWatch(name string) *Iterator

MustWatch is a version of Watch which panics if there was an error.

func (FileWatcher) Watch

func (fw FileWatcher) Watch(name string) (*Iterator, error)

Watch starts watching changes for the given file.

type Iterator

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

Iterators iterates every time there is a file change.

func (*Iterator) Close

func (it *Iterator) Close() error

Close stop the iterator and frees allocated resources.

func (*Iterator) Err

func (it *Iterator) Err() error

Err returns an error received by the last Next() call. Note that stream termination error is ignored and Next() always returns false afterwards.

func (*Iterator) Next

func (it *Iterator) Next(ctx context.Context) bool

Next blocks until an event happens. Returned value indicates whether this iterator is still valid after the recent event received.

func (*Iterator) Text

func (it *Iterator) Text() string

Text returns string received by the last Next() call.

type Publisher

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

Publisher broadcasts data.

func (Publisher) Publish

func (pub Publisher) Publish(s string) error

Publish broadcasts the string.

type WatchCloser

type WatchCloser interface {
	Watch(string, Publisher) error
	Close() error
}

WatchCloser allows to inject actual watching implementations.

Jump to

Keyboard shortcuts

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