Rate current track

This commit is contained in:
Dan Jones 2023-08-27 20:07:00 -05:00
commit 83dc41cb8e
4 changed files with 83 additions and 9 deletions

58
cmd/current-rate.go Normal file
View file

@ -0,0 +1,58 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"errors"
"strconv"
"codeberg.org/danjones000/mpc-extra/mpd"
"github.com/spf13/cobra"
)
// rateCmd represents the rate command
var rateCmd = &cobra.Command{
Use: "rate [rating]",
Short: "Rates the current playing song: 0-10",
/*
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.`,
*/
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing rating")
}
num, err := strconv.Atoi(args[0])
if err != nil {
return err
}
if num < 0 || num > 10 {
return errors.New("Rating must be between zero and ten")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
curr, _ := mpd.GetCurrent()
curr.AddSticker("rating", args[0])
curr.PrintAll(true)
},
}
func init() {
currentCmd.AddCommand(rateCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// rateCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// rateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -11,13 +11,15 @@ import (
// currentCmd represents the current command // currentCmd represents the current command
var currentCmd = &cobra.Command{ var currentCmd = &cobra.Command{
Use: "current", Use: "current",
Short: "A brief description of your command", Short: "Shows information about the currently playing song",
Long: `A longer description that spans multiple lines and likely contains examples /*
and usage of using your command. For example: 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. Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application.`, to quickly create a Cobra application.`,
*/
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
curr, _ := mpd.GetCurrent() curr, _ := mpd.GetCurrent()
curr.PrintAll(true) curr.PrintAll(true)

View file

@ -12,7 +12,8 @@ import (
// stickerCmd represents the sticker command // stickerCmd represents the sticker command
var stickerCmd = &cobra.Command{ var stickerCmd = &cobra.Command{
Use: "sticker", Use: "sticker [sticker-name]",
Args: cobra.MinimumNArgs(1),
Short: "A brief description of your command", Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example: and usage of using your command. For example:

View file

@ -10,6 +10,7 @@ type Song struct {
Path string Path string
Attrs mpd.Attrs Attrs mpd.Attrs
stickers []mpd.Sticker stickers []mpd.Sticker
// @todo refactor as a map so there's no dupes
} }
func newSong(attrs mpd.Attrs) *Song { func newSong(attrs mpd.Attrs) *Song {
@ -52,8 +53,8 @@ func (s *Song) GetSticker(name string) string {
return sticker.Name == name return sticker.Name == name
}) })
if ret != nil && len(ret) > 0 { if ret != nil && len(ret) > 0 {
first := ret[0] last := ret[len(ret)-1]
return first.Value return last.Value
} }
} }
@ -70,3 +71,15 @@ func (s *Song) GetSticker(name string) string {
return found.Value return found.Value
} }
func (s *Song) AddSticker(name, value string) error {
ret := conn.StickerSet(s.Path, name, value)
if ret == nil {
if s.stickers == nil || len(s.stickers) == 0 {
s.GetStickers()
} else {
s.stickers = append(s.stickers, mpd.Sticker{Name: name, Value: value})
}
}
return ret
}