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 bash completion #128

Merged
merged 2 commits into from
Jun 15, 2020
Merged

Fix bash completion #128

merged 2 commits into from
Jun 15, 2020

Conversation

phamann
Copy link
Member

@phamann phamann commented Jun 15, 2020

TL;DR

Fixes bash/zsh completion and adds documentation to README.md. Closes #127

Why?

Unfortunately our flag parsing library kingpin produces the bash completion as a side-effect of its main kingpin.Parse(args ...) function. It also doesn't follow its own convention of writing the output the io.Writer's provided to app.Writers(out, in), thus we have no control over the output. Lastly it attempts to call app.Terminate() to exit the program early. See full implementation here.

We attempt to control kingpin's side-effects via suppressing any writing to stdout/err during Parse() and preventing it from terminating the application - which gives us much greater control over error and usage output. However our own logic was conflicting with the completion and appending usage to the end of the output thus breaking completion.

How?

Due to the above, the easiest solution is to detect that completion flags are present and allow kingpins parsing logic to terminate the app early. This has the trade-off of making it harder to test, which is why I've omitted any test coverage in this PR 😿

@phamann phamann added the bug Something isn't working label Jun 15, 2020
@phamann phamann requested a review from thommahoney June 15, 2020 10:13
@phamann phamann force-pushed the phamann/fix-bash-completion branch from 8d6e054 to edb3f30 Compare June 15, 2020 10:24
```
eval "$(fastly --completion-script-zsh)"
```

Copy link
Contributor

Choose a reason for hiding this comment

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

I find this a tad confusing - what's the difference between --completion-bash and --completion-script-bash? Does installing it in the profile avoid the need to use the --completion-bash flag? When would you want to do one vs the other? And when we say autocomplete do we mean tab-based autocomplete? If not how do I trigger the autocompletion (I know this is a shell specific thing but you're providing instructions for both popular shells and I imagine the resulting UX would be similar?)

Copy link
Member Author

@phamann phamann Jun 15, 2020

Choose a reason for hiding this comment

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

Good questions.

The --completetion-script-bash generates the shell script that you'd source in your bash profile via eval "$(fastly --completion-script-bash)" or similar, and tells your shell how to load the tab autocompletions.

_fastly_bash_autocomplete() {
    local cur prev opts base
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} )
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
}
complete -F _fastly_bash_autocomplete fastly

The --completion-bash actually provides the completions in a newline delimited list. Such as:

create
list
describe
update
delete

So TL;DR the script tells your shell how to invoke the CLI with the completion flag to get the list of tab completions. I.e. users only need to know about --completion-script-bash and how to source it in their shell.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's great, thanks. Do you think people other than me would benefit from that context? If so, you could update the README text.

@phamann phamann force-pushed the phamann/fix-bash-completion branch from edb3f30 to 3711b8d Compare June 15, 2020 20:29
Copy link
Member

@thommahoney thommahoney left a comment

Choose a reason for hiding this comment

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

My comment is not blocking. I think @triblondon wanted to move the installation instructions into their own file anyway.

README.md Outdated Show resolved Hide resolved
@phamann phamann force-pushed the phamann/fix-bash-completion branch from 3711b8d to d062f50 Compare June 15, 2020 20:34
@phamann phamann merged commit 396b7ba into master Jun 15, 2020
@phamann phamann deleted the phamann/fix-bash-completion branch June 15, 2020 20:43
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.

Bash Autocomplete is broken
3 participants