watcher

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSkippedPaths = []string{"node_modules", "testdata", "vendor"}

DefaultSkipped is a list of paths that will be skipped by Watcher.

Functions

This section is empty.

Types

type EventBatch

type EventBatch struct {
	Events map[string]Op // map of file paths and operations.
}

EventBatch published by Watcher when changes to Go source files are detected within watched directories.

func (EventBatch) Paths

func (e EventBatch) Paths() []string

Paths returns unique file paths in event batch.

type Op

type Op int

Op represents a file operation.

const (
	Create Op = iota // file created.
	Write            // file was written to.
	Delete           // file was deleted.
)

func (Op) String

func (o Op) String() string

String representation of file operation.

type Option

type Option func(*Watcher)

Option configures a Watcher.

func WithBatchInterval

func WithBatchInterval(interval time.Duration) Option

WithBatchInterval configures a Watcher to use interval for event batches.

By default, watcher checks every 250ms if file events are ready to be published in a batch.

func WithLogger

func WithLogger(logger *log.Logger) Option

WithLogger configures Watcher to use given logger.

func WithSkipPaths

func WithSkipPaths(paths ...string) Option

WithSkipPaths configures Watcher with additional paths to skip.

Paths in DefaultSkippedPaths will always be skipped, as well as paths prefixed with `.` like `.git`.

type Watcher

type Watcher struct {
	Events chan *EventBatch
	// contains filtered or unexported fields
}

Watcher watches a directory, and its sub directories, for changes to Go source files and publishes the events over a channel for other components to take action on them.

func New

func New(ctx context.Context, rootDir string, opts ...Option) *Watcher

New Watcher to monitor a directory and its sub directories for changes to Go source files.

This function only constructs the Watcher. Call Watcher.Watch to start the actual monitoring.

func (*Watcher) Close

func (w *Watcher) Close() error

Close Watcher.

func (*Watcher) Watch

func (w *Watcher) Watch() error

Watch for changes to Go source files within the root directory and its sub directories.

All paths prefixed with `.`, like `.git`, as well as paths defined in DefaultSkippedPaths are automatically skipped by the watcher. Additional paths can be added to the skip list with the WithSkipPaths option.

If new directories are created within the root directory while Watcher is monitoring, they are automatically added to the watch list, unless they match a path to skip.

Jump to

Keyboard shortcuts

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