72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
// 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
|
|
}
|