Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Use Cobra's bash completion V2 #4001

Merged
merged 1 commit into from
Nov 25, 2022

Conversation

marckhouzam
Copy link
Contributor

What this PR does / why we need it

This commit makes the tanzu CLI use Cobra's bash completion V2.

Besides fixing #3963, this new version comes with extra features compared to the original bash completion logic, such as:

  • Supports completion descriptions (like the other shells)
  • Small bash completion script (325 lines versus 1342 lines for tanzu)
  • Streamlined user experience thanks to a completion behavior aligned with the other shells (for example, it does not suggest the = form for flag completion)

It is also the version that is actively maintained by Cobra.

While at it, the commit also enables completion descriptions for bash which is available with Cobra's bash completion v2.
Tanzu CLI offers shell completions descriptions for zsh, fish and powershell, we were just missing bash.

Which issue(s) this PR fixes

Fix #3963

Describe testing done for PR

Testing for bug fix:
Before PR:

bash
bash-5.1$ source <(tanzu completion bash)
bash-5.1$ tanzu te[tab][tab]
telemetry  test
tanzu plugin delete test
Deleting Plugin 'test'. Are you sure? [y/N]: y
✔  successfully deleted plugin 'test'

# Notice that `test` is still being suggested
bash-5.1$ tanzu te[tab][tab]
telemetry  test
bash-5.1$ tanzu test
✖  unknown command "test" for "tanzu"

After PR:

bash
bash-5.1$ source <(./tanzu completion bash)
bash-5.1$ tanzu plugin install test
ℹ  Installing plugin 'test:v0.28.0-dev'
✔  successfully installed 'test' plugin
bash-5.1$ tanzu te[tab][tab]
telemetry  (configure cluster-wide settings for vmware tanzu telemetry)
test       (Test the CLI)
bash-5.1$ tanzu plugin delete test
Deleting Plugin 'test'. Are you sure? [y/N]: y
✔  successfully deleted plugin 'test'

# Notice `test` is no longer suggested
bash-5.1$ tanzu t[tab][tab]
bash-5.1$ tanzu telemetry

Testing for new features:

Before PR:

bash
bash-5.1$ source <(tanzu completion bash)
bash-5.1$ tanzu c[tab][tab]
cluster     codegen     completion  config
bash-5.1$ tanzu plugin install --[tab][tab]
--local     --local=    --version   --version=

With PR:

bash
bash-5.1$ source <(./tanzu completion bash)

# Notice descriptions
bash-5.1$ ./tanzu c[tab][tab]
cluster     (Kubernetes cluster operations)  completion  (Output shell completion code)
codegen     (Tanzu code generation tool)     config      (Configuration for the CLI)

# Notice that the = form is no longer suggested for flags
bash-5.1$ ./tanzu plugin install --[tab][tab]
--local    (path to local discovery/distribution source)  --version  (version of the plugin)

Release note

Shell completions for the bash shell now include descriptions.

Additional information

Helm has been using this version of bash completion since August 2nd 2021 (helm/helm#9990)
Kubectl has been using this version of bash completion since January 12, 2022 (kubernetes/kubernetes#107439)

Fix vmware-tanzu#3963

Cobra's bash completion V2 is the version now maintained by Cobra.
See https://github.com/spf13/cobra/blob/main/shell_completions.md#bash-completion-v2

This new version comes with extra features compared to the original bash
completion logic, such as:
- Supports completion descriptions (like the other shells)
- Small bash completion script (325 lines versus 1342 lines for tanzu)
- Streamlined user experience thanks to a completion behavior aligned
  with the other shells (for example, it does not suggest the = form for
  flag completion)

This commit also enables completion descriptions for bash which is
available with Cobra's bash completion v2.

Signed-off-by: Marc Khouzam <kmarc@vmware.com>
@codecov
Copy link

codecov bot commented Nov 24, 2022

Codecov Report

Merging #4001 (1804af2) into main (6788e79) will decrease coverage by 0.66%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #4001      +/-   ##
==========================================
- Coverage   47.63%   46.96%   -0.67%     
==========================================
  Files         420      448      +28     
  Lines       42070    44107    +2037     
==========================================
+ Hits        20038    20716     +678     
- Misses      20115    21436    +1321     
- Partials     1917     1955      +38     
Impacted Files Coverage Δ
cli/core/pkg/command/completion.go 85.00% <100.00%> (ø)
tkg/managementcomponents/helper.go 93.02% <0.00%> (-1.94%) ⬇️
packageclients/pkg/packageclient/package_update.go 83.57% <0.00%> (-1.43%) ⬇️
cli/runtime/config/nodeutils/merge_nodes.go 52.83% <0.00%> (-1.02%) ⬇️
addons/pkg/util/cluster_util.go 3.57% <0.00%> (-0.72%) ⬇️
tkg/client/upgrade_region.go 23.34% <0.00%> (-0.28%) ⬇️
tkg/client/init.go 0.00% <0.00%> (ø)
addons/pkg/util/bom_util.go 0.00% <0.00%> (ø)
cmd/cli/plugin/cluster/get.go 6.27% <0.00%> (ø)
.../cli/plugin/cluster/set_machinehealthcheck_node.go 23.33% <0.00%> (ø)
... and 30 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@randomvariable randomvariable added the ok-to-merge PRs should be labelled with this before merging label Nov 25, 2022
@randomvariable randomvariable merged commit 164c82d into vmware-tanzu:main Nov 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/core-cli cla-not-required ok-to-merge PRs should be labelled with this before merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bash shell completion becomes stale when installing/uninstalling plugins
3 participants