watcher

package module
Version: v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2019 License: BSD-3-Clause Imports: 9 Imported by: 320

README

watcher

Build Status

watcher is a Go package for watching for files or directory changes (recursively or non recursively) without using filesystem events, which allows it to work cross platform consistently.

watcher watches for changes and notifies over channels either anytime an event or an error has occurred.

Events contain the os.FileInfo of the file or directory that the event is based on and the type of event and file or directory path.

Installation
Features
Example
Contributing
Watcher Command

Update

  • Event.OldPath has been added [Aug 17, 2019]
  • Added new file filter hooks (Including a built in regexp filtering hook) [Dec 12, 2018]
  • Event.Path for Rename and Move events is now returned in the format of fromPath -> toPath
Chmod event is not supported under windows.

Installation

go get -u github.com/radovskyb/watcher/...

Features

  • Customizable polling interval.
  • Filter Events.
  • Watch folders recursively or non-recursively.
  • Choose to ignore hidden files.
  • Choose to ignore specified files and folders.
  • Notifies the os.FileInfo of the file that the event is based on. e.g Name, ModTime, IsDir, etc.
  • Notifies the full path of the file that the event is based on or the old and new paths if the event was a Rename or Move event.
  • Limit amount of events that can be received per watching cycle.
  • List the files being watched.
  • Trigger custom events.

Todo

  • Write more tests.
  • Write benchmarks.

Example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/radovskyb/watcher"
)

func main() {
	w := watcher.New()

	// SetMaxEvents to 1 to allow at most 1 event's to be received
	// on the Event channel per watching cycle.
	//
	// If SetMaxEvents is not set, the default is to send all events.
	w.SetMaxEvents(1)

	// Only notify rename and move events.
	w.FilterOps(watcher.Rename, watcher.Move)

	// Only files that match the regular expression during file listings
	// will be watched.
	r := regexp.MustCompile("^abc$")
	w.AddFilterHook(watcher.RegexFilterHook(r, false))

	go func() {
		for {
			select {
			case event := <-w.Event:	
				fmt.Println(event) // Print the event's info.
			case err := <-w.Error:
				log.Fatalln(err)
			case <-w.Closed:
				return
			}
		}
	}()

	// Watch this folder for changes.
	if err := w.Add("."); err != nil {
		log.Fatalln(err)
	}

	// Watch test_folder recursively for changes.
	if err := w.AddRecursive("../test_folder"); err != nil {
		log.Fatalln(err)
	}

	// Print a list of all of the files and folders currently
	// being watched and their paths.
	for path, f := range w.WatchedFiles() {
		fmt.Printf("%s: %s\n", path, f.Name())
	}

	fmt.Println()

	// Trigger 2 events after watcher started.
	go func() {
		w.Wait()
		w.TriggerEvent(watcher.Create, nil)
		w.TriggerEvent(watcher.Remove, nil)
	}()

	// Start the watching process - it'll check for changes every 100ms.
	if err := w.Start(time.Millisecond * 100); err != nil {
		log.Fatalln(err)
	}
}

Contributing

If you would ike to contribute, simply submit a pull request.

Command

watcher comes with a simple command which is installed when using the go get command from above.

Usage

Usage of watcher:
  -cmd string
    	command to run when an event occurs
  -dotfiles
    	watch dot files (default true)
  -ignore string
        comma separated list of paths to ignore
  -interval string
    	watcher poll interval (default "100ms")
  -keepalive
    	keep alive when a cmd returns code != 0
  -list
    	list watched files on start
  -pipe
    	pipe event's info to command's stdin
  -recursive
    	watch folders recursively (default true)
  -startcmd
    	run the command when watcher starts

All of the flags are optional and watcher can also be called by itself:

watcher

(watches the current directory recursively for changes and notifies any events that occur.)

A more elaborate example using the watcher command:

watcher -dotfiles=false -recursive=false -cmd="./myscript" main.go ../

In this example, watcher will ignore dot files and folders and won't watch any of the specified folders recursively. It will also run the script ./myscript anytime an event occurs while watching main.go or any files or folders in the previous directory (../).

Using the pipe and cmd flags together will send the event's info to the command's stdin when changes are detected.

First create a file called script.py with the following contents:

import sys

for line in sys.stdin:
	print (line + " - python")

Next, start watcher with the pipe and cmd flags enabled:

watcher -cmd="python script.py" -pipe=true

