*: initial pass at fsnotify
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
commit
1d0bd01fe7
1 changed files with 72 additions and 0 deletions
72
app.go
Normal file
72
app.go
Normal file
|
@ -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
|
||||||
|
}
|
Reference in a new issue