fswatch

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: GPL-2.0 Imports: 9 Imported by: 9

README

fswatch

Test Lint Go Reference

fswatch is a simple fsnotify wrapper to watch file updates correctly.

Install

go get github.com/sagernet/fswatch

Example

package main

import (
	"log"

	"github.com/sagernet/fswatch"
)

func main() {
	var watchPath []string
	watchPath = append(watchPath, "/tmp/my_file")
	watcher, err := fswatch.NewWatcher(fswatch.Options{
		Path: watchPath,
		Callback: func(path string) {
			log.Println("file updated: ", path)
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()
	// Block main goroutine forever.
	<-make(chan struct{})
}

Documentation

Index

Constants

View Source
const DefaultWaitTimeout = 100 * time.Millisecond

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Path is the list of files or directories to watch
	// It is the caller's responsibility to ensure that paths are absolute.
	Path []string

	// Direct is the flag to watch the file directly if file will never be removed
	Direct bool

	// Callback is the function to call when a file is updated
	Callback func(path string)

	// WaitTimeout is the time to wait write events before calling the callback
	// DefaultWaitTimeout is used by default
	WaitTimeout time.Duration

	// Logger is the logger to log errors
	// optional
	Logger logger.Logger
}

type Watcher

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

Watcher is a simple fsnotify wrapper to watch updates correctly.

func NewWatcher

func NewWatcher(options Options) (*Watcher, error)

func (*Watcher) Close

func (w *Watcher) Close() error

func (*Watcher) Start

func (w *Watcher) Start() error

Jump to

Keyboard shortcuts

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