watcher

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

README

watcher

Installation

go get -u github.com/mrzwzw/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.

Example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/mrzwzw/watcher"
)

type File struct {
}
func (f *File) ListRecursive(name string, ignored map[string]struct{}, ignoreHidden bool,
	ffh []watcher.FilterFileHookFunc) (map[string]os.FileInfo, error) {
    //
}

func main() {
    //
    w := watcher.NewWatcher(new(File), "devdb")

	// 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)
	}
}

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 Entry

type Entry struct {
	*ftp.Entry
}

func (*Entry) IsDir

func (en *Entry) IsDir() bool

func (*Entry) ModTime

func (en *Entry) ModTime() time.Time

func (*Entry) Mode

func (en *Entry) Mode() os.FileMode

func (*Entry) Name

func (en *Entry) Name() string

func (*Entry) Size

func (en *Entry) Size() int64

func (*Entry) Sys

func (en *Entry) Sys() interface{}

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

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

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 Recursive

type Recursive interface {
	ListRecursive(name string, ignored map[string]struct{},
		ignoreHidden bool, fns []FilterFileHookFunc) (map[string]os.FileInfo, error)
}

type Watcher

type Watcher struct {
	ID string

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

Watcher describes a process that watches files for changes.

func NewWatcher

func NewWatcher(recursive Recursive, id ...string) *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

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) ClearOffsetByPath added in v1.0.1

func (w *Watcher) ClearOffsetByPath(fn string) error

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) GetOffset

func (w *Watcher) GetOffset(fn string) uint64

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, filterTime ...time.Duration) error

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

func (*Watcher) UpdateOffset

func (w *Watcher) UpdateOffset(fn string, recentCount uint64) error

UpdateOffset 将文件fn最近读取的计数recentCount存入.

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

Jump to

Keyboard shortcuts

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