fswatch

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: BSD-2-Clause Imports: 3 Imported by: 0

README

FSWatch

GoDoc Go Report Card

FSWatch is like fsnotfiy, but debounces the emitted events for better handling. E.g. when a (large) file is created, fswatch debounces the Created event until the writing is done.

It uses fsnotfiy internally.

go get github.com/Q1-Energie-AG/fswatch

Sample code

package main

import (
 	"log"
	"time"
  
	"github.com/q1-energie-ag/fswatch"
	"gopkg.in/fsnotify.v1"
)


func main() {

	// Create a new watcher which waits 10 seconds for new events
	// after the inital event was emitted
	watcher, err := fswatch.NewWatcher(10 * time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()
	
	done := make(chan bool)
	go func() {
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add("/tmp/foo")
	if err != nil {
		log.Fatal(err)
	}
	<-done
	
	
}

Documentation

Overview

Package fswatch provides a platform-independent filewatcher which debounces events to avoid using files before they are entirly written to disk

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Watcher

type Watcher struct {
	// IgnoreTemporaryFiles indicates if files which
	// are CREATEd and DELETEd during the debounce duration are ignored
	// The default value is true.
	// If this is false, only the DELETE event is emitted right after it occurs.
	IgnoreTemporaryFiles bool

	// Events is the channel on which all events are published
	Events chan fsnotify.Event

	// Errors is the channel on which all errors are published
	Errors chan error
	// contains filtered or unexported fields
}

Watcher is a debounced filewatcher If a CREATE / WRITE happens it waits for {debounceDuration} to publish the event and resets the {debounceDuration} when a new WRITE event is created for the file

func NewWatcher

func NewWatcher(debounceDuration time.Duration) (*Watcher, error)

NewWatcher creates a new watcher with the specified debounceDuration

func (*Watcher) Add

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

Add adds a new path to the watcher

func (*Watcher) Close

func (w *Watcher) Close() error

Close closes the watcher

func (*Watcher) Remove

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

Remove removes a path from the watcher

Jump to

Keyboard shortcuts

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