watcher

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package watcher provides a thin wrapper around fsnotify for watching file system paths. It offers a configurable interface to register event and error handlers, and ensures safe lifecycle management of the watcher.

The Watcher struct is the main type that manages:

  • Configured paths to watch
  • Event handlers for file changes
  • Error handlers for watcher errors

Example usage:

events := make(chan fsnotify.Event, 1)
errors := make(chan error, 1)

w, err := watcher.Create(
    watcher.OnPath("/tmp"),
    watcher.WithEventChainAsHandler(events),
    watcher.WithErrorChainAsHandler(errors),
)
if err != nil {
    log.Fatalf("failed to create watcher: %v", err)
}

if err := w.Start(); err != nil {
    log.Fatalf("failed to start watcher: %v", err)
}

go func() {
    for {
        select {
        case e := <-events:
            fmt.Println("file event:", e)
        case err := <-errors:
            fmt.Println("watcher error:", err)
        }
    }
}

defer w.Close()

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoPathsConfigured is returned when Start is called
	// without any paths configured to watch.
	ErrNoPathsConfigured = errors.New("no paths has been configured")
)

Functions

This section is empty.

Types

type Option

type Option func(*Watcher) error

Option represents a configuration option that can be applied to a Watcher.

func OnPath

func OnPath(path string) Option

OnPath configures the watcher to observe a single path.

func OnPaths

func OnPaths(paths ...string) Option

OnPaths configures the watcher to observe multiple paths at once.

func WatchSubfolders added in v1.4.4

func WatchSubfolders(enabled bool) Option

WatchSubfolders enables or disables recursive monitoring of subfolders.

By default, fsnotify does not watch subfolders of a directory automatically. When enabled, this option ensures that all nested directories are included in the watch scope, so events such as file creation, modification, renaming, or deletion inside subdirectories will also be detected. Parameters:

  • enabled: set to true to include subfolders in the watch, false to restrict watching only to the top-level directory.

func WithErrorChainAsHandler

func WithErrorChainAsHandler(eventsCh chan<- error) Option

WithErrorChainAsHandler sends all watcher errors to the provided channel. This is useful for external error handling loops.

func WithErrorEventHandler

func WithErrorEventHandler(handler func(error)) Option

WithErrorEventHandler sets the error handler that will be called whenever the watcher encounters an error.

func WithEventChainAsHandler

func WithEventChainAsHandler(eventsCh chan<- fsnotify.Event) Option

WithEventChainAsHandler sends all file system events to the provided channel. This is useful for external event processing loops.

func WithEventHandler

func WithEventHandler(handler func(fsnotify.Event)) Option

WithEventHandler sets the event handler that will be called whenever a file system event occurs on a watched path.

type Watcher added in v1.4.4

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

Watcher wraps fsnotify.Watcher and provides higher-level configuration via functional options. It simplifies setting up file system watchers with custom event and error handlers.

It tracks its lifecycle (`started`) and ensures consistent behavior when adding paths or starting multiple times.

func Create added in v1.4.4

func Create(opts ...Option) (*Watcher, error)

Create creates a new Watcher with the given options.

Example:

w, err := watcher.Create(
    watcher.OnPath("/tmp"),
    watcher.WithEventHandler(func(e fsnotify.Event) { fmt.Println(e) }),
)
if err != nil {
    log.Fatal(err)
}

func (*Watcher) AddPath added in v1.4.4

func (w *Watcher) AddPath(path string) error

AddPath adds a new path to the watcher. It must be called before Start. If the watcher has already been started, this returns ErrOnStartedStateNotAllowingNewPath.

The path must exist on the filesystem, otherwise an error is returned.

func (*Watcher) Close added in v1.4.4

func (w *Watcher) Close() error

Close stops the watcher and releases resources.

func (*Watcher) IsStarted added in v1.4.4

func (w *Watcher) IsStarted() bool

func (*Watcher) Start added in v1.4.4

func (w *Watcher) Start() error

Start initializes the underlying fsnotify.Watcher and begins watching all configured paths. It also launches the event processing goroutine.

Returns ErrNoPathsConfigured if no paths were added.

After Start, the watcher cannot accept new paths (AddPath will fail).

Jump to

Keyboard shortcuts

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