-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
224 lines (202 loc) · 5.36 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"fmt"
"io/fs"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/fsnotify/fsnotify"
"github.com/jakewan/go-procrotator/childproc"
"github.com/jakewan/go-procrotator/logger"
"github.com/jakewan/go-procrotator/runtimeconfig"
"github.com/jakewan/go-procrotator/watchdirs"
)
func main() {
l := logger.NewLogger("go-procrotator", os.Stderr)
if cfg, err := runtimeconfig.Build(os.Args[1:]); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
} else {
l.SetErrorLevel(cfg.LogLevel())
if cfg.WorkingDirectory() != "" {
if err := os.Chdir(cfg.WorkingDirectory()); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
}
}
if wd, err := os.Getwd(); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
} else if len(cfg.IncludeFileRegexes()) < 1 {
l.Errorf(logger.WARNING, "Warning, no include file regexes detected.")
os.Exit(1)
} else {
startProcessing(wd, l, cfg)
}
}
}
func startProcessing(wd string, l logger.Logger, cfg runtimeconfig.Config) {
if watchDirs, err := getDirectoriesToWatch(wd); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
} else {
startFilesystemWatcher(l, cfg, watchDirs)
}
}
func startFilesystemWatcher(l logger.Logger, cfg runtimeconfig.Config, watchDirs []string) {
if w, err := fsnotify.NewWatcher(); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
} else {
for _, d := range watchDirs {
if err := w.Add(d); err != nil {
l.Errorf(logger.ERROR, err.Error())
os.Exit(1)
}
}
startBackgroundProcesses(l, cfg, w)
}
}
func startBackgroundProcesses(
l logger.Logger,
cfg runtimeconfig.Config,
watcher *fsnotify.Watcher,
) {
defer watcher.Close()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
trapSignalsDone := make(chan bool, 1)
watchDirEvents := make(chan watchdirs.WatcherEvent)
watchDirErrors := make(chan error)
fileChangedChan := make(chan watchdirs.FileChangedEvent)
quitWatchDirs := make(chan bool)
watchDirsDone := make(chan bool)
childProcManagerDone := make(chan bool)
go childproc.StartChildProcess(
newChildProcDeps(l),
cfg,
fileChangedChan,
childProcManagerDone,
)
eventProcessingDone := make(chan bool)
go watchdirs.StartEventProcessing(
newWatchDirsDeps(l),
cfg.IncludeFileRegexes(),
cfg.ExcludeFileRegexes(),
fileChangedChan,
watchDirEvents,
watchDirErrors,
eventProcessingDone,
)
l.Errorf(logger.DEBUG, "Waiting for file change events")
go startWatchDirs(
l,
watcher,
watchDirEvents,
quitWatchDirs,
watchDirsDone,
)
l.Errorf(logger.DEBUG, "Watching directories")
// Start trapping signals and wait for the user to terminate the program.
go startTrapSignals(sigChan, trapSignalsDone)
<-trapSignalsDone
l.Errorf(logger.DEBUG, "Quit signal received")
// Signal the director watching process to quit and wait for completion.
quitWatchDirs <- true
<-watchDirsDone
l.Errorf(logger.DEBUG, "Directory watching processes completed")
// Signal the event processor to quit by closing its receiving channels
// and wait for it to signal completion.
close(watchDirEvents)
close(watchDirErrors)
<-eventProcessingDone
l.Errorf(logger.DEBUG, "Event processor completed")
// Signal the child process manager to quit by closing the file change
// channel and wait for it to signal completion.
close(fileChangedChan)
<-childProcManagerDone
l.Errorf(logger.DEBUG, "Child process manager completed")
}
func startWatchDirs(l logger.Logger, w *fsnotify.Watcher, changes chan<- watchdirs.WatcherEvent, quit <-chan bool, done chan<- bool) {
defer func() {
done <- true
}()
for {
select {
case <-quit:
return
case ev, ok := <-w.Events:
if ok {
var ops []watchdirs.WatcherEventOp
if ev.Op.Has(fsnotify.Chmod) {
ops = append(ops, watchdirs.CHMOD)
}
if ev.Op.Has(fsnotify.Create) {
ops = append(ops, watchdirs.CREATE)
}
if ev.Op.Has(fsnotify.Remove) {
ops = append(ops, watchdirs.REMOVE)
}
if ev.Op.Has(fsnotify.Rename) {
ops = append(ops, watchdirs.RENAME)
}
if ev.Op.Has(fsnotify.Write) {
ops = append(ops, watchdirs.WRITE)
}
changes <- watchdirs.WatcherEvent{
Path: ev.Name,
Ops: ops,
}
}
case err, ok := <-w.Errors:
if ok {
l.Errorf(logger.ERROR, "Error from fsnotify: %s", err)
}
}
}
}
func startTrapSignals(sigChan <-chan os.Signal, done chan<- bool) {
defer func() {
done <- true
}()
<-sigChan
}
type watchdirsDeps struct {
logger logger.Logger
}
// Logger implements watchdirs.Dependencies.
func (w *watchdirsDeps) Logger() logger.Logger {
return w.logger
}
func newWatchDirsDeps(l logger.Logger) watchdirs.Dependencies {
return &watchdirsDeps{logger: l}
}
func getDirectoriesToWatch(root string) ([]string, error) {
result := []string{}
if err := filepath.WalkDir(
root,
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
result = append(result, path)
}
return nil
},
); err != nil {
return nil, fmt.Errorf("walking root directory: %w", err)
}
return result, nil
}
type childprocmanagerDeps struct {
logger logger.Logger
}
// Logger implements childprocmanager.Dependencies.
func (c *childprocmanagerDeps) Logger() logger.Logger {
return c.logger
}
func newChildProcDeps(l logger.Logger) childproc.Dependencies {
return &childprocmanagerDeps{logger: l}
}