Skip to content

Commit

Permalink
Merge pull request #60 from Somsubhra/add-completion-command
Browse files Browse the repository at this point in the history
#51 Enable completion command to generate shell completions
  • Loading branch information
mathieu-benoit authored Oct 21, 2024
2 parents a979469 + 19de64f commit 50f6bd1
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ Flags:
--patch-manifests stringArray An optional set of <kind|*>/<name|*>/path=key operations for the output manifests
```

### Shell Completions

```
$ score-k8s completion --help
Generate the autocompletion script for score-k8s for the specified shell.
See each sub-command's help for details on how to use the generated script.
Usage:
score-k8s completion [command]
Available Commands:
bash Generate the autocompletion script for bash
fish Generate the autocompletion script for fish
powershell Generate the autocompletion script for powershell
zsh Generate the autocompletion script for zsh
Flags:
-h, --help help for completion
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
Use "score-k8s completion [command] --help" for more information about a command.
```

## Installation

Either, install through Homebrew for macOS and supported Linux distributions:
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func init() {
rootCmd.Version = version.BuildVersionString()
rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "%s" .Version}}
`)
rootCmd.CompletionOptions = cobra.CompletionOptions{HiddenDefaultCmd: true}
rootCmd.PersistentFlags().Bool("quiet", false, "Mute any logging output")
rootCmd.PersistentFlags().CountP("verbose", "v", "Increase log verbosity and detail by specifying this flag one or more times")
}
Expand Down
193 changes: 193 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,199 @@ func TestRootVersion(t *testing.T) {
assert.Equal(t, "", stderr)
}

func TestRootCompletion(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion"})
assert.NoError(t, err)
assert.Equal(t, `Generate the autocompletion script for score-k8s for the specified shell.
See each sub-command's help for details on how to use the generated script.
Usage:
score-k8s completion [command]
Available Commands:
bash Generate the autocompletion script for bash
fish Generate the autocompletion script for fish
powershell Generate the autocompletion script for powershell
zsh Generate the autocompletion script for zsh
Flags:
-h, --help help for completion
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
Use "score-k8s completion [command] --help" for more information about a command.
`, stdout)
assert.Equal(t, "", stderr)

stdout2, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"help", "completion"})
assert.NoError(t, err)
assert.Equal(t, stdout, stdout2)
assert.Equal(t, "", stderr)
}

func TestRootCompletionBashHelp(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "bash", "--help"})
assert.NoError(t, err)
assert.Equal(t, `Generate the autocompletion script for the bash shell.
This script depends on the 'bash-completion' package.
If it is not installed already, you can install it via your OS's package manager.
To load completions in your current shell session:
source <(score-k8s completion bash)
To load completions for every new session, execute once:
#### Linux:
score-k8s completion bash > /etc/bash_completion.d/score-k8s
#### macOS:
score-k8s completion bash > $(brew --prefix)/etc/bash_completion.d/score-k8s
You will need to start a new shell for this setup to take effect.
Usage:
score-k8s completion bash
Flags:
-h, --help help for bash
--no-descriptions disable completion descriptions
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
`, stdout)
assert.Equal(t, "", stderr)
}

func TestRootCompletionBash(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "bash"})
assert.NoError(t, err)
assert.Contains(t, stdout, "# bash completion V2 for score-k8s")
assert.Equal(t, "", stderr)
}

func TestRootCompletionFishHelp(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "fish", "--help"})
assert.NoError(t, err)
assert.Equal(t, `Generate the autocompletion script for the fish shell.
To load completions in your current shell session:
score-k8s completion fish | source
To load completions for every new session, execute once:
score-k8s completion fish > ~/.config/fish/completions/score-k8s.fish
You will need to start a new shell for this setup to take effect.
Usage:
score-k8s completion fish [flags]
Flags:
-h, --help help for fish
--no-descriptions disable completion descriptions
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
`, stdout)
assert.Equal(t, "", stderr)
}

func TestRootCompletionFish(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "fish"})
assert.NoError(t, err)
assert.Contains(t, stdout, "# fish completion for score-k8s")
assert.Equal(t, "", stderr)
}

func TestRootCompletionZshHelp(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "zsh", "--help"})
assert.NoError(t, err)
assert.Equal(t, `Generate the autocompletion script for the zsh shell.
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 in your current shell session:
source <(score-k8s completion zsh)
To load completions for every new session, execute once:
#### Linux:
score-k8s completion zsh > "${fpath[1]}/_score-k8s"
#### macOS:
score-k8s completion zsh > $(brew --prefix)/share/zsh/site-functions/_score-k8s
You will need to start a new shell for this setup to take effect.
Usage:
score-k8s completion zsh [flags]
Flags:
-h, --help help for zsh
--no-descriptions disable completion descriptions
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
`, stdout)
assert.Equal(t, "", stderr)
}

func TestRootCompletionZsh(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "zsh"})
assert.NoError(t, err)
assert.Contains(t, stdout, "# zsh completion for score-k8s")
assert.Equal(t, "", stderr)
}

func TestRootCompletionPowershellHelp(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "powershell", "--help"})
assert.NoError(t, err)
assert.Equal(t, `Generate the autocompletion script for powershell.
To load completions in your current shell session:
score-k8s completion powershell | Out-String | Invoke-Expression
To load completions for every new session, add the output of the above command
to your powershell profile.
Usage:
score-k8s completion powershell [flags]
Flags:
-h, --help help for powershell
--no-descriptions disable completion descriptions
Global Flags:
--quiet Mute any logging output
-v, --verbose count Increase log verbosity and detail by specifying this flag one or more times
`, stdout)
assert.Equal(t, "", stderr)
}

func TestRootCompletionPowershell(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"completion", "powershell"})
assert.NoError(t, err)
assert.Contains(t, stdout, "# powershell completion for score-k8s")
assert.Equal(t, "", stderr)
}

func TestRootUnknown(t *testing.T) {
stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"unknown"})
assert.EqualError(t, err, "unknown command \"unknown\" for \"score-k8s\"")
Expand Down

0 comments on commit 50f6bd1

Please sign in to comment.