From 1d0bd01fe75791b57b04e4a484751dd4dfd7affc Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 13 Oct 2016 15:58:32 -0400 Subject: [PATCH] *: initial pass at fsnotify Signed-off-by: Vincent Batts --- app.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 app.go diff --git a/app.go b/app.go new file mode 100644 index 0000000..e42dab1 --- /dev/null +++ b/app.go @@ -0,0 +1,72 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !plan9 + +package main + +import ( + "flag" + "log" + + "github.com/fsnotify/fsnotify" +) + +/* +29044 2016-09-21 12:40:21 cd x +29045 2016-09-21 12:40:25 touch file +29046 2016-09-21 12:40:39 rm file +29047 2016-09-21 12:40:42 touch file +29048 2016-09-21 12:40:50 echo farts > file +29049 2016-09-21 12:40:57 rm file + +produces: +$ ./sync_pass x +2016/09/21 12:40:25 event: "x/file": CREATE +2016/09/21 12:40:25 event: "x/file": CHMOD +2016/09/21 12:40:39 event: "x/file": REMOVE +2016/09/21 12:40:42 event: "x/file": CREATE +2016/09/21 12:40:42 event: "x/file": CHMOD +2016/09/21 12:40:50 event: "x/file": WRITE +2016/09/21 12:40:50 modified file: x/file +2016/09/21 12:40:50 event: "x/file": WRITE +2016/09/21 12:40:50 modified file: x/file +2016/09/21 12:40:57 event: "x/file": REMOVE +*/ +func main() { + flag.Parse() + + for _, arg := range flag.Args() { + WathPath(arg) + } +} + +func WathPath(path string) { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + done := make(chan bool) + go func() { + for { + select { + case event := <-watcher.Events: + log.Println("event:", event) + if event.Op&fsnotify.Write == fsnotify.Write { + log.Println("modified file:", event.Name) + } + case err := <-watcher.Errors: + log.Println("error:", err) + } + } + }() + + err = watcher.Add(path) + if err != nil { + log.Fatal(err) + } + <-done +}