dirwatch

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2018 License: MIT Imports: 10 Imported by: 0

README

dirwatch

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

Documentation:

GoDoc

Environment:

  • Ubuntu 16.04
  • Go 1.9.2

TODO:

  • more tests
  • study the case for removing directories more carefully

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(errlog func(args ...interface{}))

SetLogger replaces logger for this package. Must be called before using anything else from this package. Can be called only once and the consecutive calls have no effect.

Types

type Watch

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

Watch watches over a directory and it's sub-directories, recursively. Also watches files, if the path is explicitly provided. If a path does no longer exists, it will be removed.

func New

func New(notify func(fsnotify.Event), pathList ...string) *Watch

New creates a new *Watch

Example
// prepare sample home directory to watch over
rootDirectory, err := ioutil.TempDir("", "dirwatch-example")
if err != nil {
	panic(err)
}
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 fsnotify.Event, 100)
notify := func(ev fsnotify.Event) {
	events <- ev
}

// create the watcher
watcher := New(notify, rootDirectory)
defer watcher.Stop()

// creating a directory inside the root/home
dir2 := filepath.Join(rootDirectory, "lab2")
os.Remove(dir2)
err = os.Mkdir(dir2, 0777)
if err != nil {
	panic(err)
}

<-time.After(time.Millisecond * 100)

select {
case ev := <-events:
	if strings.Contains(ev.Name, "dirwatch-example") &&
		strings.Contains(ev.Name, "lab2") && ev.Op == fsnotify.Create {
		fmt.Println("OK")
	}
case <-time.After(time.Second * 3):
}
Output:

OK

func (*Watch) Add

func (dw *Watch) Add(pathList ...string)

Add paths

func (*Watch) Stop

func (dw *Watch) Stop()

Stop stops the watcher.

Jump to

Keyboard shortcuts

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