Implement folder mail notifier

This commit is contained in:
2024-08-25 21:48:47 +02:00
commit 128fdc5f85
5 changed files with 193 additions and 0 deletions

59
watcher.go Normal file
View File

@@ -0,0 +1,59 @@
package main
import (
"os"
"path/filepath"
"gopkg.in/fsnotify.v1"
)
func WatchRecursively(root string, changeHandler func(fsnotify.Event)) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
changeHandler(event)
case err, ok := <-watcher.Errors:
if !ok {
return
}
Error.Printf("error with watcher: %v", err)
}
}
}()
dirs := 0
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == ".git" {
return filepath.SkipDir
}
if info.IsDir() {
err = watcher.Add(path)
dirs++
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
<-done
return nil
}