/
watcher.go
74 lines (60 loc) · 1.29 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
69
70
71
72
73
74
package server
import (
"errors"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/MichaelTJones/walk"
)
type watcher struct {
cfg *Config
lastRun time.Time
mu sync.Mutex
}
func newWatcher(cfg *Config) *watcher {
return &watcher{
cfg: cfg,
lastRun: time.Now(),
}
}
func (w *watcher) scan() bool {
w.cfg.Debug("start scan")
start := time.Now()
modified := walk.Walk(".", func(path string, info os.FileInfo, err error) error {
if info.IsDir() && w.shouldSkipDir(path) {
return walk.SkipDir
}
if info.ModTime().After(w.getLastRun()) {
w.cfg.Debugf("found modified file: %v", path)
return errors.New(path)
}
return nil
})
w.cfg.Printf("scan done in %v", time.Since(start))
return modified != nil
}
func (w *watcher) getLastRun() time.Time {
w.mu.Lock()
defer w.mu.Unlock()
return w.lastRun
}
func (w *watcher) setLastRun(t time.Time) {
w.mu.Lock()
defer w.mu.Unlock()
w.lastRun = t
}
// Checks to see if this directory should be watched. Don't want to watch
// hidden directories (like .git) or ignored directories.
func (w *watcher) shouldSkipDir(path string) bool {
if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {
return true
}
for _, dir := range w.cfg.IgnoreDirs {
if dir == path {
return true
}
}
return false
}