Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 useapp.Writers()
to setos.Stdout
andos.Stderr
temporarily to useio.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 wasio.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:
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) calledshellcomplete
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.