Skip to content

Commit

Permalink
Add mute command
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed May 18, 2023
1 parent f4049ac commit ac13186
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/kefw2/cmd/mute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// muteCmd toggles the mute state of the speakers
var muteCmd = &cobra.Command{
Use: "mute",
Short: "Get or adjust the mute state of the speakers",
Long: `Get or adjust the mute state of the speakers`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
mute, _ := currentSpeaker.IsMuted()
fmt.Printf("Speakers are muted: %t\n", mute)
return
}
mute, err := parseMuteArg(args[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if mute {
err = currentSpeaker.Mute()
} else {
err = currentSpeaker.Unmute()
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
ValidArgsFunction: MuteCompletion,
}

func init() {
rootCmd.AddCommand(muteCmd)
}

func MuteCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"on", "off", "true", "false", "0", "1", "muted", "unmute", "unmuted"}, cobra.ShellCompDirectiveNoFileComp
}

func parseMuteArg(mute string) (bool, error) {
switch mute {
case "on", "true", "1", "muted":
return true, nil
case "off", "false", "0", "unmute", "unmuted":
return false, nil
default:
return false, fmt.Errorf("mute must be one of: on, off, true, false, 0, 1, muted, unmute, unmuted")
}
}

0 comments on commit ac13186

Please sign in to comment.