Prevent error if no players found

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-05-16 10:52:16 +01:00
parent d1168c838a
commit 5dde9b7d8f
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
2 changed files with 12 additions and 1 deletions

View file

@ -39,6 +39,13 @@ type playingAudioInfo struct {
func (g *AudioPlayer) getInfo() (*playingAudioInfo, error) {
rawMetadataOutput, err := runCommand(playerctlExecutable, "metadata")
if err != nil {
// If there's no player open, an error will be thrown by this command
// with the below stdout
if string(rawMetadataOutput) == "No players found" {
return &playingAudioInfo{
Status: playerStatusUnknown,
}, nil
}
return nil, err
}

View file

@ -11,7 +11,11 @@ func runCommand(program string, args ...string) ([]byte, error) {
cmd := exec.Command(program, args...)
out, err := cmd.Output()
if err != nil {
err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(append([]string{program}, args...), " "), err)
ne := fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(append([]string{program}, args...), " "), err)
if x, ok := err.(*exec.ExitError); ok {
return bytes.TrimSpace(x.Stderr), ne
}
return nil, ne
}
return bytes.TrimSpace(out), err
}