active device check

This commit is contained in:
abs3nt 2023-01-11 13:57:47 -08:00
parent 66518295a9
commit 0d1de1349b
3 changed files with 21 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"gospt/internal/commands"
"gospt/internal/tui" "gospt/internal/tui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -19,6 +20,9 @@ var tracksCmd = &cobra.Command{
Long: `Uses TUI to open a list of saved tracks`, Long: `Uses TUI to open a list of saved tracks`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
if commands.DeviceActive(ctx, client) {
return tui.StartTea(ctx, client, "tracks")
}
if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil { if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil {
return tui.StartTea(ctx, client, "devices") return tui.StartTea(ctx, client, "devices")
} }

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"gospt/internal/commands"
"gospt/internal/tui" "gospt/internal/tui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -19,6 +20,9 @@ var tuiCmd = &cobra.Command{
Long: `Default command. this is what will run if no other commands are present. Shows the main menu.`, Long: `Default command. this is what will run if no other commands are present. Shows the main menu.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
if commands.DeviceActive(ctx, client) {
return tui.StartTea(ctx, client, "main")
}
if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil { if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil {
return tui.StartTea(ctx, client, "devices") return tui.StartTea(ctx, client, "devices")
} }

View File

@ -35,6 +35,19 @@ func Play(ctx *gctx.Context, client *spotify.Client) error {
return nil return nil
} }
func DeviceActive(ctx *gctx.Context, client *spotify.Client) bool {
current, err := client.PlayerDevices(ctx)
if err != nil {
return false
}
for _, dev := range current {
if dev.Active {
return true
}
}
return false
}
func PlayUrl(ctx *gctx.Context, client *spotify.Client, args []string) error { func PlayUrl(ctx *gctx.Context, client *spotify.Client, args []string) error {
url, err := url.Parse(args[0]) url, err := url.Parse(args[0])
if err != nil { if err != nil {