watcher

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 13 Imported by: 3

README

Watcher

Go Reference

Resilient file watcher. Used in Bud.

Features

  • Watches a directory recursively
  • Debounces multiple events into a single function call
  • Smooths over differences between operating systems
  • Ignores files in your .gitignore
  • Generally does what you'd expect

Install

go get -u github.com/livebud/watcher

Example

package main

import (
  "context"
  "fmt"
  "os"

  "github.com/livebud/watcher"
)

func main() {
  ctx := context.Background()
  if err := run(ctx); err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
}

func run(ctx context.Context) error {
  return watcher.Watch(ctx, ".", func(events []watcher.Event) error {
    for _, event := range events {
      fmt.Println(string(event.Op), event.Path)
    }
    return nil
  })
}

Contributing

Thank you for your interest in contributing! To get started, first clone the repo:

git clone https://github.com/livebud/watcher
cd watcher

Next, install the dependencies:

go mod tidy

Finally, try running the tests:

go test ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Stop = errors.New("stop watching")

Stop informs the watcher to stop.

Functions

func Watch

func Watch(ctx context.Context, dir string, fn func(events []Event) error) error

Watch a directory of files

Types

type Event

type Event struct {
	Op   Op
	Path string
}

Event is used to track file events

func (Event) String

func (e Event) String() string

type Op

type Op string

Op is the type of file event that occurred

const (
	OpCreate Op = "create"
	OpUpdate Op = "update"
	OpDelete Op = "delete"
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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