-
Notifications
You must be signed in to change notification settings - Fork 3
/
growl.go
191 lines (171 loc) · 3.49 KB
/
growl.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
package growl
import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
)
type Priority struct {
Cmd string
Range []string
}
type Host struct {
Cmd string
Hostname string
}
type Command struct {
Type string
Pkg string
Msg string
Title string
Subtitle string
Sound string
Sticky string
Icon string
Url string
Priority *Priority
Host *Host
Activate string
}
type Options struct {
Name string
Sound string
Title string
Subtitle string
Url string
Sticky bool
Priority string
Image string
Exec string
}
func quote(s string) string {
return strconv.Quote(s)
}
func Which(appName string) (string, error) {
return exec.LookPath(appName)
}
func Notify(msg string, opts Options) {
var c Command
if opts.Exec != "" {
c = Command{
Type: "Custom",
Pkg: opts.Exec,
}
} else {
c = getCmd()
}
args := []string{}
if opts.Sticky {
args = append(args, c.Sticky)
}
if opts.Priority != "" && c.Priority != nil {
priority := opts.Priority
for _, v := range c.Priority.Range {
if priority == v {
args = append(args, c.Priority.Cmd, priority)
break
}
}
}
switch c.Type {
case "Darwin-Growl":
if opts.Image != "" {
flag, ext := "", filepath.Ext(opts.Image)[1:]
if ext == "icns" {
flag = "iconpath"
}
if flag == "" {
if matched, _ := regexp.MatchString("^[A-Z]", opts.Image); matched {
flag = "appIcon"
}
}
if flag == "" {
if matched, _ := regexp.MatchString("^png|gif|jpe?g$", ext); matched {
flag = "image"
}
}
if flag == "" {
if ext != "" && opts.Image == ext {
flag = "icon"
}
}
if flag == "" {
flag = "icon"
}
args = append(args, "--"+flag, quote(opts.Image))
}
args = append(args, c.Msg, quote(msg))
if opts.Title != "" {
args = append(args, quote(opts.Title))
}
if opts.Name != "" {
args = append(args, "--name", opts.Name)
}
break
case "Darwin-NotificationCenter":
args = append(args, c.Msg, quote(msg))
if opts.Title != "" {
args = append(args, c.Title, quote(opts.Title))
}
if opts.Subtitle != "" {
args = append(args, c.Subtitle, quote(opts.Subtitle))
}
if opts.Url != "" {
args = append(args, c.Url, quote(opts.Url))
}
if opts.Sound != "" {
args = append(args, c.Sound, opts.Sound)
}
break
case "Linux":
if opts.Image != "" {
args = append(args, c.Icon, opts.Image)
}
// libnotify defaults to sticky, set a hint for transient notifications
if opts.Sticky != true {
args = append(args, "--hint=int:transient:1")
}
if opts.Title != "" {
args = append(args, quote(opts.Title), c.Msg, quote(msg))
} else {
args = append(args, quote(msg))
}
break
case "Linux-Growl":
args = append(args, c.Msg, quote(msg))
if opts.Title != "" {
args = append(args, quote(opts.Title))
}
args = append(args, c.Host.Cmd, c.Host.Hostname)
break
case "Windows":
args = append(args, quote(msg))
if opts.Image != "" {
args = append(args, c.Icon+quote(opts.Image))
}
if opts.Title != "" {
args = append(args, c.Title+quote(opts.Title))
}
if opts.Url != "" {
args = append(args, c.Subtitle+quote(opts.Url))
}
break
case "Custom":
if opts.Title != "" {
msg = opts.Title + ": " + msg
}
args = append(args, quote(msg))
break
}
if c.Activate != "" {
args = append(args, c.Activate)
}
cmd := exec.Command(c.Pkg, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}