Skip to content

Instantly share code, notes, and snippets.

@sadbox
Last active August 29, 2015 14:04
Show Gist options
  • Save sadbox/dd9f8e3d8293e465bc72 to your computer and use it in GitHub Desktop.
Save sadbox/dd9f8e3d8293e465bc72 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"log"
"os"
"path/filepath"
"github.com/go-fsnotify/fsnotify"
)
func addRecursive(filename string, watcher *fsnotify.Watcher) {
filepath.Walk(filename, func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() {
return nil
}
err = watcher.Add(path)
if err != nil {
log.Printf("Error watching %s: %s", path, err)
return nil
}
log.Println("Watching:", path)
return nil
})
}
func main() {
dir := flag.String("dir", ".", "Directory to watch!")
flag.Parse()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
addRecursive(*dir, watcher)
for {
select {
case err := <-watcher.Errors:
log.Println("Error:", err)
case event := <-watcher.Events:
log.Println(event)
if event.Op&fsnotify.Create == fsnotify.Create {
addRecursive(event.Name, watcher)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment