watcher

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package watcher provides file update notification functionality as a module.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingWithTarget = fmt.Errorf("watcher.Watcher missing or empty WithTarget option")
	ErrMissingWithFunc   = fmt.Errorf("watcher.Watcher missing WithFunc option")
)

Functions

This section is empty.

Types

type Opt

type Opt func(*Watcher) error

func WithFunc

func WithFunc(fn func() error) Opt

func WithTarget

func WithTarget(fileOrDirName string) Opt

type Watcher

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

func New

func New(opts ...Opt) *Watcher

New creates watcher with given options. WithTarget and WithFunc options are mandatory.

Example
tmpFile, err := os.CreateTemp("", testFilePattern)
if err != nil {
	fmt.Println(err)
	return
}
defer os.Remove(tmpFile.Name())

t := ticker.New(
	ticker.WithInterval(time.Second),
	ticker.WithFunc(func() error {
		_, err = tmpFile.Write([]byte("updated"))
		if err != nil {
			return err
		}
		return nil
	}),
)

w := watcher.New(
	watcher.WithTarget(tmpFile.Name()),
	watcher.WithFunc(func() error {
		slog.Info("Hello from watcher")
		return errors.New("watcher error")
	}),
)

err = service.Run(service.Modules{t, w})
if err != nil {
	fmt.Println(err)
}
Output:

1 error occurred:
	* failed to run module watcher.Watcher: watcher error

func (*Watcher) Init

func (w *Watcher) Init() error

func (*Watcher) Name

func (w *Watcher) Name() string

func (*Watcher) Run

func (w *Watcher) Run() error

func (*Watcher) Stop

func (w *Watcher) Stop() error

Jump to

Keyboard shortcuts

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