-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
c7933eb
commit ea1b8ea
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
pkg/kubectl-argo-rollouts/cmd/completion/completion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |