-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: add CompletionWithDesc helper #2231
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
Conversation
6ee0588
to
5345ce2
Compare
My only hesitation about making this change is that programs that want to use the new type alias will need to replace Would |
The type alias is not required. But I get your point. IDE or AI tool will complete it this way I think cobra.Completion is clear enough. But then, we also move to cobra.CompletionWithDescription ? I'm fine with CompletionWithDescription and CompletionWithDesc What would be your choice? |
OK for cobra.Completion |
277becc
to
8499067
Compare
The code has also been refactored to use a type alias for completion and a completion helper Using a type alias is a non-breaking change and it makes the code more readable and easier to understand. Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com> Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com> Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
8499067
to
8bcda5b
Compare
The completion should be tested also in a mode that returns the description Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com> Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
I tried this with I went ahead and used your new |
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
I've just pushed a commit to use the new type alias and function everywhere. |
I'm more than fine with the changes, you did. I love the way how it somehow shows the changed was not only a good thing, but needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
Thanks!
From what I can see Go can handle the conversion between the original The same goes for the type defined by the docker CLI: The problem happens because now we are asking go to accept the type Cobra should not break programs, so we should fix this in Cobra; who knows how many other programs use their own type for the completion functions. We could simply revert and not define What do people think? I haven't used type aliases much so I don't have a good feel for it. |
Thank you @marckhouzam for the investigation Sorry for the inconvenience @thaJeztah or anyone who may have face this issue. I thibk the issue is in fact related to #2220 where CompletionFunc was added The changes in the current PR and #2220 were released at same time. I think the solution of using a type alias for CompletionFunc so adding an alias is a good one. I'm surprised the problem was not covered by a test, I can add one. I would recommend to retract 1.9.0 for now via a 1.9.1 But the fix seems easy. So let's try to fix it |
Thanks both! Sorry for my rather brief comment earlier; I saw the issue reported and only had time to dive in for a tiny bit because I had to head out. I'm not sure how widely spread the problem is; if the issue is purely in our specific situation or more widespread. At least I saw that part of the issue on our side is that we define a I suspect that fixing on our side would not be too complicated and could possibly be limited to just that (aliasing our Looking at the comment, I see it was mostly done to create a "pseudo" type to make for a nicer documentation;
I've done similar tricks in some cases for the same reason, and most of the time it works without issues, but there's definitely been cases where it's slightly more involved or not possible without breaking things (unfortunately!). So .. possibly changing back the type CompletionFunc func(cmd *Command, args []string, toComplete string) (completion []string, _ ShellCompDirective)
Ah, possibly that sounds plausible as well, and possibly could also explain why we have that local |
I did a quick test, and it seems that making the (I asked @tassa-yoniso-manasi-karoto, who reported the issue (thanks!!) to verify if that approach works) |
Thanks for your note @thaJeztah I managed to understand the issue. I confirm adding the type alias as @marckhouzam suggested fixes the issue. I managed to replicate the issue locally. I'm working on a fix. |
Awesome, thank you! And no stress (please!) the world didn't burn because of this, and it was great to be discovered this soon after the release. |
Here is the fix |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/spf13/cobra](https://github.com/spf13/cobra) | require | minor | `v1.8.1` -> `v1.9.1` | --- ### Release Notes <details> <summary>spf13/cobra (github.com/spf13/cobra)</summary> ### [`v1.9.1`](https://github.com/spf13/cobra/releases/tag/v1.9.1) [Compare Source](spf13/cobra@v1.9.0...v1.9.1) ##### 🐛 Fixes - Fix CompletionFunc implementation by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2234 - Revert "Make detection for test-binary more universal ([#​2173](spf13/cobra#2173))" by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2235 **Full Changelog**: spf13/cobra@v1.9.0...v1.9.1 ### [`v1.9.0`](https://github.com/spf13/cobra/releases/tag/v1.9.0) [Compare Source](spf13/cobra@v1.8.1...v1.9.0) #### ✨ Features - Allow linker to perform deadcode elimination for program using Cobra by [@​aarzilli](https://github.com/aarzilli) in spf13/cobra#1956 - Add default completion command even if there are no other sub-commands by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#1559 - Add CompletionWithDesc helper by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2231 #### 🐛 Fixes - Fix deprecation comment for Command.SetOutput by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2172 - Replace deprecated ioutil usage by [@​nirs](https://github.com/nirs) in spf13/cobra#2181 - Fix --version help and output for plugins by [@​nirs](https://github.com/nirs) in spf13/cobra#2180 - Allow to reset the templates to the default by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2229 #### 🤖 Completions - Make Powershell completion work in constrained mode by [@​lstemplinger](https://github.com/lstemplinger) in spf13/cobra#2196 - Improve detection for flags that accept multiple values by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2210 - add CompletionFunc type to help with completions by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2220 - Add similar whitespace escape logic to bash v2 completions than in other completions by [@​kangasta](https://github.com/kangasta) in spf13/cobra#1743 - Print ActiveHelp for bash along other completions by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2076 - fix(completions): Complete map flags multiple times by [@​gabe565](https://github.com/gabe565) in spf13/cobra#2174 - fix(bash): nounset unbound file filter variable on empty extension by [@​scop](https://github.com/scop) in spf13/cobra#2228 #### 🧪 Testing - Test also with go 1.23 by [@​nirs](https://github.com/nirs) in spf13/cobra#2182 - Make detection for test-binary more universal by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2173 #### ✍🏼 Documentation - docs: update README.md by [@​eltociear](https://github.com/eltociear) in spf13/cobra#2197 - Improve site formatting by [@​nirs](https://github.com/nirs) in spf13/cobra#2183 - doc: add Conduit by [@​raulb](https://github.com/raulb) in spf13/cobra#2230 - doc: azion project added to the list of CLIs that use cobra by [@​maxwelbm](https://github.com/maxwelbm) in spf13/cobra#2198 - Fix broken links in active_help.md by [@​vuil](https://github.com/vuil) in spf13/cobra#2202 - chore: fix function name in comment by [@​zhuhaicity](https://github.com/zhuhaicity) in spf13/cobra#2216 #### 🔧 Dependency upgrades - build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.5 to 2.0.6 by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2206 - Update to latest go-md2man by [@​mikelolasagasti](https://github.com/mikelolasagasti) in spf13/cobra#2201 - Upgrade `pflag` dependencies for v1.9.0 by [@​jpmcb](https://github.com/jpmcb) in spf13/cobra#2233 *** Thank you to all of our amazing contributors and all the great work that's been going into the completions feature!! ##### 👋🏼 New Contributors - [@​gabe565](https://github.com/gabe565) made their first contribution in spf13/cobra#2174 - [@​maxwelbm](https://github.com/maxwelbm) made their first contribution in spf13/cobra#2198 - [@​lstemplinger](https://github.com/lstemplinger) made their first contribution in spf13/cobra#2196 - [@​vuil](https://github.com/vuil) made their first contribution in spf13/cobra#2202 - [@​mikelolasagasti](https://github.com/mikelolasagasti) made their first contribution in spf13/cobra#2201 - [@​zhuhaicity](https://github.com/zhuhaicity) made their first contribution in spf13/cobra#2216 - [@​ccoVeille](https://github.com/ccoVeille) made their first contribution in spf13/cobra#2220 - [@​kangasta](https://github.com/kangasta) made their first contribution in spf13/cobra#1743 - [@​aarzilli](https://github.com/aarzilli) made their first contribution in spf13/cobra#1956 **Full Changelog**: spf13/cobra@v1.8.1...v1.9.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4wLjgiLCJ1cGRhdGVkSW5WZXIiOiI0MC4wLjgiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbXX0=--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/557 Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org> Co-authored-by: Renovate Bot <bot@kriese.eu> Co-committed-by: Renovate Bot <bot@kriese.eu>
The code has also been refactored to use a type alias for completion and a completion helper
Using a type alias is a non-breaking change, and it makes the code more readable and easier to understand.
Fixes #2222