dirwatch

package module
v0.0.0-...-2f0afdf Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 License: MIT Imports: 9 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dirwatch

For watching for changes inside a directory and all sub-directories, recursively. Uses the package fsnotify.

Documentation:

GoDoc

Sample Usage

notify := func(ev Event) {
	// processing the event ev
}

// create the watcher which excludes
// any folder along the added paths
// that matches provided pattern(s).
watcher := New(Notify(notify), Exclude("/*/*/node_modules"))
defer watcher.Stop()
watcher.Add(dir1, true)
watcher.Add(dir2, false)
watcher.Add(dir3, true)
Environment:
  • Ubuntu 18.04
  • Go 1.10.3
TODO:
  • more tests

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Name string
	Op   fsnotify.Op
}

Event represents a single file system notification.

type Option

type Option func(*options)

Option modifies the options.

func Exclude

func Exclude(exclude ...string) Option

Exclude sets patterns to exclude from watch.

func Logger

func Logger(logger func(args ...interface{})) Option

Logger sets the logger for the watcher.

func Notify

func Notify(notify func(Event)) Option

Notify sets the notify callback.

type Watcher

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

Watcher watches over a directory and it's sub-directories, recursively.

Example (Recursive)
// prepare sample home directory to watch over
// rootDirectory, err := ioutil.TempDir(os.TempDir(), "dirwatch-example-")
rootDirectory := filepath.Join(os.TempDir(), "dirwatch-example-exclude")
os.RemoveAll(rootDirectory)
os.Mkdir(rootDirectory, 0777)

os.MkdirAll(filepath.Join(rootDirectory, "node_modules"), 0777)
os.MkdirAll(filepath.Join(rootDirectory, "lab1"), 0777)
os.MkdirAll(filepath.Join(rootDirectory, "lab2"), 0777)

// our notification callback (I feel it's simpler to
// have a callback instead of passing a channel in an API)
var events = make(chan Event, 100)
notify := func(ev Event) {
	events <- ev
}

// create the watcher
watcher := New(Notify(notify), Exclude("/*/*/node_modules"))
defer watcher.Stop()
watcher.Add(rootDirectory, true)
<-time.After(time.Millisecond * 500)

go func() {
	defer func() {
		events <- Event{Name: "ALLDONE"}
	}()
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "node_modules", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "lab1", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "lab2", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
}()

count := 0
for v := range events {
	if v.Name == "ALLDONE" {
		break
	}
	if strings.Contains(v.Name, "LVL2.txt") {
		count++
		continue
	}
}
fmt.Println(count)
Output:
4
Example (Simple)
dir := prep()

notify := func(ev Event) {
	fmt.Println(filepath.Base(ev.Name))
}

// create the watcher
watcher := New(Notify(notify))
defer watcher.Stop()
watcher.Add(dir, true)

ioutil.WriteFile(filepath.Join(dir, "text.txt"), nil, 0777)
<-time.After(time.Millisecond * 300)
Output:
text.txt
Example (SimpleExclude)
// prepare sample home directory to watch over
// rootDirectory, err := ioutil.TempDir(os.TempDir(), "dirwatch-example-")
rootDirectory := filepath.Join(os.TempDir(), "dirwatch-example-exclude")
os.RemoveAll(rootDirectory)
os.Mkdir(rootDirectory, 0777)

// our notification callback (I feel it's simpler to
// have a callback instead of passing a channel in an API)
var events = make(chan Event, 100)
notify := func(ev Event) {
	events <- ev
}

// create the watcher
watcher := New(Notify(notify), Exclude("/*/*/node_modules"))
defer watcher.Stop()
watcher.Add(rootDirectory, true)
<-time.After(time.Millisecond * 500)

go func() {
	defer func() {
		events <- Event{Name: "ALLDONE"}
	}()
	os.MkdirAll(filepath.Join(rootDirectory, "node_modules"), 0777)
	os.MkdirAll(filepath.Join(rootDirectory, "lab1"), 0777)
	os.MkdirAll(filepath.Join(rootDirectory, "lab2"), 0777)
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "node_modules", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "lab1", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
	if err := ioutil.WriteFile(filepath.Join(rootDirectory, "lab2", "LVL2.txt"), []byte("TEST"), 0777); err != nil {
		panic(err)
	}
	<-time.After(time.Millisecond * 10)
}()

count := 0
for v := range events {
	if v.Name == "ALLDONE" {
		break
	}
	if strings.Contains(v.Name, "LVL2.txt") {
		count++
		continue
	}
}
fmt.Println(count)
Output:
4

func New

func New(opt ...Option) *Watcher

New creates a new *Watcher. Excluded patterns are based on filepath.Match function patterns.

func (*Watcher) Add

func (dw *Watcher) Add(path string, recursive bool)

Add adds a path to be watched.

func (*Watcher) Stop

func (dw *Watcher) Stop()

Stop stops the watcher. Safe to be called mutiple times.

Jump to

Keyboard shortcuts

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