Rename monitor to master

This commit is contained in:
Michael Wolf
2026-01-01 12:30:58 -06:00
parent 3bece46638
commit a178536472
9 changed files with 2 additions and 2 deletions

74
master/watchfiles.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"github.com/fsnotify/fsnotify"
"log"
"os"
"path/filepath"
)
func watchFiles(dir string, changes chan<- FileChange) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Add all directories recursively
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
err = watcher.Add(path)
if err != nil {
log.Printf("Error watching %s: %v\n", path, err)
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// Handle different types of events
var operation string
switch {
case event.Op&fsnotify.Write == fsnotify.Write:
operation = "MODIFIED"
case event.Op&fsnotify.Create == fsnotify.Create:
operation = "CREATED"
// If a new directory is created, start watching it
if info, err := os.Stat(event.Name); err == nil && info.IsDir() {
watcher.Add(event.Name)
}
case event.Op&fsnotify.Remove == fsnotify.Remove:
operation = "REMOVED"
case event.Op&fsnotify.Rename == fsnotify.Rename:
operation = "RENAMED"
case event.Op&fsnotify.Chmod == fsnotify.Chmod:
operation = "CHMOD"
default:
operation = "UNKNOWN"
}
changes <- FileChange{
Path: event.Name,
Operation: operation,
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("Watcher error: %v\n", err)
}
}
}