70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
)
|
|
|
|
func initHomeWatcher() (*fsnotify.Watcher, error) {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO filepath.Walk over /home, adding to watcher
|
|
paths := []string{"/home/vilmibm", "/home/wren"}
|
|
|
|
for _, path := range paths {
|
|
err = watcher.Add(path)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to watch path %s: %w", path, err)
|
|
continue
|
|
}
|
|
}
|
|
return watcher, nil
|
|
}
|
|
|
|
func watchHome(watcher *fsnotify.Watcher) {
|
|
for {
|
|
select {
|
|
case event, ok := <-watcher.Events:
|
|
if !ok {
|
|
return
|
|
}
|
|
log.Println("event:", event)
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
|
log.Println("modified file:", event.Name)
|
|
}
|
|
case err, ok := <-watcher.Errors:
|
|
if !ok {
|
|
return
|
|
}
|
|
log.Println("error:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func cli(args []string) int {
|
|
watcher, err := initHomeWatcher()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to create watcher: %w", err)
|
|
return 1
|
|
}
|
|
defer watcher.Close()
|
|
|
|
// TODO have an event bus to write to
|
|
// TODO what is this done for do i need this done
|
|
done := make(chan bool)
|
|
go watchHome(watcher)
|
|
<-done
|
|
|
|
return 0
|
|
}
|
|
|
|
func main() {
|
|
os.Exit(cli(os.Args))
|
|
}
|