diff --git a/internal/playlist/list.go b/internal/playlist/list.go index 1fbf4be..b381c1c 100644 --- a/internal/playlist/list.go +++ b/internal/playlist/list.go @@ -3,27 +3,39 @@ package playlist import ( "fmt" "spotify/internal" + "strings" + "github.com/brianstrauch/spotify" "github.com/spf13/cobra" ) func NewListCommand() *cobra.Command { return &cobra.Command{ - Use: "list", - Short: "List playlists.", + Use: "list", + Short: "List playlists.", RunE: func(cmd *cobra.Command, args []string) error { api, err := internal.Authenticate() if err != nil { return err } - playlists, err := api.GetPlaylists() + output, err := List(api) if err != nil { return err } - for _, pl := range playlists { - fmt.Println(pl.Name) - } + fmt.Print(output) return nil }, } } + +func List(api *spotify.API) (string, error) { + playlists, err := api.GetPlaylists() + if err != nil { + return "", err + } + var builder strings.Builder + for _, pl := range playlists { + builder.WriteString(fmt.Sprintln(pl.Name)) + } + return builder.String(), nil +}