2014-06-18 22:30:49 +00:00
|
|
|
package gotifier
|
|
|
|
|
|
|
|
import (
|
2014-06-19 19:55:15 +00:00
|
|
|
//"log"
|
2014-06-18 22:30:49 +00:00
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
terminalNotifier = "terminal-notifier"
|
|
|
|
notifySend = "notify-send"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Notification struct {
|
|
|
|
Title, Subtitle, Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (notif Notification) Push() bool {
|
2014-06-19 19:55:15 +00:00
|
|
|
//log.Println("Pushing notification")
|
2014-06-18 22:30:49 +00:00
|
|
|
if activeCmd == nil {
|
2014-06-19 19:55:15 +00:00
|
|
|
//log.Println("No active notification command found")
|
2014-06-18 22:30:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := (*activeCmd).Command(notif)
|
|
|
|
|
2014-06-19 19:55:15 +00:00
|
|
|
cmd.Run()
|
2014-06-18 22:30:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotifierCommand struct {
|
|
|
|
Name, Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd NotifierCommand) Command(notif Notification) *exec.Cmd {
|
|
|
|
|
|
|
|
// message is required
|
|
|
|
message := nonNull(notif.Message, notif.Title, notif.Subtitle)
|
|
|
|
var args = []string{""}
|
|
|
|
|
|
|
|
switch cmd.Name {
|
|
|
|
case terminalNotifier:
|
|
|
|
// Append message
|
|
|
|
args = append(args, "-message")
|
|
|
|
args = append(args, "\""+message+"\"")
|
|
|
|
|
|
|
|
if notif.Title != "" {
|
|
|
|
args = append(args, "-title")
|
|
|
|
args = append(args, "\""+notif.Title+"\"")
|
|
|
|
}
|
|
|
|
if notif.Subtitle != "" {
|
|
|
|
args = append(args, "-subtitle")
|
|
|
|
args = append(args, "\""+notif.Subtitle+"\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
case notifySend:
|
|
|
|
if notif.Title != "" {
|
|
|
|
args = append(args, notif.Title)
|
|
|
|
}
|
|
|
|
args = append(args, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
//return log.Sprintf("%s %s", cmd.Path, strings.Join(args, " "))
|
|
|
|
return &exec.Cmd{Path: cmd.Path, Args: args}
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeCmd *NotifierCommand
|
|
|
|
|
|
|
|
var notifiers = []NotifierCommand{
|
|
|
|
NotifierCommand{Name: "terminal-notifier"},
|
|
|
|
NotifierCommand{Name: "notify-send"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func nonNull(vals ...string) string {
|
|
|
|
for _, val := range vals {
|
|
|
|
if val != "" {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-06-19 19:55:15 +00:00
|
|
|
//log.Println("Initing Gotifier")
|
2014-06-18 22:30:49 +00:00
|
|
|
for _, cmd := range notifiers {
|
|
|
|
path, err := exec.LookPath(cmd.Name)
|
|
|
|
if err == nil && path != "" {
|
|
|
|
activeCmd = &cmd
|
|
|
|
activeCmd.Path = path
|
2014-06-19 19:55:15 +00:00
|
|
|
//log.Println("Found notifier command: " + path)
|
2014-06-18 22:30:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|