/
fswatcher.go
97 lines (89 loc) · 1.92 KB
/
fswatcher.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"log"
"os"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/myjimnelson/c3sat/queryciv3"
)
type watchListType struct {
watches []string
}
// Does not check to see if already added
func (w *watchListType) addWatch(path string) error {
err := savWatcher.Add(path)
if err != nil {
return err
}
w.watches = append(w.watches, path)
return nil
}
// Only deletes one
func (w *watchListType) removeWatch(path string) error {
err := savWatcher.Remove(path)
if err != nil {
return err
}
for i := 0; i < len(w.watches); i++ {
if w.watches[i] == path {
// remove element from array by swapping last element and replacing with one-shorter array
w.watches[i] = w.watches[len(w.watches)-1]
w.watches[len(w.watches)-1] = ""
w.watches = w.watches[:len(w.watches)-1]
break
}
}
return nil
}
func loadDefaultBiq(s string) error {
fi, err := os.Stat(s)
if err != nil {
return err
}
if fi.Mode().IsRegular() {
err := queryciv3.ChangeDefaultBicPath(s)
if err != nil {
return err
}
}
return nil
}
func loadNewSav(s string) error {
if len(s) > 4 && strings.ToLower(s[len(s)-4:]) == ".sav" {
fi, err := os.Stat(s)
if err != nil {
return err
}
if fi.Mode().IsRegular() {
err := queryciv3.ChangeSavePath(s)
if err != nil {
return err
}
longPoll.Publish("refresh", s)
}
}
return nil
}
func watchSavs() {
var fn string
for {
select {
case event, ok := <-savWatcher.Events:
if !ok {
return
}
fn = event.Name
if event.Op&fsnotify.Write == fsnotify.Write {
debounceTimer.Reset(debounceInterval)
}
case <-debounceTimer.C:
// This will get called once debounceInterval after program start, and I'm going to live with that
loadNewSav(fn)
case err, ok := <-savWatcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}