| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package notification |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"os/exec" |
| 22 |
) |
| 23 |
|
| 24 |
var terminalNotifierAvailable = false |
| 25 |
|
| 26 |
func init() { |
| 27 |
if err := exec.Command("which", "terminal-notifier").Run(); err != nil { |
| 28 |
terminalNotifierAvailable = false |
| 29 |
} |
| 30 |
terminalNotifierAvailable = true |
| 31 |
} |
| 32 |
|
| 33 |
const sendScript = `on run {notifText, notifTitle} |
| 34 |
display notification notifText with title "gomuks" subtitle notifTitle |
| 35 |
end run` |
| 36 |
|
| 37 |
func Send(title, text string, critical, sound bool) error { |
| 38 |
if terminalNotifierAvailable { |
| 39 |
args := []string{"-title", "gomuks", "-subtitle", title, "-message", text} |
| 40 |
if critical { |
| 41 |
args = append(args, "-timeout", "15") |
| 42 |
} else { |
| 43 |
args = append(args, "-timeout", "4") |
| 44 |
} |
| 45 |
if sound { |
| 46 |
args = append(args, "-sound", "default") |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
return exec.Command("terminal-notifier", args...).Run() |
| 52 |
} |
| 53 |
cmd := exec.Command("osascript", "-", text, title) |
| 54 |
if stdin, err := cmd.StdinPipe(); err != nil { |
| 55 |
return fmt.Errorf("failed to get stdin pipe for osascript: %w", err) |
| 56 |
} else if _, err = stdin.Write([]byte(sendScript)); err != nil { |
| 57 |
return fmt.Errorf("failed to write notification script to osascript: %w", err) |
| 58 |
} else if err = cmd.Run(); err != nil { |
| 59 |
return fmt.Errorf("failed to run notification script: %w", err) |
| 60 |
} else if !cmd.ProcessState.Success() { |
| 61 |
return fmt.Errorf("notification script exited unsuccessfully") |
| 62 |
} else { |
| 63 |
return nil |
| 64 |
} |
| 65 |
} |