converge/cmd/fsnotifytest/monitor.go
2024-08-13 21:33:29 +02:00

59 lines
949 B
Go

package main
import (
"fmt"
"log"
"github.com/fsnotify/fsnotify"
)
// create .hold
// CREATE + CHMOD
// remove
// REMOVE
// touch existing:
// CHMOD
// echo > .hold
//
// file name: ./.hold on linux and just .hold on windows
func main() {
// Create a new watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Start listening for events
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
fmt.Printf("Event: %s File: %s\n", event.Op, event.Name)
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("Modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("Error:", err)
}
}
}()
// Add a directory to watch
err = watcher.Add(".")
if err != nil {
log.Fatal(err)
}
// Block main goroutine forever
<-make(chan struct{})
}