Skip to content
Snippets Groups Projects

Watch

What does this library?

Lighthouse Watch is a Go library for efficiently watching file and directory changes in your filesystem. Built on top of the powerful fsnotify library, Lighthouse offers a simple, yet highly flexible, API to monitor changes in real-time.

Features

  • Real-time file and directory change events
  • Customizable event handlers for different types of operations (Create, Write, Rename, Remove)
  • Debounce mechanism to group rapid events
  • Thread-safe operations
  • Error handling

Installation

Install the package using Go's package manager:

go get gitlab.schukai.com/oss/libraries/go/utilities/watch

Note: This library uses Go Modules to manage dependencies.

Usage

package main

import (
	"fmt"
	"gitlab.schukai.com/oss/libraries/go/utilities/watch"
)

func main() {
	// Initialize Lighthouse
	l := watch.NewLighthouse()

	// Define Watch instance
	w1 := &watch.Watch{
		Path: "/path/to/watch",
		OnCreate: func(name string) {
			fmt.Println("File created:", name)
		},
		OnChange: func(name string) {
			fmt.Println("File changed:", name)
		},
		OnRename: func(name string) {
			fmt.Println("File renamed:", name)
		},
		OnDelete: func(name string) {
			fmt.Println("File deleted:", name)
		},
	}

	// Add watch
	err := l.Add(w1)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Start watching
	l.StartWatching()
}

Add Multiple Watches

w2 := &watch.Watch{ /*...*/ }
err = l.Add(w2)

Remove a Watch

err = l.Remove("/path/to/watch")

Error Handling

l.SetOnError(func(err error) {
    fmt.Println("Error:", err)
})

Debounce

// Debounce time in milliseconds
l.SetDebounce(1000)

Contributing

Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

Versioning is done with SemVer. Changelog is generated with git-chglog

Commit messages should follow the Conventional Commits specification. Messages are started with a type, which is one of the following:

  • feat: A new feature
  • fix: A bug fix
  • doc: Documentation only changes
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance
  • test: Adding missing or correcting existing tests
  • chore: Other changes that don't modify src or test files

The footer would be used for a reference to an issue or a breaking change.

A commit that has a footer BREAKING CHANGE:, or appends a ! after the type/scope, introduces a breaking API change (correlating with MAJOR in semantic versioning). A BREAKING CHANGE can be part of commits of any type.

the following is an example of a commit message:

feat: add 'extras' field

License

AGPL-3.0