gospt/runner/runner.go

80 lines
1.6 KiB
Go
Raw Normal View History

2023-01-07 07:40:39 +00:00
package runner
import (
"fmt"
2023-01-08 00:03:43 +00:00
"gospt/ctx"
"gospt/tui"
2023-01-07 07:40:39 +00:00
"github.com/zmb3/spotify/v2"
)
2023-01-08 00:03:43 +00:00
func Run(ctx *ctx.Context, client *spotify.Client, args []string) error {
2023-01-07 07:40:39 +00:00
if len(args) == 0 {
2023-01-08 00:03:43 +00:00
user, err := client.CurrentUser(ctx)
2023-01-07 07:40:39 +00:00
if err != nil {
return fmt.Errorf("Failed to get current user")
}
2023-01-07 16:19:19 +00:00
fmt.Println("The following commands are currently supported:\nplay pause next shuffle\nhave fun", user.DisplayName)
2023-01-07 07:40:39 +00:00
return nil
}
switch args[0] {
case "play":
2023-01-08 00:03:43 +00:00
return Play(ctx, client)
2023-01-07 07:40:39 +00:00
case "pause":
2023-01-08 00:03:43 +00:00
return Pause(ctx, client)
2023-01-07 07:40:39 +00:00
case "next":
2023-01-08 00:03:43 +00:00
return Skip(ctx, client)
2023-01-07 07:40:39 +00:00
case "shuffle":
2023-01-08 00:03:43 +00:00
return Shuffle(ctx, client)
case "tracks":
return GetTracks(ctx, client, args)
2023-01-07 16:19:19 +00:00
default:
return fmt.Errorf("Unsupported Command")
}
}
2023-01-08 00:03:43 +00:00
func Play(ctx *ctx.Context, client *spotify.Client) error {
2023-01-07 16:19:19 +00:00
err := client.Play(ctx)
if err != nil {
return err
}
fmt.Println("Playing!")
return nil
}
2023-01-08 00:03:43 +00:00
func Pause(ctx *ctx.Context, client *spotify.Client) error {
2023-01-07 16:19:19 +00:00
err := client.Pause(ctx)
if err != nil {
return err
}
fmt.Println("Pausing!")
return nil
}
2023-01-08 00:03:43 +00:00
func Skip(ctx *ctx.Context, client *spotify.Client) error {
2023-01-07 16:19:19 +00:00
err := client.Next(ctx)
if err != nil {
return err
}
fmt.Println("Skipping!")
return nil
}
2023-01-08 00:03:43 +00:00
func Shuffle(ctx *ctx.Context, client *spotify.Client) error {
2023-01-07 16:19:19 +00:00
state, err := client.PlayerState(ctx)
if err != nil {
return fmt.Errorf("Failed to get current playstate")
}
err = client.Shuffle(ctx, !state.ShuffleState)
if err != nil {
return err
2023-01-07 07:40:39 +00:00
}
2023-01-07 16:19:19 +00:00
fmt.Println("Shuffle set to", !state.ShuffleState)
2023-01-07 07:40:39 +00:00
return nil
}
2023-01-08 00:03:43 +00:00
func GetTracks(ctx *ctx.Context, client *spotify.Client, args []string) error {
return tui.DisplayList(ctx, client)
}