diff --git a/README.md b/README.md index 4af3179..1b9f49f 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,32 @@ Flags: --patch-manifests stringArray An optional set of //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: diff --git a/main.go b/main.go index ee4225e..d1b909a 100644 --- a/main.go +++ b/main.go @@ -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") } diff --git a/main_test.go b/main_test.go index 0eca63b..4d829f9 100644 --- a/main_test.go +++ b/main_test.go @@ -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\"")