/
gape.go
96 lines (80 loc) · 2.26 KB
/
gape.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
package main
/* Easy to use recursive directory filesystem changes notification tool that can send output to
remote or local syslog servers, based on github.com/rjeczalik/notify
Floris Meester floris@grid6.io
*/
import (
"log"
"encoding/json"
"github.com/rjeczalik/notify"
"flag"
"os"
"fmt"
"log/syslog"
)
type Configuration struct {
Sysloghost string
Syslogproto string
Syslogport string
Stdout bool
Localonly bool
Paths []string
}
func main(){
// Open configuration file
conf := flag.String("config","/etc/gape.conf", "Path to configuration file")
flag.Parse()
file, err := os.Open(*conf)
if err != nil {
log.Fatal("Can't find configuration file, try 'gape -config <path> ", *conf)
}
decoder := json.NewDecoder(file)
configuration := Configuration{}
err = decoder.Decode(&configuration)
if err != nil {
fmt.Println("error opening configuration:", err)
}
// Create the notification channel
c := make(chan notify.EventInfo, 1)
// Create a syslog writer for logging local or remote
if configuration.Localonly{
logger, err := syslog.New(syslog.LOG_NOTICE, "Gape")
if err == nil {
log.SetOutput(logger)
} else {
log.Fatal(err)
}
}else {
logger, err := syslog.Dial(configuration.Syslogproto, configuration.Sysloghost +
":" + configuration.Syslogport, syslog.LOG_NOTICE, "Gape")
if err == nil {
log.SetOutput(logger)
}else{
log.Fatal(err)
}
}
// Create the notification watches
for _,item := range configuration.Paths {
// Check if item exists and is a directory otherwise bailout
fd, err := os.Stat(item)
if err != nil {
log.Fatal(err)
}
if !fd.IsDir(){
log.Fatal("Not a directory: ", item)
}
// I might move the notification options to the config file
if err := notify.Watch(item + "...", c, notify.Remove, notify.Create, notify.Write, notify.Rename ); err != nil {
log.Fatal(err)
}
}
defer notify.Stop(c)
// Loop forever and receive events from the channel.
for {
ei := <-c
log.Print(ei)
if configuration.Stdout {
fmt.Println(ei)
}
}
}