Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ clusterctl completion zsh: Use native zsh completion #4113

Merged

Conversation

superbrothers
Copy link
Contributor

What this PR does / why we need it:

ref/ #4090

Due to this change, we will not able to load the zsh completion code
with the following method:

source <(clusterctl completion zsh)

Instead, we execute the following command once:

clusterctl completion zsh >"${fpath[1]}/_clusterctl"

Then we need to start a new shell for this setup to take effect.


In addition, github.com/spf13/cobra is updated to v1.1.1 in order to use
native zsh completion feature.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Closes #4090

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 24, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @superbrothers. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 24, 2021
@prankul88
Copy link
Contributor

/assign

Thanks for this PR. I can review this to check functionality.

cmd/clusterctl/cmd/completion.go Show resolved Hide resolved
echo "autoload -U compinit; compinit" >> ~/.zshrc

# To load completions for each session, execute once:
clusterctl completion zsh > "${fpath[1]}/_clusterctl"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting a permission error when running this command,

zsh: permission denied: /usr/local/share/zsh/site-functions/_clusterctl

Not sure if only it is system-specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the system.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 2, 2021
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 4, 2021
Copy link
Contributor

@wfernandes wfernandes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@superbrothers Thanks for this update! I haven't been able to test the zsh completion but it mostly lgtm just some nits and clarifications regarding some files.

cmd/clusterctl/cmd/completion.go Outdated Show resolved Hide resolved
go.mod.orig Outdated Show resolved Hide resolved
go.sum.orig Outdated Show resolved Hide resolved
Due to this change, we will not able to load the zsh completion code
with the following method:
```
source <(clusterctl completion zsh)
```

Instead, we execute the following command once:
```
clusterctl completion zsh >"${fpath[1]}/_clusterctl"
```
Then we need to start a new shell for this setup to take effect.

---

In addition, github.com/spf13/cobra is updated to v1.1.1 in order to use
native zsh completion feature.
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Feb 5, 2021
@wfernandes
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 5, 2021
@CecileRobertMichon
Copy link
Contributor

➜  cluster-api git:(pr/4113) bin/clusterctl 

alpha       -- Commands for features in alpha.
completion  -- Output shell completion code for the specified shell (bash or
config      -- Display provider configuration and templates to create worklo
delete      -- Delete one or more providers from the management cluster.
describe    -- Describe workload clusters.
generate    -- Generate yaml using clusterctl yaml processor.
get         -- Get info from a management or a workload cluster
help        -- Help about any command
init        -- Initialize a management cluster.
move        -- Move Cluster API objects and all dependencies between managem
upgrade     -- Upgrade core and provider components in a management cluster.
version     -- Print clusterctl version.

Works for me!

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 8, 2021
@wfernandes
Copy link
Contributor

/lgtm
/approve

@CecileRobertMichon
Copy link
Contributor

Just one question: why is the change marked as breaking ⚠️ ?

@wfernandes
Copy link
Contributor

@CecileRobertMichon It seems that the loading of the zsh logic (from the changes in the docs) has changed a bit.

@CecileRobertMichon
Copy link
Contributor

@CecileRobertMichon It seems that the loading of the zsh logic (from the changes in the docs) has changed a bit.

Ok, just wanted to make sure there wasn't another breaking change I had missed

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CecileRobertMichon, wfernandes

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 8, 2021
@wfernandes
Copy link
Contributor

/test pull-cluster-api-test-main

@k8s-ci-robot k8s-ci-robot merged commit 207400d into kubernetes-sigs:master Feb 8, 2021
@superbrothers superbrothers deleted the completion-zsh-native branch February 9, 2021 01:07
@marckhouzam
Copy link

Just one question: why is the change marked as breaking ⚠️ ?

Due to this change, we will not able to load the zsh completion code with the following method:

source <(clusterctl completion zsh)

Zsh recommends putting the completion file on fpath instead of sourcing it, which is why Cobra does not support sourcing the completion file. This is better for performance as it enables zsh to do some sort of caching of the script.

It is however possible to support sourcing if you feel it is better for your users. To do that you have to add the line compdef _clusterctl clusterctl to the completion script (at the end of the script is safe). This is what the Helm project has chosen to do to avoid a "breaking" change: https://github.com/helm/helm/blob/037a43c98bc6331ffe7027d89e57fb1334cc1189/cmd/helm/completion.go#L168

@wfernandes
Copy link
Contributor

@marckhouzam We have this in our documentation https://master.cluster-api.sigs.k8s.io/clusterctl/commands/completion.html#zsh which seems like it adds it to the fpath.

Also the output of clusterctl completion zsh has compdef _clusterctl clusterctl as the first line.

So I guess this isn't a breaking change. Yay!

@marckhouzam
Copy link

@marckhouzam We have this in our documentation https://master.cluster-api.sigs.k8s.io/clusterctl/commands/completion.html#zsh which seems like it adds it to the fpath.

Well, it tells users to add it themselves 😁. This is great and may be what you prefer to stick with. I just wanted to mention there is a way to enable sourcing of you feel it is important.

Also the output of clusterctl completion zsh has compdef _clusterctl clusterctl as the first line.

I believe the first line starts with a #, which makes the line a directive for zsh when the script is on fpath. You would need a second one without the # for sourcing to work.

But I'm not arguing in favor, I just wanted to inform as it is something that has come up a couple of times in Cobra Issues.

@superbrothers
Copy link
Contributor Author

It is however possible to support sourcing if you feel it is better for your users. To do that you have to add the line compdef _clusterctl clusterctl to the completion script (at the end of the script is safe). This is what the Helm project has chosen to do to avoid a "breaking" change:

This is a nice technique. We should use this technique to avoid a "breaking change".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clusterctl completion: Use native zsh completion instead
6 participants