-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
62 lines (49 loc) · 1.61 KB
/
notifier.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
package goqute
import (
"fmt"
"log"
"os/exec"
)
// Notifier can be used for string based notifications
type Notifier interface {
Notify(message string, urgency UrgencyLevel)
}
// UrgencyLevel represents notify urgancy level
type UrgencyLevel string
const (
// UrgencyLow for messages with low priority (debugging,…)
UrgencyLow UrgencyLevel = "low"
// UrgencyNormal for messages with normal priority (normal messages for user)
UrgencyNormal UrgencyLevel = "normal"
// UrgencyCritical for messages with high priority (errors, warnings,…)
UrgencyCritical UrgencyLevel = "critical"
)
// Desktop Notifier
type desktopNotifier struct{}
func (dn desktopNotifier) Notify(message string, urgency UrgencyLevel) {
cmd := exec.Command("notify-send", "-u", string(urgency), message)
if err := cmd.Run(); err != nil {
log.Printf("error while starting notify-send: %s\n", err)
}
}
// NewDesktopNotifier returns a notifier for desktop notifications
func NewDesktopNotifier() Notifier {
return desktopNotifier{}
}
// Command Line Notifier for tests
type clNotifier struct{}
func (cln clNotifier) Notify(message string, urgency UrgencyLevel) {
log.Println(string(urgency) + ": " + message)
}
// NewCommandLineNotifier returns a notifier for desktop notifications
func NewCommandLineNotifier() Notifier {
return clNotifier{}
}
func createNotifyMessage(message string, urgency UrgencyLevel) []byte {
var messageCommands = map[UrgencyLevel]string{
UrgencyLow: "message-info",
UrgencyNormal: "message-info",
UrgencyCritical: "message-error",
}
return []byte(fmt.Sprintf("%s \"%s\" \n", messageCommands[urgency], message))
}