watcher

package
v0.0.0-...-9a67539 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: MIT, BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDurationTooShort occurs when calling the watcher's Start
	// method with a duration that's less than 1 nanosecond.
	ErrDurationTooShort = errors.New("error: duration is less than 1ns")

	// ErrWatcherRunning occurs when trying to call the watcher's
	// Start method and the polling cycle is still already running
	// from previously calling Start and not yet calling Close.
	ErrWatcherRunning = errors.New("error: watcher is already running")

	// ErrWatchedFileDeleted is an error that occurs when a file or folder that was
	// being watched has been deleted.
	ErrWatchedFileDeleted = errors.New("error: watched file or folder deleted")

	// ErrSkip is less of an error, but more of a way for path hooks to skip a file or
	// directory.
	ErrSkip = errors.New("error: skipping file")
)

Functions

This section is empty.

Types

type Event

type Event struct {
	Op
	Path    string
	OldPath string
	fs.FileInfo
}

An Event describes an event that is received when files or directory changes occur. It includes the os.FileInfo of the changed file or directory and the type of event that's occurred and the full path of the file.

func (Event) String

func (e Event) String() string

String returns a string depending on what type of event occurred and the file name associated with the event.

type FilterFileHookFunc

type FilterFileHookFunc func(info fs.FileInfo, fullPath string) error

FilterFileHookFunc is a function that is called to filter files during listings. If a file is ok to be listed, nil is returned otherwise ErrSkip is returned.

func RegexFilterHook

func RegexFilterHook(r *regexp.Regexp, useFullPath bool) FilterFileHookFunc

RegexFilterHook is a function that accepts or rejects a file for listing based on whether it's filename or full path matches a regular expression.

type Op

type Op uint32

An Op is a type that is used to describe what type of event has occurred during the watching process.

const (
	Create Op = iota
	Write
	Remove
	Rename
	Chmod
	Move
)

Ops

func (Op) String

func (e Op) String() string

String prints the string version of the Op consts

type Watcher

type Watcher struct {
	Event  chan Event
	Error  chan error
	Closed chan struct{}
	// contains filtered or unexported fields
}

Watcher describes a process that watches files for changes.

func New

func New(fsys fs.FS) *Watcher

New creates a new Watcher.

func (*Watcher) Add

func (w *Watcher) Add(name string) (err error)

Add adds either a single file or directory to the file list.

func (*Watcher) AddFilterHook

func (w *Watcher) AddFilterHook(f FilterFileHookFunc)

AddFilterHook

func (*Watcher) AddRecursive

func (w *Watcher) AddRecursive(name string) (err error)

AddRecursive adds either a single file or directory recursively to the file list.

func (*Watcher) Close

func (w *Watcher) Close()

Close stops a Watcher and unlocks its mutex, then sends a close signal.

func (*Watcher) FilterOps

func (w *Watcher) FilterOps(ops ...Op)

FilterOps filters which event op types should be returned when an event occurs.

func (*Watcher) Ignore

func (w *Watcher) Ignore(paths ...string) (err error)

Ignore adds paths that should be ignored.

For files that are already added, Ignore removes them.

func (*Watcher) IgnoreHiddenFiles

func (w *Watcher) IgnoreHiddenFiles(ignore bool)

IgnoreHiddenFiles sets the watcher to ignore any file or directory that starts with a dot.

func (*Watcher) IsRunning

func (w *Watcher) IsRunning() bool

func (*Watcher) Remove

func (w *Watcher) Remove(name string) (err error)

Remove removes either a single file or directory from the file's list.

func (*Watcher) RemoveRecursive

func (w *Watcher) RemoveRecursive(name string) (err error)

RemoveRecursive removes either a single file or a directory recursively from the file's list.

func (*Watcher) SetMaxEvents

func (w *Watcher) SetMaxEvents(delta int)

SetMaxEvents controls the maximum amount of events that are sent on the Event channel per watching cycle. If max events is less than 1, there is no limit, which is the default.

func (*Watcher) Start

func (w *Watcher) Start(d time.Duration) error

Start begins the polling cycle which repeats every specified duration until Close is called.

func (*Watcher) TriggerEvent

func (w *Watcher) TriggerEvent(eventType Op, file fs.FileInfo)

TriggerEvent is a method that can be used to trigger an event, separate to the file watching process.

func (*Watcher) Wait

func (w *Watcher) Wait()

Wait blocks until the watcher is started.

func (*Watcher) WatchedFiles

func (w *Watcher) WatchedFiles() map[string]fs.FileInfo

WatchedFiles returns a map of files added to a Watcher.

Jump to

Keyboard shortcuts

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