forked from oelmekki/pgrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
68 lines (57 loc) · 1.2 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"github.com/fsnotify/fsnotify"
"path/filepath"
"os"
"fmt"
)
type Watcher struct {
Done chan bool
Error chan error
notify *fsnotify.Watcher
watches []string
}
/*
* Start the watch loop
*/
func ( watcher *Watcher ) Start() {
var err error
watcher.notify, err = fsnotify.NewWatcher()
if err != nil { watcher.Error <- err ; return }
defer watcher.notify.Close()
watcher.build()
watcher.loop()
return
}
/*
* Find all directories and watch them
*/
func ( watcher *Watcher ) build() ( err error ) {
if err = watcher.notify.Add( Cfg.SqlDirPath ) ; err != nil { return err }
err = filepath.Walk( Cfg.SqlDirPath, func( path string, info os.FileInfo, err error ) error {
if IsDir( path ) {
if err = watcher.notify.Add( path ) ; err != nil { return err }
watcher.watches = append( watcher.watches, path )
}
return nil
})
return
}
/*
* Watcher event loop
*/
func ( watcher *Watcher ) loop() {
for {
select {
case event := <-watcher.notify.Events:
if ! IsHiddenFile( event.Name ) {
fmt.Printf( "\nFS changed. Building.\n" )
watcher.Done <- true
return
}
case err := <-watcher.notify.Errors:
watcher.Error <- err
return
}
}
}