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

Enhance Tanzu CLI completion #701

Open
3 of 9 tasks
tschwaller opened this issue Sep 28, 2021 · 5 comments
Open
3 of 9 tasks

Enhance Tanzu CLI completion #701

tschwaller opened this issue Sep 28, 2021 · 5 comments
Assignees
Labels
area/cli area/core-cli kind/feature Categorizes issue or PR as related to a new feature
Milestone

Comments

@tschwaller
Copy link

tschwaller commented Sep 28, 2021

Tanzu CLI command completion is not fully implemented (i.e. only completes up to level 2 but not further)
As an admin I would expect e.g.

tanzu cluster kubeconfig get <TAB><TAB>

to give me the list of clusters I can choose from. But even if you add one

tanzu cluster kubeconfig get tkg-cluster-2

this will not give you the completion for --admin, i.e.

tanzu cluster kubeconfig get tkg-cluster-2 --admin

The same is true for other -- options (e.g. --help) at the end for this or other commands. They are not completed. Other commands like

tanzu management-cluster create <TAB><TAB>

do not provide any further options like --ui, --bind or --browser, etc. Similar for

tanzu plugin install <TAB><TAB>

which does not give you options like --local, --help, etc.

The whole command set needs to be enhanced to give a uniform Tanzu CLI completion user experience (not just the ones I mentioned).

Affected product area

  • APIs
  • Addons
  • CLI
  • Docs
  • Installation
  • Plugin
  • Security
  • Test and Release
  • User Experience
@tschwaller tschwaller added kind/feature Categorizes issue or PR as related to a new feature needs-triage Indicates an issue or PR needs to be triaged labels Sep 28, 2021
@vuil vuil added area/cli and removed needs-triage Indicates an issue or PR needs to be triaged labels Sep 29, 2021
@stmcginnis
Copy link
Contributor

I'm taking a look at the completion for -- arguments now. But there are really two issues here.

For the tanzu cluster kubeconfig get <TAB><TAB> case of giving a list of clusters to choose from, I think we have a few commands where it would make sense like this to look up possible values to supply as suggestions. That may be a bigger effort. We should review all commands to identify where this is the case, then get issues/PRs filed to support that. I can't remember now if we have a doc for it, but we should get this written down too so as folks implement new commands, this should be one of the things they should consider as part of their implementation.

@stmcginnis
Copy link
Contributor

Good news, bad news for the flag completion part. Good news is I found why it wasn't working. The bad news is I don't think we can get this to behave the way we want in many cases.

This appears to be a limitation of Cobra. The only time flags are included in the completion output is when they are required flags. More details in the docs here: https://github.com/spf13/cobra/blob/master/shell_completions.md#mark-flags-as-required

In many of our cases, the flag is not mandatory. Things like --help or tanzu management-cluster create --ui are things that we don't want to make mandatory, because there are very valid use cases for them not to be used.

In cases where the flag IS mandatory, completion works as expected. You can used the __complete hidden command from cobra to get completion information. We can see these required flags being returned from something like tanzu __complete cluster node-pool delete "" with the --name flag.

So it doesn't look like we can get completion for flags, though we should keep an eye on any cases where there is a required flag that we have not marked as such.

The other part of this - providing completion options for things that are not commands, such as providing the list of cluster names or files, and be done. That will be a matter of going through each command and looking where there are opportunities to enhance this. There is a dynamic completion function that would need to be implemented in each of these commands for noun (https://github.com/spf13/cobra/blob/master/shell_completions.md#dynamic-completion-of-nouns) and flag (https://github.com/spf13/cobra/blob/master/shell_completions.md#specify-dynamic-flag-completion) completion options.

@saji-pivotal saji-pivotal added this to the Icebox milestone Oct 14, 2021
@stmcginnis
Copy link
Contributor

We've updated to a new release of cobra that I think has some enhancements to how this is handled. It may work now, but just leaving this comment here as a breadcrumb for when I have a chance to come back to this and test things.

@stmcginnis
Copy link
Contributor

Tested with a recent build and it does not appear the bump in the cobra package changed the behavior outlined here.

@marckhouzam
Copy link
Contributor

As @stmcginnis pointed out we have two issues:

  1. flag names are not being completed e.g., tanzu unmanaged-cluster --<tab> does not work. This is a bug.
  2. dynamic completion is not implemented (for some commands?) e.g., tanzu unmanaged-cluster stop <TAB> does not list clusters. This is a feature request.

Let's start with the first, and look at the bug of flag names not being completed. If the tanzu command was built/installed through the TCE project it is most probably version v0.11.4. This can be verified by running:

$ tanzu version
version: v0.11.4
buildDate: 2022-06-15
sha: a9b8f3a

Here the version is the version of tanzu-framework used by TCE.
The problem is that tanzu-framework v0.11.4 uses Cobra v1.2.1 which has a bug about completing flags when using plugins. This was fixed in tanzu-framework by #1251. However, while TCE remains on tanzu-framework v0.11.4, this bug will remain.

Another problem seems to be that the -h and --help flag don't get completed at all. This seems to be a problem with Cobra as it happens in other CLIs. I had not really paid attention to that before; I will open an issue with Cobra.

Finally, for point 2 of adding dynamic completions, I think it just needs some implementation effort. I personally will soon be posting a PR for the tanzu unmanaged-cluster plugin to add such completions.

marckhouzam added a commit to marckhouzam/community-edition that referenced this issue Jun 22, 2022
Related to vmware-tanzu/tanzu-framework#701

Signed-off-by: Marc Khouzam <kmarc@vmware.com>
@stmcginnis stmcginnis removed their assignment Aug 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/cli area/core-cli kind/feature Categorizes issue or PR as related to a new feature
Projects
None yet
Development

No branches or pull requests

5 participants