-
Notifications
You must be signed in to change notification settings - Fork 1
/
winnotify.go
86 lines (81 loc) · 2.05 KB
/
winnotify.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
//go:build windows
package main
import (
_ "embed"
"os"
"time"
"gopkg.in/toast.v1"
)
var (
//go:embed static/success.ico
successIcon []byte
//go:embed static/failed.ico
failedIcon []byte
err error
)
func init() {
// 系统为 windows 时,启用 winnotify 并显示对应的帮助信息
var errOccurred = false
notifyHelpMsg = "\n -n --winnotify\tSend notifacation cards, only available on Windows"
notifyErrCheck := func() {
if err != nil {
err = nil
errOccurred = true
}
}
notify = func(title string, message string, succeed bool) {
genNotifyAndLog := func(succeed bool, notification toast.Notification, iconPath string) toast.Notification {
notification = toast.Notification{
AppID: "QCIP",
Title: title,
Message: "Some error occurred, please check the error log for details",
}
if iconPath != "" {
notification.Icon = iconPath
}
if !succeed {
err = os.WriteFile("qciperrlog.txt", []byte("QCIP error log | "+time.Now().Format("2006-01-02 15:04:05")+"\n"+message), 0644)
if err != nil {
notification.Message = message
return notification
}
errLogPath, _ := os.Getwd()
errLogPath += "\\qciperrlog.txt"
notification.Actions = []toast.Action{
{
Type: "protocol",
Label: "Show error logs",
Arguments: errLogPath,
},
}
} else {
notification.Message = message
}
return notification
}
var iconFile *os.File
iconFile, err = os.CreateTemp("", "qcip-*.ico")
notifyErrCheck()
if succeed {
_, err = iconFile.Write(successIcon)
notifyErrCheck()
} else {
_, err = iconFile.Write(failedIcon)
notifyErrCheck()
}
err = iconFile.Close()
notifyErrCheck()
iconPath := iconFile.Name()
var notification toast.Notification
if !errOccurred {
notification = genNotifyAndLog(succeed, notification, iconPath)
} else {
notification = genNotifyAndLog(succeed, notification, "")
}
err = notification.Push()
notifyErrCheck()
if errOccurred {
errOutput("Error occurred when sending notification cards")
}
}
}