error out on invalid arguments

This commit is contained in:
abs3nt 2024-04-07 10:43:58 -07:00
parent a7152075fa
commit 8a21c7cb52
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
2 changed files with 103 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
@ -22,6 +23,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
EnableShellCompletion: true, EnableShellCompletion: true,
Version: Version, Version: Version,
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unknown command: %s", strings.Join(cmd.Args().Slice(), " "))
}
return tui.StartTea(c, "main") return tui.StartTea(c, "main")
}, },
Commands: []*cli.Command{ Commands: []*cli.Command{
@ -30,6 +34,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"pl", "start", "s"}, Aliases: []string{"pl", "start", "s"},
Usage: "Plays spotify", Usage: "Plays spotify",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Play() return c.Play()
}, },
Category: "Playback", Category: "Playback",
@ -40,6 +47,12 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Plays a spotify url", Usage: "Plays a spotify url",
ArgsUsage: "url", ArgsUsage: "url",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if !cmd.Args().Present() {
return fmt.Errorf("no url provided")
}
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.PlayURL(cmd.Args().First()) return c.PlayURL(cmd.Args().First())
}, },
Category: "Playback", Category: "Playback",
@ -49,6 +62,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"pa"}, Aliases: []string{"pa"},
Usage: "Pauses spotify", Usage: "Pauses spotify",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Pause() return c.Pause()
}, },
Category: "Playback", Category: "Playback",
@ -58,6 +74,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"t"}, Aliases: []string{"t"},
Usage: "Toggles play/pause", Usage: "Toggles play/pause",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.TogglePlay() return c.TogglePlay()
}, },
Category: "Playback", Category: "Playback",
@ -67,6 +86,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"yy"}, Aliases: []string{"yy"},
Usage: "Prints the current song's spotify link", Usage: "Prints the current song's spotify link",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.PrintLink() return c.PrintLink()
}, },
Category: "Sharing", Category: "Sharing",
@ -76,6 +98,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"lc"}, Aliases: []string{"lc"},
Usage: "Prints the current album or playlist", Usage: "Prints the current album or playlist",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.PrintLinkContext() return c.PrintLinkContext()
}, },
Category: "Sharing", Category: "Sharing",
@ -85,6 +110,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"yl"}, Aliases: []string{"yl"},
Usage: "Prints the current song's youtube link", Usage: "Prints the current song's youtube link",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.PrintYoutubeLink() return c.PrintYoutubeLink()
}, },
Category: "Sharing", Category: "Sharing",
@ -95,6 +123,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Skips to the next song", Usage: "Skips to the next song",
ArgsUsage: "amount", ArgsUsage: "amount",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
if cmd.NArg() > 0 { if cmd.NArg() > 0 {
amt, err := strconv.Atoi(cmd.Args().First()) amt, err := strconv.Atoi(cmd.Args().First())
if err != nil { if err != nil {
@ -111,6 +142,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"b", "prev", "back"}, Aliases: []string{"b", "prev", "back"},
Usage: "Skips to the previous song", Usage: "Skips to the previous song",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Previous() return c.Previous()
}, },
Category: "Playback", Category: "Playback",
@ -120,6 +154,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"l"}, Aliases: []string{"l"},
Usage: "Likes the current song", Usage: "Likes the current song",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Like() return c.Like()
}, },
Category: "Library Management", Category: "Library Management",
@ -129,6 +166,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"u"}, Aliases: []string{"u"},
Usage: "Unlikes the current song", Usage: "Unlikes the current song",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.UnLike() return c.UnLike()
}, },
Category: "Library Management", Category: "Library Management",
@ -146,6 +186,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
}, },
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.NowPlaying(cmd.Bool("force")) return c.NowPlaying(cmd.Bool("force"))
}, },
Category: "Info", Category: "Info",
@ -161,6 +204,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Increase the volume", Usage: "Increase the volume",
ArgsUsage: "percent", ArgsUsage: "percent",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
amt, err := strconv.Atoi(cmd.Args().First()) amt, err := strconv.Atoi(cmd.Args().First())
if err != nil { if err != nil {
return err return err
@ -174,6 +220,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Decrease the volume", Usage: "Decrease the volume",
ArgsUsage: "percent", ArgsUsage: "percent",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
amt, err := strconv.Atoi(cmd.Args().First()) amt, err := strconv.Atoi(cmd.Args().First())
if err != nil { if err != nil {
return err return err
@ -186,6 +235,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"m"}, Aliases: []string{"m"},
Usage: "Mute", Usage: "Mute",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Mute() return c.Mute()
}, },
}, },
@ -194,6 +246,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"um"}, Aliases: []string{"um"},
Usage: "Unmute", Usage: "Unmute",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.UnMute() return c.UnMute()
}, },
}, },
@ -202,6 +257,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"tm"}, Aliases: []string{"tm"},
Usage: "Toggle mute", Usage: "Toggle mute",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.ToggleMute() return c.ToggleMute()
}, },
}, },
@ -218,6 +276,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
} }
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.DownloadCover(cmd.Args().First()) return c.DownloadCover(cmd.Args().First())
}, },
Category: "Info", Category: "Info",
@ -227,6 +288,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Starts a radio from the current song", Usage: "Starts a radio from the current song",
Aliases: []string{"r"}, Aliases: []string{"r"},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Radio() return c.Radio()
}, },
Category: "Radio", Category: "Radio",
@ -236,6 +300,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Clears the radio queue", Usage: "Clears the radio queue",
Aliases: []string{"cr"}, Aliases: []string{"cr"},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.ClearRadio() return c.ClearRadio()
}, },
Category: "Radio", Category: "Radio",
@ -245,6 +312,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Refills the radio queue with similar songs", Usage: "Refills the radio queue with similar songs",
Aliases: []string{"rr"}, Aliases: []string{"rr"},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.RefillRadio() return c.RefillRadio()
}, },
Category: "Radio", Category: "Radio",
@ -253,6 +323,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "status", Name: "status",
Usage: "Prints the current status", Usage: "Prints the current status",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Status() return c.Status()
}, },
Category: "Info", Category: "Info",
@ -262,6 +335,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Lists available devices", Usage: "Lists available devices",
Aliases: []string{"d"}, Aliases: []string{"d"},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.ListDevices() return c.ListDevices()
}, },
Category: "Info", Category: "Info",
@ -274,6 +350,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
if cmd.NArg() == 0 { if cmd.NArg() == 0 {
return fmt.Errorf("no device id provided") return fmt.Errorf("no device id provided")
} }
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.SetDevice(spotify.ID(cmd.Args().First())) return c.SetDevice(spotify.ID(cmd.Args().First()))
}, },
Category: "Playback", Category: "Playback",
@ -282,6 +361,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "repeat", Name: "repeat",
Usage: "Toggle repeat mode", Usage: "Toggle repeat mode",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Repeat() return c.Repeat()
}, },
Category: "Playback", Category: "Playback",
@ -290,6 +372,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "shuffle", Name: "shuffle",
Usage: "Toggle shuffle mode", Usage: "Toggle shuffle mode",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Shuffle() return c.Shuffle()
}, },
Category: "Playback", Category: "Playback",
@ -298,6 +383,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "tui", Name: "tui",
Usage: "Starts the TUI", Usage: "Starts the TUI",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return tui.StartTea(c, "main") return tui.StartTea(c, "main")
}, },
}, },
@ -307,6 +395,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"sk"}, Aliases: []string{"sk"},
Category: "Playback", Category: "Playback",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() > 1 {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
pos, err := strconv.Atoi(cmd.Args().First()) pos, err := strconv.Atoi(cmd.Args().First())
if err != nil { if err != nil {
return err return err
@ -319,6 +410,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"f"}, Aliases: []string{"f"},
Usage: "Seek forward", Usage: "Seek forward",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Seek(true) return c.Seek(true)
}, },
}, },
@ -327,6 +421,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"b"}, Aliases: []string{"b"},
Usage: "Seek backward", Usage: "Seek backward",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
}
return c.Seek(false) return c.Seek(false)
}, },
}, },

View File

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"fmt"
"net/url" "net/url"
"strings" "strings"
@ -96,7 +97,11 @@ func (c *Commander) PlayURL(urlString string) error {
if err != nil { if err != nil {
return err return err
} }
trackID := strings.Split(url.Path, "/")[2] splittUrl := strings.Split(url.Path, "/")
if len(splittUrl) < 3 {
return fmt.Errorf("invalid url")
}
trackID := splittUrl[2]
err = c.Client().QueueSong(c.Context, spotify.ID(trackID)) err = c.Client().QueueSong(c.Context, spotify.ID(trackID))
if err != nil { if err != nil {
if isNoActiveError(err) { if isNoActiveError(err) {