-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
57 lines (50 loc) · 1.12 KB
/
monitor.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
package main
import (
"path/filepath"
fsnotify "gopkg.in/fsnotify.v1"
)
// set up directory monitoring
func startMonitor() {
monitor, err := fsnotify.NewWatcher()
if err != nil {
logger(err, true)
}
defer monitor.Close()
// which path
if *folder != "" {
dir = *folder
} else {
dir, err = getCurrentDir()
if err != nil {
logger(err, true)
}
}
done := make(chan bool)
go func() {
for {
select {
case event := <-monitor.Events:
if filepath.Ext(event.Name) == ".gcode" || filepath.Ext(event.Name) == ".stl" {
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
msg := "file modified " + filepath.Base(event.Name)
logger(msg, false)
// check if not in progress and send
if checkAndTrack(event.Name) {
go sendSCP(event.Name)
} else {
logger("file transfer already in progress", false)
}
}
}
case err := <-monitor.Errors:
logger(err, false)
}
}
}()
// add the path to our monitor
if err = monitor.Add(dir); err != nil {
logger(err, true)
}
logger("monitoring changes", false)
<-done
}