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

Fix shell completion #439

Merged
merged 1 commit into from
Oct 14, 2021
Merged

Fix shell completion #439

merged 1 commit into from
Oct 14, 2021

Conversation

Integralist
Copy link
Collaborator

@Integralist Integralist commented Oct 14, 2021

An earlier refactor of app.Run() caused the shell completion flags to return a runtime panic.

This subsequently broke updating the app via Homebrew as it calls the fastly binary with these flags automatically.

TESTS

I've added tests to validate the shell completion output. This means in future if we were to accidentally break the behaviour again we should see it as a CI error and it will prevent us from merging the breaking code.

NOTES

While implementing a fix for this feature I discovered app.Parse() (a Kingpin method) was resetting the values we set for where Kingpin should write its output (we use app.Writers() to set os.Stdout and os.Stderr temporarily to use io.Discard for reasons already documented in the CLI code).

This was a problem because once I started working on a fix I noticed I was not only seeing the shell completion output but it was being followed by our verbose help output. This was happening because when we execute the CLI binary but only pass a shell completion flag, then we're not actually passing a command (e.g. like fastly acl), so the Kingpin parser thinks this is an error and provides the help output and tells us to provide a command.

Because Kingpin is internally overriding where it writes output to (it sets it to os.Stdout rather than what we had set, which was io.Discard) it means I can't stop Kingpin from displaying the help output. So to work around this I ensure the arguments provided to the Kingpin parser include a valid command (along with the shell completion flag).

An example might look something like:

fastly --completion-script-bash acl

But rather than rely on an actual user feature command like acl, I define a 'hidden' command (i.e. it doesn't show up in the help output) called shellcomplete that we can safely append to the arguments list. We don't have to worry about this hidden command being accidentally removed in the future as we now have a test to validate the shell autocomplete behaviours.

For posterity I've added these notes as comments in the CLI code.

@Integralist Integralist added the bug Something isn't working label Oct 14, 2021
Copy link
Member

@kailan kailan left a comment

Choose a reason for hiding this comment

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

Code and tests look good! Is there a way we can test that this fixes the Homebrew issues before we merge?

@Integralist
Copy link
Collaborator Author

I don't believe so. But we can see that running the relevant commands (e.g. fastly --completion-script-bash) no longer cause a runtime panic so we've fixed the issue from that side of things and so there should be no reason for Homebrew to error now.

@Integralist Integralist merged commit f9429f7 into main Oct 14, 2021
@Integralist Integralist deleted the integralist/homebrew-broken branch October 14, 2021 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants