diff --git a/cmd/current-rate.go b/cmd/current-rate.go new file mode 100644 index 0000000..5e2cbfa --- /dev/null +++ b/cmd/current-rate.go @@ -0,0 +1,58 @@ +/* +Copyright © 2023 NAME HERE +*/ +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") +} diff --git a/cmd/current.go b/cmd/current.go index 4a1ad07..2b218ca 100644 --- a/cmd/current.go +++ b/cmd/current.go @@ -11,13 +11,15 @@ import ( // currentCmd represents the current command var currentCmd = &cobra.Command{ Use: "current", - 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: + 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: -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.`, + 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.`, + */ Run: func(cmd *cobra.Command, args []string) { curr, _ := mpd.GetCurrent() curr.PrintAll(true) diff --git a/cmd/find-sticker.go b/cmd/find-sticker.go index d81a74b..f143a0e 100644 --- a/cmd/find-sticker.go +++ b/cmd/find-sticker.go @@ -12,7 +12,8 @@ import ( // stickerCmd represents the sticker command var stickerCmd = &cobra.Command{ - Use: "sticker", + Use: "sticker [sticker-name]", + Args: cobra.MinimumNArgs(1), 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: diff --git a/mpd/song.go b/mpd/song.go index 351aa75..0e4cf5c 100644 --- a/mpd/song.go +++ b/mpd/song.go @@ -10,6 +10,7 @@ type Song struct { Path string Attrs mpd.Attrs stickers []mpd.Sticker + // @todo refactor as a map so there's no dupes } func newSong(attrs mpd.Attrs) *Song { @@ -52,8 +53,8 @@ func (s *Song) GetSticker(name string) string { return sticker.Name == name }) if ret != nil && len(ret) > 0 { - first := ret[0] - return first.Value + last := ret[len(ret)-1] + return last.Value } } @@ -70,3 +71,15 @@ func (s *Song) GetSticker(name string) string { 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 +}