-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
183 lines (171 loc) · 4.09 KB
/
utils.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
package main
import (
"fmt"
"github.com/esiqveland/notify"
"github.com/godbus/dbus/v5"
"github.com/urfave/cli/v2"
"log"
"path/filepath"
"sort"
"strings"
)
func parseFiles(files string) (watchList []string){
tokens := strings.Split(files, " ")
for _, token := range tokens {
matches, err := filepath.Glob(token)
if err != nil {
log.Fatalln(err)
}
if matches != nil {
watchList = append(watchList, matches...)
} else {
log.Println("Token '" + token + "' did not match to any file")
}
}
if len(watchList) > 1 {
deduplicate(watchList)
}
return
}
func postprocessFlags() {
// TODO: function that will check if flags that should exclude each other are set
}
func absPath(matches []string) (expanded []string) {
for _, path := range matches {
f, err := filepath.Abs(path)
if err != nil {
log.Println(err)
expanded = append(expanded, path)
} else {
expanded = append(expanded, f)
}
}
return
}
func deduplicate(watchList []string) (uniques []string) {
sort.Strings(watchList)
j := 0
for i := 1; i < len(watchList); i++ {
f1, _ := filepath.Abs(watchList[i])
f2, _ := filepath.Abs(watchList[j])
if f1 == f2 {
continue
}
j++
watchList[j] = watchList[i]
}
uniques = watchList[:j+1]
return
}
func convertEvents(eventsString string) (events int) {
elems := strings.Split(eventsString, " ")
for _, el := range elems {
if e, ok := eventsMap[el]; ok {
events |= e
}
}
return
}
func systemNotify(notification string) {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
n := notify.Notification{
AppName: "Watcher",
ReplacesID: uint32(0),
Summary: "Event received",
Body: notification,
Actions: []string{"cancel", "Cancel", "open", "Open"},
Hints: map[string]dbus.Variant{},
ExpireTimeout: int32(5000),
}
createdID, err := notify.SendNotification(conn, n)
if err != nil {
log.Printf("error sending notification: %v", err.Error())
}
log.Printf("created notification with id: %v", createdID)
}
func setupApp() *cli.App {
app := &cli.App{
Name: "watcher",
Usage: "command line program for monitoring file changes",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "watch_list",
Aliases: []string{"wl", "l"},
Usage: "string describing files to watch. Each entry has to be space separated. Globs supported (.., *, **).",
Required: true,
},
&cli.StringFlag{
Name: "events",
Aliases: []string{"e"},
Usage: "string describing which events to listen to. Multiple events have to be space separated. Supported values: WRITE REMOVE RENAME",
Value: "WRITE",
},
},
Commands: []*cli.Command{
{
Name: "notify",
Usage: "notify the user when an event occurs",
Action: func(c *cli.Context) error {
err := run(c, CmdNotify)
return err
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "system",
Aliases: []string{"s"},
Usage: "outputs notifications to dbus",
Value: false,
},
&cli.BoolFlag{
Name: "stdout",
Aliases: []string{"o"},
Usage: "outputs notifications to stdout",
Value: true,
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "outputs notifications to file",
},
},
},
{
Name: "exec",
Usage: "execute a command after watcher receives an event",
Subcommands: []*cli.Command{
{
Name: "shell",
Usage: "command will run in shell",
Action: func(c *cli.Context) error {
err := run(c, CmdShell)
return err
},
},
{
Name: "builtin",
Usage: "runs a builtin command",
Action: func(c *cli.Context) error {
fmt.Println("There are no builtin commands for now.")
//err := run(c, CmdBuiltin)
return nil
},
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "list all builtin commands",
Action: func(c *cli.Context) error {
fmt.Println("There are no builtin commands for now.")
return nil
},
},
},
},
},
},
},
}
return app
}