forked from deckarep/gosx-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosx-notifier.go
98 lines (81 loc) · 2.09 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
package gosxnotifier
import (
"errors"
"net/url"
"os/exec"
"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"
Tink Sound = "Tink"
)
type Notification struct {
Message string //required
Title string //optional
Subtitle string //optional
Sound Sound //optional
Link string //optional
Sender string //optional
}
func NewNotification(message string) *Notification {
n := &Notification{Message: message}
return n
}
func (n *Notification) Push() error {
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 url if specified
url, err := url.Parse(n.Link)
if err != nil {
n.Link = ""
}
if url != nil {
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
}