Skip to content

Commit

Permalink
feat: provide shell completion. Closes #619 (#1478)
Browse files Browse the repository at this point in the history
* feat: provide shell completion. Closes #619

Signed-off-by: Kostis Kapelonis <[email protected]>

* docs: autogenerated CLI navigation page

Signed-off-by: Kostis Kapelonis <[email protected]>
  • Loading branch information
kostis-codefresh authored Sep 13, 2021
1 parent c7933eb commit ea1b8ea
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
68 changes: 68 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
96 changes: 96 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/completion/completion_test.go
Original file line number Diff line number Diff line change
@@ -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")

}

0 comments on commit ea1b8ea

Please sign in to comment.