Next Initial Commit

This commit is contained in:
ViViDboarder 2014-06-18 15:30:49 -07:00
parent 765c73a5eb
commit 17969495fd
2 changed files with 98 additions and 1 deletions

View File

@ -1,4 +1,7 @@
gotifier
========
Desktop notifications for Go
Attempt at a global notification library for [Go](http://golang.org) that supports both OSX and Ubuntu
OSX: Using [terminal-notifier](https://github.com/alloy/terminal-notifier)
Ubuntu: Using [notify-send](http://manpages.ubuntu.com/manpages/saucy/man1/notify-send.1.html)

94
gotifier.go Normal file
View File

@ -0,0 +1,94 @@
package gotifier
import (
"log"
"os/exec"
)
const (
terminalNotifier = "terminal-notifier"
notifySend = "notify-send"
)
type Notification struct {
Title, Subtitle, Message string
}
func (notif Notification) Push() bool {
log.Println("Pushing notification")
if activeCmd == nil {
log.Println("No active notification command found")
return false
}
cmd := (*activeCmd).Command(notif)
cmd.Start()
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() {
log.Println("Initing Gotifier")
for _, cmd := range notifiers {
path, err := exec.LookPath(cmd.Name)
if err == nil && path != "" {
activeCmd = &cmd
activeCmd.Path = path
log.Println("Found notifier command: " + path)
return
}
}
}