fswatch

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 6 Imported by: 0

README

go-fswatch

CI Go Reference License

Polling-based file system watcher for Go. Zero dependencies.

Installation

go get github.com/philiprehberger/go-fswatch

Usage

package main

import (
	"context"
	"fmt"
	"os/signal"
	"syscall"
	"time"

	"github.com/philiprehberger/go-fswatch"
)

func main() {
	w, err := fswatch.New(
		fswatch.Paths("./src", "./config"),
		fswatch.Glob("*.go", "*.yaml"),
		fswatch.Ignore(".git", "*.tmp", "node_modules"),
		fswatch.Debounce(300*time.Millisecond),
		fswatch.PollInterval(1*time.Second),
		fswatch.Recursive(true),
	)
	if err != nil {
		panic(err)
	}

	w.OnChange(func(events []fswatch.Event) {
		for _, e := range events {
			fmt.Printf("%s %s\n", e.Op, e.Path)
		}
	})

	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	if err := w.Start(ctx); err != nil && err != context.Canceled {
		panic(err)
	}
}
Configuration
Option Description Default
Paths(...) Directories to watch required
Glob(...) Include patterns all files
Ignore(...) Exclude patterns none
Debounce(d) Debounce interval 500ms
PollInterval(d) Poll frequency 1s
Recursive(bool) Watch subdirs true
Events
Op Description
Create New file detected
Modify File content changed
Delete File removed

API

Function / Type Description
New(opts ...Option) (*Watcher, error) Create a new watcher
(*Watcher).OnChange(fn func([]Event)) Register change callback
(*Watcher).Start(ctx context.Context) error Start watching (blocks)
(*Watcher).Close() error Stop watching
Paths(paths ...string) Option Set directories to watch
Glob(patterns ...string) Option Set include patterns
Ignore(patterns ...string) Option Set exclude patterns
Debounce(d time.Duration) Option Set debounce interval
PollInterval(d time.Duration) Option Set poll frequency
Recursive(enabled bool) Option Enable/disable recursive watching
Event File system change event
Op Operation type (Create, Modify, Delete)

Development

go test ./...
go vet ./...

License

MIT

Documentation

Overview

Package fswatch provides a polling-based file system watcher for Go.

It uses polling (not OS-level events) to detect file changes, which means zero external dependencies. File modification times are tracked via os.Stat, directories are walked with filepath.WalkDir, and glob matching uses filepath.Match from the standard library.

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 of the affected file.
	Path string
	// Op is the operation that triggered the event.
	Op Op
	// ModTime is the last modification time of the file at the time the event was detected.
	ModTime time.Time
}

Event represents a file system change event.

type Op

type Op int

Op describes a set of file operations.

const (
	// Create indicates a new file was detected.
	Create Op = iota + 1
	// Modify indicates a file's content changed.
	Modify
	// Delete indicates a file was removed.
	Delete
)

func (Op) String

func (o Op) String() string

String returns a human-readable representation of the operation.

type Option

type Option func(*config)

Option configures a Watcher.

func Debounce

func Debounce(d time.Duration) Option

Debounce sets the debounce interval. Events are batched and delivered after no new events have been detected for this duration. Default is 500ms.

func Glob

func Glob(patterns ...string) Option

Glob sets file patterns to include (e.g., "*.yaml", "*.go"). Patterns are matched using filepath.Match against the file's base name. If no glob patterns are set, all files are included.

func Ignore

func Ignore(patterns ...string) Option

Ignore sets patterns to exclude (e.g., ".git", "*.tmp", "node_modules"). Patterns are matched using filepath.Match against the file's base name.

func Paths

func Paths(paths ...string) Option

Paths sets the directories to watch.

func PollInterval

func PollInterval(d time.Duration) Option

PollInterval sets how often the watcher checks for file system changes. Default is 1s.

func Recursive

func Recursive(enabled bool) Option

Recursive sets whether subdirectories are watched. Default is true.

type Watcher

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

Watcher watches directories for file system changes using polling.

func New

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

New creates a new Watcher with the given options. At least one path must be specified via the Paths option.

func (*Watcher) Close

func (w *Watcher) Close() error

Close stops the watcher. It is safe to call multiple times.

func (*Watcher) OnChange

func (w *Watcher) OnChange(fn func(events []Event))

OnChange registers a callback that is called with a batch of events whenever file system changes are detected. Only one callback can be registered; subsequent calls overwrite the previous callback.

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

Start begins watching for file system changes. It blocks until the provided context is cancelled or Close is called.

Jump to

Keyboard shortcuts

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