Skip to content

Move go custom completions to Cobra 1.0 - #8131

Merged
marckhouzam merged 1 commit into
helm:masterfrom
VilledeMontreal:feat/compInGoWithCobra
Jun 11, 2020
Merged

marckhouzam merged 1 commit into
helm:masterfrom
VilledeMontreal:feat/compInGoWithCobra

Conversation

@marckhouzam

@marckhouzam marckhouzam commented May 15, 2020 •

Copy link
Copy Markdown
Member

What this PR does / why we need it:

Cobra 1.0 introduces custom Go completions. This commit replaces Helm's own solution to use Cobra's solution. This allows to completely remove Helm's internal "completion" package and reduce our maintenance burden.

It is also a requirement to benefit from other Cobra completion improvements like Fish completions (#8111), an upcoming native zsh completion, etc.

Special notes for your reviewer:

Cobra's solution is actually a port of Helm's solution so it is extremely similar. The main difference is that Cobra's solution is slightly more elegant because it introduces a new field to the Cobra command structure: ValidArgsFunction which replaces helm's completion.RegisterValidArgsFunc().

This PR basically does the following:

  1. remove the internal/completion/complete.go file (the completion package)
  2. move each call to completion.RegisterValidArgsFunc() to the new ValidArgsFunction field
  3. replace completion.RegisterFlagCompletionFunc() with cmd.RegisterFlagCompletionFunc()
  4. replace constants from helm's completion package, with the corresponding one from the cobra package

No functionality is changed and no new functionality is introduced.
This was mostly a chore commit.

If applicable:

  • this PR has been tested for backwards compatibility using the acceptance-testing repo

@marckhouzam
marckhouzam force-pushed the feat/compInGoWithCobra branch from 69e5f6a to e16807a Compare May 22, 2020 18:59
@helm-bot helm-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label May 22, 2020
@marckhouzam

Copy link
Copy Markdown
Member Author

The force push was a rebase to master.

If anyone has time to have a look, a quick way to feel warm and fuzzy about this change is to run the completion tests of the acceptance-testing repo. Which can be done from the helm repo with:

make test-acceptance-completion

@marckhouzam
marckhouzam force-pushed the feat/compInGoWithCobra branch from e16807a to da479ff Compare June 3, 2020 13:13
@marckhouzam

Copy link
Copy Markdown
Member Author

Force-push done to resolve conflicts with latest master.

@marckhouzam marckhouzam added this to the 3.3.0 milestone Jun 4, 2020
Comment thread cmd/helm/flags.go
})

if err != nil {
log.Fatal(err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are a number of log.Fatal calls added to the completion functions. Could this cause Helm to exit when completion is not critical? Meaning should we print an error and continue?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I wasn't sure if it followed helm's correct practices when I added the log.Fatal() calls.

You'll notice that all of them are on the initialization code path, and not on the completion generation path. Such a log.Fatal() call would be triggered when receiving a Cobra error when setting up completion during initialization, and would be caused by a coding error. So I thought it was ok to abort helm since this would be seen immediately during development.

But I'll follow whichever best practice you recommend.

Comment thread go.sum Outdated
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
helm.sh/helm v2.16.7+incompatible h1:3Hp8GLmr6uuBNYGmTQ4p3J4N6xKqxvFDswxHAOposlM=

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

helm-v3 should not import helm-v2

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorry about that. I don't know how that got there. But I should have noticed it before pushing it. I have removed it in the last push and also rebased to master.

Cobra 1.0 introduces custom Go completions.  This commit replaces Helm's
own solution to use Cobra's solution.

This allows to completely remove Helm's internal "completion" package.

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam
marckhouzam force-pushed the feat/compInGoWithCobra branch from da479ff to 001cb3a Compare June 11, 2020 19:26
@marckhouzam

Copy link
Copy Markdown
Member Author

Thanks @adamreese!

@marckhouzam
marckhouzam merged commit 88a3d6e into helm:master Jun 11, 2020
@marckhouzam
marckhouzam deleted the feat/compInGoWithCobra branch June 11, 2020 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants