/
main.go
174 lines (160 loc) · 3.13 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"github.com/SaidinWoT/gulf/glob"
set "github.com/deckarep/golang-set"
"gopkg.in/fsnotify.v1"
"log"
"os"
"os/exec"
"time"
)
type Entry struct {
Name string
Files []string
Run []string
}
var config []Entry
var configMap map[string]Entry
func readConfig() {
config = make([]Entry, 0)
configMap = make(map[string]Entry)
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
err := decoder.Decode(&config)
if err != nil {
fmt.Println("error:", err)
}
for id, entry := range config {
configMap[entry.Name] = config[id]
}
toRun = set.NewSet()
}
var toRun set.Set
func runEntry(name string) {
entry, ok := configMap[name]
if !ok {
fmt.Println("Couldn't find entry ", name)
return
}
fmt.Println("=====")
fmt.Println(name)
for _, cmd := range entry.Run {
fmt.Println("\033[31m", cmd, "\033[0m")
proc := exec.Command("bash", "-c", cmd)
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
err := proc.Run()
if err != nil {
fmt.Println(err)
}
}
fmt.Println("=====")
}
func run() {
for entryNameI := range toRun.Iter() {
entryName, ok := entryNameI.(string)
if !ok {
fmt.Println("Error converting entryname:", entryNameI)
continue
}
go runEntry(entryName)
}
}
func filesUpdated(set set.Set) {
fmt.Println("Updated:")
toRun.Clear()
for file := range set.Iter() {
fmt.Println(file)
filestr, ok := file.(string)
if !ok {
fmt.Println("Error converting to string:\n", file)
continue
}
for _, entry := range config {
for _, pattern := range entry.Files {
if toRun.Contains(entry.Name) {
break
}
match, err := glob.Match(pattern, filestr)
if err != nil {
fmt.Println(err)
continue
}
if match {
toRun.Add(entry.Name)
}
}
}
}
run()
}
func unused() {
fileEntry := make(map[string][]Entry)
entries := config
for _, entry := range entries {
fmt.Println(entry.Name)
for _, file := range entry.Files {
fmt.Println(" ", file)
registeredEntries, ok := fileEntry[file]
if !ok {
registeredEntries = make([]Entry, 1)
fileEntry[file] = entries
}
fileEntry[file] = append(registeredEntries, entry)
}
}
file := "blah"
var matches []string
matches, err := glob.Glob(file)
if err != nil {
fmt.Print("Err", err)
return
}
for matchid, match := range matches {
fmt.Println(" ", matchid, match)
}
}
func main() {
readConfig()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
run := false
delay := 1000 * time.Millisecond
timer := time.NewTimer(delay)
updated := set.NewSet()
for {
select {
case <-timer.C:
if run {
run = false
filesUpdated(updated)
updated.Clear()
}
case event := <-watcher.Events:
timer.Reset(delay)
//if event.Op&fsnotify.Write == fsnotify.Write {
run = true
updated.Add(event.Name)
//}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
wd, err := os.Getwd()
if err == nil {
fmt.Println("Monitoring", wd)
err = watcher.Add(wd)
}
if err != nil {
log.Fatal(err)
}
<-done
}