2023-01-07 07:40:39 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zmb3/spotify/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Run(client *spotify.Client, args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
user, err := client.CurrentUser(context.Background())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
switch args[0] {
|
|
|
|
case "play":
|
2023-01-07 16:26:52 +00:00
|
|
|
return Play(ctx, client, args)
|
2023-01-07 07:40:39 +00:00
|
|
|
case "pause":
|
2023-01-07 16:26:52 +00:00
|
|
|
return Pause(ctx, client, args)
|
2023-01-07 07:40:39 +00:00
|
|
|
case "next":
|
2023-01-07 16:26:52 +00:00
|
|
|
return Skip(ctx, client, args)
|
2023-01-07 07:40:39 +00:00
|
|
|
case "shuffle":
|
2023-01-07 16:26:52 +00:00
|
|
|
return Shuffle(ctx, client, args)
|
2023-01-07 16:19:19 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported Command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Play(ctx context.Context, client *spotify.Client, args []string) error {
|
|
|
|
err := client.Play(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Playing!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Pause(ctx context.Context, client *spotify.Client, args []string) error {
|
|
|
|
err := client.Pause(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Pausing!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Skip(ctx context.Context, client *spotify.Client, args []string) error {
|
|
|
|
err := client.Next(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Skipping!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Shuffle(ctx context.Context, client *spotify.Client, args []string) error {
|
|
|
|
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
|
|
|
|
}
|