forked from nwg-piotr/nwg-drawer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
64 lines (50 loc) · 1.28 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
package main
import (
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/fsnotify/fsnotify"
)
// Thanks to Steve Domino https://medium.com/@skdomino/watch-this-file-watching-in-go-5b5a247cf71f
var watcher *fsnotify.Watcher
func watchFiles() {
// creates a new file watcher
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
if err := watcher.Add(pinnedFile); err != nil {
log.Errorf("ERROR: %s", err)
}
for _, fp := range appDirs {
if err := filepath.Walk(fp, watchDir); err != nil {
log.Errorf("ERROR: %s", err)
}
}
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if strings.HasSuffix(event.Name, ".desktop") &&
(event.Op.String() == "CREATE" ||
event.Op.String() == "REMOVE" ||
event.Op.String() == "RENAME") {
desktopTrigger = true
} else if event.Name == pinnedFile {
// TODO: This can be used to propagate information about the changed file to the
// GUI to avoid recreating everything
pinnedItemsChanged <- struct{}{}
}
case err := <-watcher.Errors:
log.Errorf("ERROR: %s", err)
}
}
}()
<-done
}
func watchDir(path string, fi os.FileInfo, err error) error {
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}