diff --git a/internal/p/p.go b/internal/p/p.go index 6777b4c..e0db637 100644 --- a/internal/p/p.go +++ b/internal/p/p.go @@ -6,6 +6,7 @@ import ( "spotify/internal" "spotify/internal/pause" "spotify/internal/play" + "spotify/internal/playlist" "strings" ) @@ -49,6 +50,8 @@ func NewCommand() *cobra.Command { cmd.Flags().String("playlist", "", "playlist name from 'spotify playlist list'") cmd.Flags().String("album", "", "album name") + _ = cmd.RegisterFlagCompletionFunc("playlist", playlist.AutocompletePlaylist) + return cmd } diff --git a/internal/pause/pause.go b/internal/pause/pause.go index 99a616b..bbd4a80 100644 --- a/internal/pause/pause.go +++ b/internal/pause/pause.go @@ -10,7 +10,7 @@ import ( ) func NewCommand() *cobra.Command { - cmd := &cobra.Command{ + return &cobra.Command{ Use: "pause", Short: "pause music", RunE: func(cmd *cobra.Command, _ []string) error { @@ -28,8 +28,6 @@ func NewCommand() *cobra.Command { return nil }, } - - return cmd } func Pause(api internal.APIInterface) (string, error) { diff --git a/internal/play/play.go b/internal/play/play.go index a2cedcb..9dbc551 100644 --- a/internal/play/play.go +++ b/internal/play/play.go @@ -3,6 +3,7 @@ package play import ( "errors" "spotify/internal" + "spotify/internal/playlist" "spotify/internal/status" "strings" @@ -49,6 +50,8 @@ func NewCommand() *cobra.Command { cmd.Flags().String("playlist", "", "playlist name from 'spotify playlist list'") cmd.Flags().String("album", "", "album name that you wish to play") + _ = cmd.RegisterFlagCompletionFunc("playlist", playlist.AutocompletePlaylist) + return cmd } diff --git a/internal/playlist/show.go b/internal/playlist/show.go index 99fb0c1..9dc36a7 100644 --- a/internal/playlist/show.go +++ b/internal/playlist/show.go @@ -13,9 +13,10 @@ import ( func NewShowCommand() *cobra.Command { return &cobra.Command{ - Use: "show [playlist]", - Short: "show artist and songs", - Args: cobra.MinimumNArgs(1), + Use: "show [playlist]", + Short: "show artist and songs", + Args: cobra.MinimumNArgs(1), + ValidArgsFunction: AutocompletePlaylist, RunE: func(cmd *cobra.Command, args []string) error { api, err := internal.Authenticate() if err != nil { @@ -70,3 +71,21 @@ func formatPlaylist(playlist *spotify.Playlist) (string, error) { return output.String(), nil } + +func AutocompletePlaylist(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + api, err := internal.Authenticate() + if err != nil { + return []string{}, cobra.ShellCompDirectiveError + } + + playlists, err := api.GetPlaylists() + if err != nil { + return []string{}, cobra.ShellCompDirectiveError + } + + var completions []string + for _, playlist := range playlists { + completions = append(completions, playlist.Name) + } + return completions, cobra.ShellCompDirectiveDefault +}