Now when changes are detected, the event's info will be output from the running python script.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDurationTooShort occurs when calling the watcher's Start
	// method with a duration that's less than 1 nanosecond.
	ErrDurationTooShort = errors.New("error: duration is less than 1ns")

	// ErrWatcherRunning occurs when trying to call the watcher's
	// Start method and the polling cycle is still already running
	// from previously calling Start and not yet calling Close.
	ErrWatcherRunning = errors.New("error: watcher is already running")

	// ErrWatchedFileDeleted is an error that occurs when a file or folder that was
	// being watched has been deleted.
	ErrWatchedFileDeleted = errors.New("error: watched file or folder deleted")

	// ErrSkip is less of an error, but more of a way for path hooks to skip a file or
	// directory.
	ErrSkip = errors.New("error: skipping file")
)

Functions

This section is empty.

Types

type Event

type Event struct {
	Op
	Path    string
	OldPath string
	os.FileInfo
}

An Event describes an event that is received when files or directory changes occur. It includes the os.FileInfo of the changed file or directory and the type of event that's occurred and the full path of the file.

func (Event) String

func (e Event) String() string

String returns a string depending on what type of event occurred and the file name associated with the event.

type FilterFileHookFunc added in v1.0.5

type FilterFileHookFunc func(info os.FileInfo, fullPath string) error

FilterFileHookFunc is a function that is called to filter files during listings. If a file is ok to be listed, nil is returned otherwise ErrSkip is returned.

func RegexFilterHook added in v1.0.5

func RegexFilterHook(r *regexp.Regexp, useFullPath bool) FilterFileHookFunc

RegexFilterHook is a function that accepts or rejects a file for listing based on whether it's filename or full path matches a regular expression.

type Op

type Op uint32

An Op is a type that is used to describe what type of event has occurred during the watching process.

const (
	Create Op = iota
	Write
	Remove
	Rename
	Chmod
	Move
)

Ops

func (Op) String

func (e Op) String() string

String prints the string version of the Op consts

type Watcher

type Watcher struct {
	Event  chan Event
	Error  chan error
	Closed chan struct{}
	// contains filtered or unexported fields
}

Watcher describes a process that watches files for changes.

func New

func New() *Watcher

New creates a new Watcher.

func (*Watcher) Add

func (w *Watcher) Add(name string) (err error)

Add adds either a single file or directory to the file list.

func (*Watcher) AddFilterHook added in v1.0.5

func (w *Watcher) AddFilterHook(f FilterFileHookFunc)

AddFilterHook

func (*Watcher) AddRecursive

func (w *Watcher) AddRecursive(name string) (err error)

AddRecursive adds either a single file or directory recursively to the file list.

func (*Watcher) Close

func (w *Watcher) Close()

Close stops a Watcher and unlocks its mutex, then sends a close signal.

func (*Watcher) FilterOps

func (w *Watcher) FilterOps(ops ...Op)

FilterOps filters which event op types should be returned when an event occurs.

func (*Watcher) Ignore

func (w *Watcher) Ignore(paths ...string) (err error)

Ignore adds paths that should be ignored.

For files that are already added, Ignore removes them.

func (*Watcher) IgnoreHiddenFiles

func (w *Watcher) IgnoreHiddenFiles(ignore bool)

IgnoreHiddenFiles sets the watcher to ignore any file or directory that starts with a dot.

func (*Watcher) Remove

func (w *Watcher) Remove(name string) (err error)

Remove removes either a single file or directory from the file's list.

func (*Watcher) RemoveRecursive

func (w *Watcher) RemoveRecursive(name string) (err error)

RemoveRecursive removes either a single file or a directory recursively from the file's list.

func (*Watcher) SetMaxEvents

func (w *Watcher) SetMaxEvents(delta int)

SetMaxEvents controls the maximum amount of events that are sent on the Event channel per watching cycle. If max events is less than 1, there is no limit, which is the default.

func (*Watcher) Start

func (w *Watcher) Start(d time.Duration) error

Start begins the polling cycle which repeats every specified duration until Close is called.

func (*Watcher) TriggerEvent

func (w *Watcher) TriggerEvent(eventType Op, file os.FileInfo)

TriggerEvent is a method that can be used to trigger an event, separate to the file watching process.

func (*Watcher) Wait

func (w *Watcher) Wait()

Wait blocks until the watcher is started.

func (*Watcher) WatchedFiles

func (w *Watcher) WatchedFiles() map[string]os.FileInfo

WatchedFiles returns a map of files added to a Watcher.

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL