-
Notifications
You must be signed in to change notification settings - Fork 27
/
monitor.go
161 lines (134 loc) · 2.48 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
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
package watching
import (
"fmt"
"path/filepath"
"time"
)
const (
Create = 1
Modify = 2
Remove = 3
Rename = 4
)
//
// Errors emitted by the monitor
// concrete implementation
//
type MonitorError struct {
err error
context string
time time.Time
}
func NewMonitorError(err error, ctx string) *MonitorError {
return &MonitorError{
err: err,
context: ctx,
time: time.Now(),
}
}
func (e *MonitorError) Describe() string {
return fmt.Sprintf("An error occured while handling monitoring events: %s, %s", e.err.Error(), e.context)
}
func (e *MonitorError) Error() error {
return e.err
}
func (e *MonitorError) Time() time.Time {
return e.time
}
//
// Events emitted by the monitor
// concrete implementation
//
type MonitorEvent struct {
r string
f string
dir string
ops []int
time time.Time
}
func NewMonitorEvent(root, dir, file string, ops []int) *MonitorEvent {
ev := &MonitorEvent{
r: root,
f: file,
dir: dir,
ops: ops,
time: time.Now(),
}
return ev
}
func (e *MonitorEvent) Name() string {
return "watching.directory"
}
func (e *MonitorEvent) Time() time.Time {
return e.time
}
func (e *MonitorEvent) Operations() []int {
return e.ops
}
func (e *MonitorEvent) Directory() string {
return e.dir
}
func (e *MonitorEvent) Describe() string {
ops := []string{}
for _, op := range e.ops {
switch op {
case 1:
ops = append(ops, "Create")
case 2:
ops = append(ops, "Modify")
case 3:
ops = append(ops, "Remove")
case 4:
ops = append(ops, "Rename")
}
}
return fmt.Sprintf("%s happened on %s", ops, e.rel())
}
//
// Abstract for a monitor
//
type monitor struct {
dir string
events chan DirEvent
errors chan Error
}
func newMonitor(dir string) *monitor {
return &monitor{
dir: dir,
events: make(chan DirEvent),
errors: make(chan Error),
}
}
func (m *Monitor) Events() chan DirEvent {
return m.events
}
func (m *Monitor) Errors() chan Error {
return m.errors
}
func (m *Monitor) Emit(ev DirEvent) {
m.events <- ev
}
func (m *Monitor) Throw(err Error) {
m.errors <- err
}
func (m *Monitor) Directory() string {
return m.dir
}
//
// UNEXPORTED
// Methods for more indepth manipulation and inspection
//
func (e *MonitorEvent) file() string {
return e.f
}
func (e *MonitorEvent) root() string {
return e.r
}
func (e *MonitorEvent) relDir() string {
path, _ := filepath.Rel(e.r, e.Directory())
return path
}
func (e *MonitorEvent) rel() string {
path, _ := filepath.Rel(e.r, e.file())
return path
}