Skip to content

Commit

Permalink
error out on invalid arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
abs3nt committed Apr 7, 2024
1 parent a715207 commit 8a21c7c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
97 changes: 97 additions & 0 deletions src/components/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/urfave/cli/v3"
"github.com/zmb3/spotify/v2"
Expand All @@ -22,6 +23,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
EnableShellCompletion: true,
Version: Version,
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")
},
Commands: []*cli.Command{
Expand All @@ -30,6 +34,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"pl", "start", "s"},
Usage: "Plays spotify",
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()
},
Category: "Playback",
Expand All @@ -40,6 +47,12 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Plays a spotify url",
ArgsUsage: "url",
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())
},
Category: "Playback",
Expand All @@ -49,6 +62,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"pa"},
Usage: "Pauses spotify",
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()
},
Category: "Playback",
Expand All @@ -58,6 +74,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"t"},
Usage: "Toggles play/pause",
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()
},
Category: "Playback",
Expand All @@ -67,6 +86,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"yy"},
Usage: "Prints the current song's spotify link",
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()
},
Category: "Sharing",
Expand All @@ -76,6 +98,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"lc"},
Usage: "Prints the current album or playlist",
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()
},
Category: "Sharing",
Expand All @@ -85,6 +110,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"yl"},
Usage: "Prints the current song's youtube link",
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()
},
Category: "Sharing",
Expand All @@ -95,6 +123,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Skips to the next song",
ArgsUsage: "amount",
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 {
amt, err := strconv.Atoi(cmd.Args().First())
if err != nil {
Expand All @@ -111,6 +142,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"b", "prev", "back"},
Usage: "Skips to the previous song",
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()
},
Category: "Playback",
Expand All @@ -120,6 +154,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"l"},
Usage: "Likes the current song",
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()
},
Category: "Library Management",
Expand All @@ -129,6 +166,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"u"},
Usage: "Unlikes the current song",
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()
},
Category: "Library Management",
Expand All @@ -146,6 +186,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
},
},
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"))
},
Category: "Info",
Expand All @@ -161,6 +204,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Increase the volume",
ArgsUsage: "percent",
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())
if err != nil {
return err
Expand All @@ -174,6 +220,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Decrease the volume",
ArgsUsage: "percent",
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())
if err != nil {
return err
Expand All @@ -186,6 +235,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"m"},
Usage: "Mute",
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()
},
},
Expand All @@ -194,6 +246,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"um"},
Usage: "Unmute",
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()
},
},
Expand All @@ -202,6 +257,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"tm"},
Usage: "Toggle mute",
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()
},
},
Expand All @@ -218,6 +276,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
}
},
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())
},
Category: "Info",
Expand All @@ -227,6 +288,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Starts a radio from the current song",
Aliases: []string{"r"},
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()
},
Category: "Radio",
Expand All @@ -236,6 +300,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Clears the radio queue",
Aliases: []string{"cr"},
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()
},
Category: "Radio",
Expand All @@ -245,6 +312,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Refills the radio queue with similar songs",
Aliases: []string{"rr"},
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()
},
Category: "Radio",
Expand All @@ -253,6 +323,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "status",
Usage: "Prints the current status",
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()
},
Category: "Info",
Expand All @@ -262,6 +335,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Usage: "Lists available devices",
Aliases: []string{"d"},
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()
},
Category: "Info",
Expand All @@ -274,6 +350,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
if cmd.NArg() == 0 {
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()))
},
Category: "Playback",
Expand All @@ -282,6 +361,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "repeat",
Usage: "Toggle repeat mode",
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()
},
Category: "Playback",
Expand All @@ -290,6 +372,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "shuffle",
Usage: "Toggle shuffle mode",
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()
},
Category: "Playback",
Expand All @@ -298,6 +383,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "tui",
Usage: "Starts the TUI",
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")
},
},
Expand All @@ -307,6 +395,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"sk"},
Category: "Playback",
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())
if err != nil {
return err
Expand All @@ -319,6 +410,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"f"},
Usage: "Seek forward",
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)
},
},
Expand All @@ -327,6 +421,9 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Aliases: []string{"b"},
Usage: "Seek backward",
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)
},
},
Expand Down
7 changes: 6 additions & 1 deletion src/components/commands/play.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"fmt"
"net/url"
"strings"

Expand Down Expand Up @@ -96,7 +97,11 @@ func (c *Commander) PlayURL(urlString string) error {
if err != nil {
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))
if err != nil {
if isNoActiveError(err) {
Expand Down

0 comments on commit 8a21c7c

Please sign in to comment.