Skip to content

Commit

Permalink
Get/set default speaker and volume
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed May 18, 2023
1 parent a1ce6e8 commit 7289b46
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/kefw2/cmd/config_speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var speakerSetDefaultCmd = &cobra.Command{
Long: "Set default speaker",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Error: missing speaker IP address")
fmt.Printf("Default speaker is: %s (%s)\n", defaultSpeaker.Name, defaultSpeaker.IPAddress)
return
}
if err := setDefaultSpeaker(args[0]); err != nil {
Expand Down
22 changes: 19 additions & 3 deletions cmd/kefw2/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import (
)

var (
cfgFile string
speakers []kefw2.KEFSpeaker
defaultSpeaker *kefw2.KEFSpeaker
cfgFile string
currentSpeakerParam string
speakers []kefw2.KEFSpeaker
defaultSpeaker *kefw2.KEFSpeaker
currentSpeaker *kefw2.KEFSpeaker
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -79,6 +81,7 @@ func init() {
// will be global for your application.

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", viper.ConfigFileUsed(), "config file")
rootCmd.PersistentFlags().StringVarP(&currentSpeakerParam, "speaker", "s", "", "default speaker")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down Expand Up @@ -114,4 +117,17 @@ func initConfig() {
break
}
}
if currentSpeakerParam != "" {
for _, s := range speakers {
if s.IPAddress == currentSpeakerParam {
currentSpeaker = &s
break
}
}
} else {
if defaultSpeaker == nil {
log.Fatal("Default speaker not found. Set it with `kefw2 config speaker default` or specify it with the --speaker (-s) flag")
}
currentSpeaker = defaultSpeaker
}
}
36 changes: 25 additions & 11 deletions cmd/kefw2/cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// volumeCmd represents the volume command
var volumeCmd = &cobra.Command{
Use: "volume",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Get or adjust the volume of the speakers",
Long: `Get or adjust the volume of the speakers`,
Run: func(cmd *cobra.Command, args []string) {
s, err := kefw2.NewSpeaker(os.Getenv("KEFW2_IP"))
if len(args) != 1 {
volume, _ := currentSpeaker.GetVolume()
fmt.Printf("Volume is: %d%%\n", volume)
return
}
volume, err := parseVolume(args[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = currentSpeaker.SetVolume(volume)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
volume, _ := s.GetVolume()
fmt.Printf("Volume is: %d%%\n", volume)
},
}

Expand All @@ -42,3 +44,15 @@ func init() {
// is called directly, e.g.:
// volumeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func parseVolume(volume string) (int, error) {
var v int
_, err := fmt.Sscanf(volume, "%d", &v)
if err != nil {
return 0, err
}
if v < 0 || v > 100 {
return 0, fmt.Errorf("volume must be between 0%% and 100%%")
}
return v, nil
}

0 comments on commit 7289b46

Please sign in to comment.