Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Link with Open and Activate #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions gosx-notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package gosxnotifier

import (
"errors"
"net/url"
"os/exec"
"path/filepath"
"strings"
)

type Sound string
Expand Down Expand Up @@ -33,7 +31,8 @@ type Notification struct {
Title string //optional
Subtitle string //optional
Sound Sound //optional
Link string //optional
Open string //optional
Activate string //optional
Sender string //optional
Group string //optional
AppIcon string //optional
Expand Down Expand Up @@ -97,30 +96,24 @@ func (n *Notification) Push() error {
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}...)
if n.Open != "" {
commandTuples = append(commandTuples, []string{"-open", n.Open}...)
}

//add bundle id if specified
if strings.HasPrefix(strings.ToLower(n.Link), "com.") {
commandTuples = append(commandTuples, []string{"-activate", n.Link}...)
if n.Activate != "" {
commandTuples = append(commandTuples, []string{"-activate", n.Activate}...)
}

//add sender if specified
if strings.HasPrefix(strings.ToLower(n.Sender), "com.") {
if n.Sender != "" {
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()
_, err := exec.Command(FinalPath, commandTuples...).Output()
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions gosx-notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,21 @@ func Test_Sound(t *testing.T) {
}

func Test_Link_Url(t *testing.T) {
n := NewNotification("Testing Link Url")
n.Link = "http://www.yahoo.com"
n := NewNotification("Testing Open")
n.Open = "http://www.yahoo.com"
err := n.Push()

if err != nil {
t.Error("Test_Link failed with error: ", err)
t.Error("Test_Open failed with error: ", err)
}
}

func Test_Link_App_Bundle(t *testing.T) {
n := NewNotification("Testing Link Terminal")
n.Link = "com.apple.Safari"
n := NewNotification("Testing Activate")
n.Activate = "com.apple.Safari"
err := n.Push()

if err != nil {
t.Error("Test_Link failed with error: ", err)
t.Error("Test_Activate failed with error: ", err)
}
}