filewatcher

package
v0.0.0-...-0e9fed5 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 4 Imported by: 2

README

FileWatcher

FileWatcher checks periodically if a file has changed and notifies if that is the case using a callback.

package main

import (
	"fmt"
	"bitbucket.org/HelgeOlav/utils/filewatcher"
	"time"
	"context"
)

func mycallback(fn string) {
	fmt.Println(fn, "has changed")
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	fw := filewatcher.NewFileWatcher(ctx, "file-to-watch", mycallback, time.Second)
	// do some work
	time.Sleep(time.Minute)
	cancel() // stop filewatcher for this file
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileWatcher

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

FileWatcher tracks file changes and issues a callback when a file has changed. The file is polled on given intervals to see if it has been changed and a callback is done if there is a change. If the file is deleted you will not get notified before a new file is in place.

func NewFileWatcher

func NewFileWatcher(ctx context.Context, filename string, callback func(string), interval time.Duration) (*FileWatcher, error)

NewFileWatcher creates a new filewatcher. The watcher is created if err is nil. A watcher is created even if the file does not exist when started.

Jump to

Keyboard shortcuts

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