Last active
August 29, 2015 14:04
-
-
Save sadbox/dd9f8e3d8293e465bc72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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