-
Notifications
You must be signed in to change notification settings - Fork 8
/
watcher.go
48 lines (39 loc) · 827 Bytes
/
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
package main
import (
"log"
"time"
)
type Watcher interface {
Watch(path string, channel chan string)
}
type GitWatcher struct {
git Git
running bool
checkInterval time.Duration
delayBeforeFiringEvent time.Duration
delayAfterFiringEvent time.Duration
}
func (f *GitWatcher) Stop() {
f.running = false
}
func (f *GitWatcher) Check(path string, channel chan string) {
dirty, err := f.git.IsDirty(path)
if err != nil {
log.Printf("Failed to get state. Error: %v", err)
}
if dirty {
log.Printf("Changes have been detected.")
time.Sleep(f.delayBeforeFiringEvent)
channel <- path
time.Sleep(f.delayAfterFiringEvent)
}
}
func (f *GitWatcher) Watch(path string, channel chan string) {
f.running = true
go func() {
for f.running {
time.Sleep(f.checkInterval)
f.Check(path, channel)
}
}()
}