Gorph is a Recursive PHile watcher

Sean MacdonaldSean Macdonald
1 min read

In order to power a project of mine, I needed a cross-platform file-watcher. I noticed that fsnotify offers the broadest support, but does not offer recursive watching. Gorph augments fsnotify, providing this functionality using doublestar, providing an elegant and flexible way to expose recursive functionality.

You can use Gorph as a binary of package. To use it as a binary, do this:

$ # get the binary
$ go install github.com/sean9999/go-fsnotify-recursively/cmd/gorph@latest
$ # watch current directory recursively
$ gorph
$ # watch Downloads recursively for .jpg files
$ gorph ~/Downloads/**/*.jpg
$ # watch Documents non-recursively
$ gorph ~/Documents/*

It can also be used as a package:

package main

import (
    "io/fs"
    "os"
    "reflect"
    "sync"
    "testing"

    gorph "github.com/sean9999/go-fsnotify-recursively"
)

func main(){

    // watch current directory recursively
    watcher := gorph.New("**")

    // two channels
    fsEvents, gorphErrors := watcher.Listen()

    // now do whatever you want with the channels
    ...

    // close it when you're done
    watcher.Close() 

}
0
Subscribe to my newsletter

Read articles from Sean Macdonald directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sean Macdonald
Sean Macdonald

I am a developer from Toronto.