-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
67 lines (57 loc) · 1.3 KB
/
watch.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
package commands
import (
"os"
log "github.com/Sirupsen/logrus"
"github.com/fsnotify/fsnotify"
"github.com/maliceio/malice/config"
)
func cmdWatch(folderName string, logs bool) error {
log.WithFields(log.Fields{
"env": config.Conf.Environment.Run,
}).Info("Malice watching folder: ", folderName)
info, err := os.Stat(folderName)
// Check that folder exists
if os.IsNotExist(err) {
log.Error("error: folder does not exist.")
return nil
}
// Check that path is a folder and not a file
if info.IsDir() {
NewWatcher(folderName)
} else {
log.Error("error: path is not a folder")
}
return nil
}
// NewWatcher creates a new watcher for the user supplied folder
func NewWatcher(folder string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Create == fsnotify.Create {
log.Println("modified file:", event.Name)
// Scan new sample in watch folder
err = cmdScan(event.Name, false)
if err != nil {
log.Error(err)
}
}
case err := <-watcher.Errors:
log.Error("error:", err)
}
}
}()
err = watcher.Add(folder)
if err != nil {
log.Fatal(err)
}
<-done
}