From 16933ce0faa0486f0db3c9d071fdc1dca5d07e74 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 27 Aug 2023 13:16:53 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Song=20struct=20to=20hold?= =?UTF-8?q?=20song=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/current.go | 7 +++---- mpd/conn.go | 4 +--- mpd/current.go | 13 +++++++++++++ mpd/song.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 mpd/current.go create mode 100644 mpd/song.go diff --git a/cmd/current.go b/cmd/current.go index aa0af66..3b0dd2b 100644 --- a/cmd/current.go +++ b/cmd/current.go @@ -21,12 +21,11 @@ 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) { - conn, _ := mpd.GetConn() - curr, _ := conn.CurrentSong() - for k, v := range curr { + curr, _ := mpd.GetCurrent() + for k, v := range curr.Attrs { fmt.Println(k, ":", v) } - sticks, _ := mpd.StickersGetFor(curr["file"]) + sticks, _ := curr.GetStickers() for _, v := range sticks { fmt.Println(v.Name, "*:", v.Value) } diff --git a/mpd/conn.go b/mpd/conn.go index 6b46242..a112656 100644 --- a/mpd/conn.go +++ b/mpd/conn.go @@ -1,8 +1,6 @@ package mpd -import ( - "github.com/fhs/gompd/v2/mpd" -) +import "github.com/fhs/gompd/v2/mpd" var conn *mpd.Client var connerror error diff --git a/mpd/current.go b/mpd/current.go new file mode 100644 index 0000000..d6f7237 --- /dev/null +++ b/mpd/current.go @@ -0,0 +1,13 @@ +package mpd + +func GetCurrent() (*Song, error) { + if connerror != nil { + return nil, connerror + } + curr, err := conn.CurrentSong() + if err != nil { + return nil, err + } + + return newSong(curr), nil +} diff --git a/mpd/song.go b/mpd/song.go new file mode 100644 index 0000000..64111f5 --- /dev/null +++ b/mpd/song.go @@ -0,0 +1,33 @@ +package mpd + +import "github.com/fhs/gompd/v2/mpd" + +type Song struct { + Path string + Attrs mpd.Attrs + stickers []mpd.Sticker +} + +func newSong(attrs mpd.Attrs) *Song { + return &Song{ + Path: attrs["file"], + Attrs: attrs, + } +} + +func (s *Song) GetStickers() ([]mpd.Sticker, error) { + if s.stickers != nil { + return s.stickers, nil + } + + if connerror != nil { + return nil, connerror + } + + sticks, err := StickersGetFor(s.Path) + s.stickers = sticks + + return s.stickers, err +} + +// github.com/akrennmair/slice