Documentation ¶
Overview ¶
Package gowatch implements utilities for watching a subset of directories and running sequences of actions when given files change.
The core of gowatch is gowatch.Config, which holds the following:
1. A list of named scripts (actions and services). An action script is something that exits, while a service does not. For example, starting a server is considered a service, since it continues to run in the background. Each script has a name that can be referenced by other parts of the config.
2. A list of scripts to run on startup of gowatch.
3. A list of file triggers. The combined list of file triggers indicate which directories gowatch should watch for file changes.
A file trigger holds the following:
1. The files/directories to include in the trigger.
2. The files/directories to exclude from the included list.
3. The scripts to trigger when a file in the watched list changes.
Include and exclude patterns can be glob expressions to a path relative to the working directory of the gowatch to an absolute path. Glob expressions match * to a directory and ** to any level of subdirectories. For example, src/**/*.js matches any JS file in any subfolder of src.
To have a pattern that only matches against directories, append a / at the end of the pattern. For example, **/ matches all directories.
Relative patterns will match relative to the working directory as defined by gowatch.Watcher. Absolute paths can still be used to watch paths outside of the working directory.
Triggers ¶
When a sequence of scripts is triggered, actions will be fired off and gowatch will wait for them to finish before going to the next script in the sequence. If the script is a service, one of two things will happen: It will be started if it is not currently running, and it will be restarted if it is.
File System Events ¶
File Triggers are collected in batches in case of many files changing at once. Whenever a file change is detected, a 250ms timer starts. All other file changes within that 250ms window will be collected. After that 250ms timer expires, all file updates collected in that batch will be analyzed and the proper triggers will fire.
Trigger Priority ¶
Triggers run in the order as defined in the trigger list. If multiple file_triggers match, the actions and services will be ran in definition order with duplicates removed. Each action will be run to completion before the next one is started.
Trigger Cancellation ¶
If another trigger event occurs while one or more triggers is queued up to run, then the queue will be cancelled and the running trigger will be aborted.
Here is an example configuration YAML file for a NodeJS project that uses gulp:
actions: install: npm i build: gulp build services: run: gulp start on_start: - install - build - run file_triggers: - include: ["/tmp/cache.lock"] trigger: - build - run exclude: ["node_modules/", "build/", "package.json", "package-lock.json"] trigger: - build - run - include: - package.json - package-lock.json trigger: - install - build - run
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Actions is a named list of oneshot scripts. Actions map[string]string `yaml:"actions"` // Services is a named list of long-running scripts that are intended to not exit. Services map[string]string `yaml:"services"` // StartupSteps holds the list of actions and services to run on start. StartupSteps []string `yaml:"on_start"` // FileTriggers holds a list of file events to watch for and a list of // scripts to execute when a matching event occurs. This is a sorted list; // earlier triggers are treated as higher precedence and will execute // first. FileTriggers []FileTrigger `yaml:"file_triggers"` }
Config holds the configuration for the directory tree that will be watched and the scripts that will be ran on it.
type FileTrigger ¶
type FileTrigger struct { // Include holds patterns to include when checking if the file trigger // is activated. A * matches all files. Include []string `yaml:"include"` // Exclude holds patterns to ignore when checking if the file trigger // is activated. Exclude []string `yaml:"exclude"` // Triggers holds the list of scripts and services to trigger when the // file trigger is detected. Triggers []string `yaml:"trigger"` }
A FileTrigger is a pattern of whitelisted and blacklisted files that will invoke a series of steps when a file within the watched list changes.
type Watcher ¶
type Watcher struct { // Working directory to watch Directory string // The writer for debug output to go to. Debug io.Writer // The writer for triggers to write output to Stdout io.Writer // The writer for triggers to write errors to Stderr io.Writer // Config of file triggers and events to run Config Config // contains filtered or unexported fields }
Watcher is the instance of the watcher itself. It holds the configuration for the directory tree to be watched and the root directory to watch.
func NewWatcher ¶
NewWatcher returns a new Watcher given a directory to watch and a config with file patterns and triggers.
func NewWatcherWithContext ¶
NewWatcherWithContext returns a new Watcher given a directory to watch and a config with file patterns and triggers. It accepts a context that, when the watcher is started, allows for cancellation.
func (*Watcher) MatchingTriggers ¶
func (w *Watcher) MatchingTriggers(path string) (triggers []FileTrigger, err error)
MatchingTriggers takes a full path to a file and returns all trigers that match that path.
func (*Watcher) Run ¶
Run runs a specific named trigger defined from the watcher's config. The trigger can either be a service or an action.
func (*Watcher) Start ¶
Start starts the watcher. Start should not exit normally unless an error occurred or the watcher is cancelled through the context passed to NewWatchWithContext.
func (*Watcher) WatchedPaths ¶
WatchedPaths returns the list of files and directories that will be monitored by the watcher. Each path is the absolute path on disk.