Skip to content

Commit

Permalink
WIP PoC dynamic shell completion for 'kubectl-argo-rollouts get rollo…
Browse files Browse the repository at this point in the history
…ut [TAB]'

works toward #2015

use ValidArgsFunction from cobra to list candidates for completion: https://github.com/spf13/cobra/blob/main/shell_completions.md#dynamic-completion-of-nouns
- filtering on 'toComplete' doesn't seem necessary, at least on
  bash (most probably bash-completion handles that itself), not sure
  for the others; I could not find cobra documentation about that

use v2 of GenBashCompletion from cobra: https://github.com/spf13/cobra/blob/main/shell_completions.md#bash-completion-v2
- I chose to disable descriptions for completion (as a bash user I'm not
  used to that), but it's enabled for fish it seems, we can enable it
  if desired, I have no strong opinion on it
  • Loading branch information
thomas-riccardi committed Oct 27, 2022
1 parent d106b3a commit d36dcda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/kubectl-argo-rollouts/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewCmdCompletion(o *options.ArgoRolloutsOptions) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(o.Out)
cmd.Root().GenBashCompletionV2(o.Out, false)
case "zsh":
cmd.Root().GenZshCompletion(o.Out)
case "fish":
Expand Down
21 changes: 21 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/get/get_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/juju/ansiterm"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/argoproj/argo-rollouts/pkg/apiclient/rollout"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/signals"
Expand Down Expand Up @@ -75,6 +76,26 @@ func NewCmdGetRollout(o *options.ArgoRolloutsOptions) *cobra.Command {
}
return nil
},
ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
// list rollouts names
ctx := c.Context()
opts := metav1.ListOptions{}
rolloutIf := o.RolloutsClientset().ArgoprojV1alpha1().Rollouts(o.Namespace())
rolloutList, err := rolloutIf.List(ctx, opts)
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}

rolloutNames := make([]string, len(rolloutList.Items))
for i, ro := range rolloutList.Items {
rolloutNames[i] = ro.Name
}

return rolloutNames, cobra.ShellCompDirectiveNoFileComp
},
}
cmd.Flags().BoolVarP(&getOptions.Watch, "watch", "w", false, "Watch live updates to the rollout")
cmd.Flags().BoolVar(&getOptions.NoColor, "no-color", false, "Do not colorize output")
Expand Down

0 comments on commit d36dcda

Please sign in to comment.