Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Watch for created directories.
package main
import (
"log"
"os"
"path/filepath"
"github.com/howeyc/fsnotify"
)
func watchAllDirs(watcher *fsnotify.Watcher, root string) (err error) {
walkFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
watcher.Watch(path)
log.Println("Added Watch: ", path)
}
return nil
}
return filepath.Walk(root, walkFn)
}
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
if ev.IsCreate() {
if finfo, err := os.Stat(ev.Name); err == nil && finfo.IsDir() {
watcher.Watch(ev.Name)
log.Println("Added Watch: ", ev.Name)
}
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
done <- true
}()
err = watchAllDirs(watcher, "test")
if err != nil {
log.Fatal(err)
}
<-done
watcher.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.