spogo/internal/cli/device.go
2026-01-02 19:00:04 +01:00

72 lines
1.7 KiB
Go

package cli
import (
"context"
"fmt"
"strings"
"github.com/steipete/spogo/internal/app"
)
type DeviceCmd struct {
List DeviceListCmd `kong:"cmd,help='List devices.'"`
Set DeviceSetCmd `kong:"cmd,help='Set active device.'"`
}
type DeviceListCmd struct{}
type DeviceSetCmd struct {
Device string `arg:"" required:"" help:"Device name or id."`
}
func (cmd *DeviceListCmd) Run(ctx *app.Context) error {
client, err := ctx.Spotify()
if err != nil {
return err
}
devices, err := client.Devices(context.Background())
if err != nil {
return err
}
plain := make([]string, 0, len(devices))
human := make([]string, 0, len(devices))
for _, device := range devices {
plain = append(plain, fmt.Sprintf("%s\t%s\t%t", device.ID, device.Name, device.Active))
label := device.Name
if device.Active {
label = ctx.Output.Theme.Accent(label)
}
human = append(human, fmt.Sprintf("%s (%s) %s", label, device.Type, strings.TrimSpace(activeMarker(device.Active))))
}
return ctx.Output.Emit(devices, plain, human)
}
func (cmd *DeviceSetCmd) Run(ctx *app.Context) error {
client, err := ctx.Spotify()
if err != nil {
return err
}
devices, err := client.Devices(context.Background())
if err != nil {
return err
}
id := cmd.Device
for _, device := range devices {
if strings.EqualFold(device.ID, cmd.Device) || strings.EqualFold(device.Name, cmd.Device) {
id = device.ID
break
}
}
if err := client.Transfer(context.Background(), id); err != nil {
return err
}
return ctx.Output.Emit(map[string]any{"status": "ok", "device": id}, []string{"ok"}, []string{fmt.Sprintf("Switched to %s", id)})
}
func activeMarker(active bool) string {
if active {
return "(active)"
}
return ""
}