watcher

package
v0.4.5-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package watcher provides abstractions for file system watching. It defines the Watcher port interface and provides an fsnotify-based implementation for cross-platform file modification detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Path is the absolute path to the file or directory that changed.
	Path string

	// Op is the type of file system operation that occurred.
	Op Operation
}

Event represents a file system change event.

type FSNotifyWatcher

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

FSNotifyWatcher implements the Watcher interface using fsnotify. It provides cross-platform file system monitoring with support for Linux (inotify), macOS (FSEvents/kqueue), and Windows (ReadDirectoryChangesW).

func NewFSNotifyWatcher

func NewFSNotifyWatcher(opts ...Option) (*FSNotifyWatcher, error)

NewFSNotifyWatcher creates a new FSNotifyWatcher with the given options. The watcher starts a background goroutine to translate fsnotify events to the Watcher interface format.

func (*FSNotifyWatcher) Add

func (w *FSNotifyWatcher) Add(path string) error

Add starts watching the specified file or directory.

func (*FSNotifyWatcher) Close

func (w *FSNotifyWatcher) Close() error

Close stops the watcher and releases all resources.

func (*FSNotifyWatcher) Errors

func (w *FSNotifyWatcher) Errors() <-chan error

Errors returns the channel for receiving watcher errors.

func (*FSNotifyWatcher) Events

func (w *FSNotifyWatcher) Events() <-chan Event

Events returns the channel for receiving file system events.

func (*FSNotifyWatcher) Remove

func (w *FSNotifyWatcher) Remove(path string) error

Remove stops watching the specified file or directory.

type Operation

type Operation int

Operation represents the type of file system operation.

const (
	// Create indicates a new file or directory was created.
	Create Operation = 1 << iota

	// Write indicates a file was modified.
	Write

	// Remove indicates a file or directory was removed.
	Remove

	// Rename indicates a file or directory was renamed.
	Rename

	// Chmod indicates file permissions were changed.
	Chmod
)

func (Operation) String

func (op Operation) String() string

String returns a human-readable representation of the operation.

type Option

type Option func(*FSNotifyWatcher)

Option is a functional option for configuring FSNotifyWatcher.

type Watcher

type Watcher interface {
	// Add starts watching the specified file or directory for changes.
	// Returns an error if the path doesn't exist or cannot be watched.
	Add(path string) error

	// Remove stops watching the specified file or directory.
	// Returns an error if the path is not currently being watched.
	Remove(path string) error

	// Events returns a read-only channel that receives file system events.
	// The channel is closed when Close() is called.
	Events() <-chan Event

	// Errors returns a read-only channel that receives watcher errors.
	// The channel is closed when Close() is called.
	Errors() <-chan error

	// Close stops all watching and releases resources.
	// After Close returns, the Events and Errors channels will be closed.
	Close() error
}

Watcher defines the port interface for file system watching. This interface follows the Hexagonal Architecture pattern, allowing the application to remain decoupled from the concrete file watching implementation (fsnotify).

Implementations must be safe for concurrent use from multiple goroutines.

Jump to

Keyboard shortcuts

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