forked from kermieisinthehouse/gosx-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosx-notifier.go
137 lines (115 loc) · 3.12 KB
/
gosx-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
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
package gosxnotifier
import (
"errors"
"net/url"
"os/exec"
"path/filepath"
"strings"
)
type Sound string
const (
Default Sound = "'default'"
Basso Sound = "Basso"
Blow Sound = "Blow"
Bottle Sound = "Bottle"
Frog Sound = "Frog"
Funk Sound = "Funk"
Glass Sound = "Glass"
Hero Sound = "Hero"
Morse Sound = "Morse"
Ping Sound = "Ping"
Pop Sound = "Pop"
Purr Sound = "Purr"
Sosumi Sound = "Sosumi"
Submarine Sound = "Submarine"
Tink Sound = "Tink"
)
type Notification struct {
Message string //required
Title string //optional
Subtitle string //optional
Sound Sound //optional
Link string //optional
Sender string //optional
Group string //optional
AppIcon string //optional
ContentImage string //optional
}
func NewNotification(message string) *Notification {
n := &Notification{Message: message}
return n
}
func (n *Notification) Push() error {
if supportedOS() {
commandTuples := make([]string, 0)
//check required commands
if n.Message == "" {
return errors.New("Please specifiy a proper message argument.")
} else {
commandTuples = append(commandTuples, []string{"-message", n.Message}...)
}
//add title if found
if n.Title != "" {
commandTuples = append(commandTuples, []string{"-title", n.Title}...)
}
//add subtitle if found
if n.Subtitle != "" {
commandTuples = append(commandTuples, []string{"-subtitle", n.Subtitle}...)
}
//add sound if specified
if n.Sound != "" {
commandTuples = append(commandTuples, []string{"-sound", string(n.Sound)}...)
}
//add group if specified
if n.Group != "" {
commandTuples = append(commandTuples, []string{"-group", n.Group}...)
}
//add appIcon if specified
if n.AppIcon != "" {
img, err := normalizeImagePath(n.AppIcon)
if err != nil {
return err
}
commandTuples = append(commandTuples, []string{"-appIcon", img}...)
}
//add contentImage if specified
if n.ContentImage != "" {
img, err := normalizeImagePath(n.ContentImage)
if err != nil {
return err
}
commandTuples = append(commandTuples, []string{"-contentImage", img}...)
}
//add url if specified
url, err := url.Parse(n.Link)
if err != nil {
n.Link = ""
}
if url != nil && n.Link != "" {
commandTuples = append(commandTuples, []string{"-open", n.Link}...)
}
//add bundle id if specified
if strings.HasPrefix(strings.ToLower(n.Link), "com.") {
commandTuples = append(commandTuples, []string{"-activate", n.Link}...)
}
//add sender if specified
if strings.HasPrefix(strings.ToLower(n.Sender), "com.") {
commandTuples = append(commandTuples, []string{"-sender", n.Sender}...)
}
if len(commandTuples) == 0 {
return errors.New("Please provide a Message and Type at a minimum.")
}
_, err = exec.Command(FinalPath, commandTuples...).Output()
if err != nil {
return err
}
}
return nil
}
func normalizeImagePath(image string) (string, error) {
if imagePath, err := filepath.Abs(image); err != nil {
return "", errors.New("Could not resolve image path of image: " + image)
} else {
return imagePath, nil
}
}