Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ee5d8e3f25157403d71546b2cb609012d130cef3 0 parents
@mrnugget authored
22 .gitignore
@@ -0,0 +1,22 @@
+# Compiled Object files, Static and Dynamic libs (Shared Objects)
+*.o
+*.a
+*.so
+
+# Folders
+_obj
+_test
+
+# Architecture specific extensions/prefixes
+*.[568vq]
+[568vq].out
+
+*.cgo1.go
+*.cgo2.c
+_cgo_defun.c
+_cgo_gotypes.go
+_cgo_export.*
+
+_testmain.go
+
+*.exe
4 README.md
@@ -0,0 +1,4 @@
+watchgopher
+===========
+
+Watch a directory for changes
27 watchgopher.go
@@ -0,0 +1,27 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "github.com/mrnugget/watchgopher/watchgopher"
+ "os"
+ "time"
+)
+
+var interval = flag.Duration("interval", 60*time.Second, "when to check")
+var path string
+
+func main() {
+ flag.Parse()
+
+ path = flag.Arg(0)
+ if path == "" {
+ fmt.Println("You have to provide a path to watch")
+ os.Exit(1)
+ }
+ if path == "." {
+ path, _ = os.Getwd()
+ }
+
+ watchgopher.Watch(path, *interval)
+}
75 watchgopher/dir.go
@@ -0,0 +1,75 @@
+package watchgopher
+
+import (
+ "github.com/howeyc/fsnotify"
+ "io/ioutil"
+ "os"
+ "path"
+)
+
+func NewDir(path string) (d *Dir) {
+ files := make(map[string]os.FileInfo)
+ events := make(chan *fsnotify.FileEvent)
+
+ return &Dir{path, files, events}
+}
+
+type Dir struct {
+ Path string
+ Files map[string]os.FileInfo
+ Events chan *fsnotify.FileEvent
+}
+
+func (d *Dir) StartWatching() (err error) {
+ err = d.scan()
+ if err != nil {
+ return
+ }
+
+ watcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return
+ }
+
+ err = watcher.Watch(d.Path)
+ if err != nil {
+ return
+ }
+
+ go d.handleEvents(watcher.Event, watcher.Error)
+ return nil
+}
+
+func (d *Dir) handleEvents(events chan *fsnotify.FileEvent, errs chan error) {
+ for {
+ select {
+ case ev := <-events:
+ if ev.IsRename() || ev.IsDelete() {
+ delete(d.Files, ev.Name)
+ }
+ if ev.IsCreate() || ev.IsModify() {
+ fi, err := os.Stat(ev.Name)
+ if err != nil {
+ break
+ }
+ d.Files[ev.Name] = fi
+ }
+ d.Events <- ev
+ case <-errs:
+ break
+ }
+ }
+}
+
+func (d *Dir) scan() (err error) {
+ files, err := ioutil.ReadDir(d.Path)
+ if err != nil {
+ return
+ }
+
+ for _, f := range files {
+ d.Files[path.Join(d.Path, f.Name())] = f
+ }
+
+ return nil
+}
43 watchgopher/monitor.go
@@ -0,0 +1,43 @@
+package watchgopher
+
+import (
+ "fmt"
+ "time"
+)
+
+func Watch(path string, interval time.Duration) {
+ fmt.Println("PATH:", path)
+ fmt.Println("INTERVAL:", interval)
+ fmt.Println("Watchgopher is watching...")
+
+ dir := NewDir(path)
+ ticker := time.Tick(interval)
+
+ monitor := &Monitor{dir, ticker}
+ monitor.start()
+}
+
+type Monitor struct {
+ dir *Dir
+ ticker <-chan time.Time
+}
+
+func (m *Monitor) start() {
+ err := m.dir.StartWatching()
+ if err != nil {
+ panic(err)
+ }
+
+ for {
+ select {
+ case <-m.dir.Events:
+ m.run()
+ case <-m.ticker:
+ m.run()
+ }
+ }
+}
+
+func (m *Monitor) run() {
+ fmt.Println(m.dir.Files)
+}
7 watchgopher/rules.go
@@ -0,0 +1,7 @@
+package watchgopher
+
+import (
+ "os"
+)
+
+type Rule func(string, os.FileInfo)
Please sign in to comment.
Something went wrong with that request. Please try again.