-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifiers.go
156 lines (136 loc) · 3.61 KB
/
notifiers.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
package notifier
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"log"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"text/template"
uuid "github.com/nu7hatch/gouuid"
gosxnotifier "github.com/deckarep/gosx-notifier"
)
type WindowsToaster struct {
AppID string
Title string
Message string
Duration string
}
const (
LINUX = "linux"
WINDOWS = "windows"
OSX = "darwin"
)
var windowToastPlaceholder *template.Template
func init() {
windowToastPlaceholder = template.New("toast")
windowToastPlaceholder.Parse(`
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$APP_ID = '{{if .AppID}}{{.AppID}}{{else}}Windows App{{end}}'
$template = @"
<toast activationType="protocol" launch="" duration="short">
<visual>
<binding template="ToastGeneric">
{{if .Title}}
<text><![CDATA[{{.Title}}]]></text>
{{end}}
{{if .Message}}
<text><![CDATA[{{.Message}}]]></text>
{{end}}
</binding>
</visual>
<audio silent="true" />
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
`)
}
func setDuration(level string) string {
switch lvl := level; lvl {
case LOW:
return "short"
case NORMAL:
return "long"
case CRITICAL:
return "long"
default:
return "short"
}
}
func parseWindowsToaster(title string, text string, level string) WindowsToaster {
return WindowsToaster{
AppID: "Windows Notifier",
Title: title,
Message: text,
Duration: setDuration(level),
}
}
func (toastData *WindowsToaster) generateWindowsXML() (string, error) {
var toasts bytes.Buffer
error := windowToastPlaceholder.Execute(&toasts, toastData)
if error != nil {
return "", error
}
return toasts.String(), nil
}
func initiateWindowsNotification(toast string) error {
id, _ := uuid.NewV4()
file := filepath.Join(os.TempDir(), id.String()+".ps1")
defer os.Remove(file)
err := ioutil.WriteFile(file, []byte(toast), 0600)
if err != nil {
return err
}
cmd := exec.Command("PowerShell", "-ExecutionPolicy", "Bypass", "-File", file)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if err = cmd.Run(); err != nil {
return err
}
return nil
}
func (toastData *WindowsToaster) Notify() error {
windowsXML, error := toastData.generateWindowsXML()
if error != nil {
return error
}
return initiateWindowsNotification(windowsXML)
}
func notSupported() {
fmt.Println("Support for this OS is not available")
}
func notifyLinux(title string, text string, level string) {
cmd := exec.Command("notify-send", title, text, "-u", level)
cmd.Run()
}
func notifyWindows(title string, text string, level string) {
windowToast := parseWindowsToaster(title, text, level)
windowToast.Notify()
}
func notifyDarwin(title string, text string, level string) {
note := gosxnotifier.NewNotification(text)
note.Title = title
err := note.Push()
if err != nil {
log.Println("Error while pushing notification")
}
}
func Notify(title string, text string, level string) {
switch os := runtime.GOOS; os {
case LINUX:
notifyLinux(title, text, level)
case WINDOWS:
notifyWindows(title, text, level)
case OSX:
notifyDarwin(title, text, level)
default:
notSupported()
}
}