Check if notify-send is available before using notifications

This commit is contained in:
Tulir Asokan 2021-09-19 20:45:15 -04:00
parent 2436a9cd39
commit 531a58ce4a

View File

@ -23,8 +23,9 @@ import (
"os/exec" "os/exec"
) )
var notifySendPath string
var audioCommand string var audioCommand string
var tryAudioCommands = []string{"paplay", "ogg123"} var tryAudioCommands = []string{"ogg123", "paplay"}
var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga" var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga"
var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga" var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga"
@ -43,6 +44,11 @@ func getSoundPath(env, defaultPath string) string {
func init() { func init() {
var err error var err error
if notifySendPath, err = exec.LookPath("notify-send"); err != nil {
return
}
for _, cmd := range tryAudioCommands { for _, cmd := range tryAudioCommands {
if audioCommand, err = exec.LookPath(cmd); err == nil { if audioCommand, err = exec.LookPath(cmd); err == nil {
break break
@ -53,6 +59,10 @@ func init() {
} }
func Send(title, text string, critical, sound bool) error { func Send(title, text string, critical, sound bool) error {
if len(notifySendPath) == 0 {
return nil
}
args := []string{"-a", "gomuks"} args := []string{"-a", "gomuks"}
if !critical { if !critical {
args = append(args, "-u", "low") args = append(args, "-u", "low")
@ -68,5 +78,5 @@ func Send(title, text string, critical, sound bool) error {
} }
_ = exec.Command(audioCommand, audioFile).Run() _ = exec.Command(audioCommand, audioFile).Run()
} }
return exec.Command("notify-send", args...).Run() return exec.Command(notifySendPath, args...).Run()
} }