diff --git a/mkdocs.yml b/mkdocs.yml index 3e9fd7a026..62c50c1ba4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -67,6 +67,7 @@ nav: - Commands: - generated/kubectl-argo-rollouts/kubectl-argo-rollouts.md - generated/kubectl-argo-rollouts/kubectl-argo-rollouts_abort.md + - generated/kubectl-argo-rollouts/kubectl-argo-rollouts_completion.md - generated/kubectl-argo-rollouts/kubectl-argo-rollouts_create.md - generated/kubectl-argo-rollouts/kubectl-argo-rollouts_create_analysisrun.md - generated/kubectl-argo-rollouts/kubectl-argo-rollouts_dashboard.md diff --git a/pkg/kubectl-argo-rollouts/cmd/cmd.go b/pkg/kubectl-argo-rollouts/cmd/cmd.go index 1f5cdb6090..53adf27b5d 100644 --- a/pkg/kubectl-argo-rollouts/cmd/cmd.go +++ b/pkg/kubectl-argo-rollouts/cmd/cmd.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/abort" + "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/completion" "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/create" "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/dashboard" "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/get" @@ -72,5 +73,7 @@ func NewCmdArgoRollouts(o *options.ArgoRolloutsOptions) *cobra.Command { cmd.AddCommand(dashboard.NewCmdDashboard(o)) cmd.AddCommand(status.NewCmdStatus(o)) cmd.AddCommand(notificationcmd.NewToolsCommand("notifications", "kubectl argo rollouts notifications", v1alpha1.RolloutGVR, record.NewAPIFactorySettings())) + cmd.AddCommand(completion.NewCmdCompletion(o)) + return cmd } diff --git a/pkg/kubectl-argo-rollouts/cmd/completion/completion.go b/pkg/kubectl-argo-rollouts/cmd/completion/completion.go new file mode 100644 index 0000000000..87546984b3 --- /dev/null +++ b/pkg/kubectl-argo-rollouts/cmd/completion/completion.go @@ -0,0 +1,68 @@ +package completion + +import ( + "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options" + "github.com/spf13/cobra" +) + +func NewCmdCompletion(o *options.ArgoRolloutsOptions) *cobra.Command { + var cmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: `To load completions: + + Bash: + + $ source <(yourprogram completion bash) + + # To load completions for each session, execute once: + # Linux: + $ yourprogram completion bash > /etc/bash_completion.d/yourprogram + # macOS: + $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram + + Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ yourprogram completion zsh > "${fpath[1]}/_yourprogram" + + # You will need to start a new shell for this setup to take effect. + + fish: + + $ yourprogram completion fish | source + + # To load completions for each session, execute once: + $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish + + PowerShell: + + PS> yourprogram completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> yourprogram completion powershell > yourprogram.ps1 + # and source this file from your PowerShell profile. + `, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.ExactValidArgs(1), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(o.Out) + case "zsh": + cmd.Root().GenZshCompletion(o.Out) + case "fish": + cmd.Root().GenFishCompletion(o.Out, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(o.Out) + } + }, + } + return cmd +} diff --git a/pkg/kubectl-argo-rollouts/cmd/completion/completion_test.go b/pkg/kubectl-argo-rollouts/cmd/completion/completion_test.go new file mode 100644 index 0000000000..64bd376c40 --- /dev/null +++ b/pkg/kubectl-argo-rollouts/cmd/completion/completion_test.go @@ -0,0 +1,96 @@ +package completion + +import ( + "bytes" + "testing" + + options "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options/fake" + "github.com/tj/assert" +) + +func TestShellNotFound(t *testing.T) { + tf, o := options.NewFakeArgoRolloutsOptions() + defer tf.Cleanup() + cmd := NewCmdCompletion(o) + cmd.PersistentPreRunE = o.PersistentPreRunE + cmd.SetArgs([]string{"does-not-exist"}) + err := cmd.Execute() + + assert.Error(t, err) + assert.Equal(t, "invalid argument \"does-not-exist\" for \"completion\"", err.Error()) + + stderr := o.ErrOut.(*bytes.Buffer).String() + assert.Empty(t, stderr) + + stdout := o.Out.(*bytes.Buffer).String() + assert.Empty(t, stdout) + +} + +func TestFish(t *testing.T) { + tf, o := options.NewFakeArgoRolloutsOptions() + defer tf.Cleanup() + cmd := NewCmdCompletion(o) + cmd.PersistentPreRunE = o.PersistentPreRunE + cmd.SetArgs([]string{"fish"}) + err := cmd.Execute() + assert.NoError(t, err) + + stderr := o.ErrOut.(*bytes.Buffer).String() + assert.Empty(t, stderr) + + stdout := o.Out.(*bytes.Buffer).String() + assert.Contains(t, stdout, "fish completion") + +} + +func TestBash(t *testing.T) { + tf, o := options.NewFakeArgoRolloutsOptions() + defer tf.Cleanup() + cmd := NewCmdCompletion(o) + cmd.PersistentPreRunE = o.PersistentPreRunE + cmd.SetArgs([]string{"bash"}) + err := cmd.Execute() + assert.NoError(t, err) + + stderr := o.ErrOut.(*bytes.Buffer).String() + assert.Empty(t, stderr) + + stdout := o.Out.(*bytes.Buffer).String() + assert.Contains(t, stdout, "bash completion") + +} + +func TestZsh(t *testing.T) { + tf, o := options.NewFakeArgoRolloutsOptions() + defer tf.Cleanup() + cmd := NewCmdCompletion(o) + cmd.PersistentPreRunE = o.PersistentPreRunE + cmd.SetArgs([]string{"zsh"}) + err := cmd.Execute() + assert.NoError(t, err) + + stderr := o.ErrOut.(*bytes.Buffer).String() + assert.Empty(t, stderr) + + stdout := o.Out.(*bytes.Buffer).String() + assert.Contains(t, stdout, "zsh completion") + +} + +func TestPowershell(t *testing.T) { + tf, o := options.NewFakeArgoRolloutsOptions() + defer tf.Cleanup() + cmd := NewCmdCompletion(o) + cmd.PersistentPreRunE = o.PersistentPreRunE + cmd.SetArgs([]string{"powershell"}) + err := cmd.Execute() + assert.NoError(t, err) + + stderr := o.ErrOut.(*bytes.Buffer).String() + assert.Empty(t, stderr) + + stdout := o.Out.(*bytes.Buffer).String() + assert.Contains(t, stdout, "powershell completion") + +}