Gelio / go-global-update

A command to update globally installed go executables
MIT License
134 stars 4 forks source link

Add bash completions #13

Open Gelio opened 2 years ago

Gelio commented 2 years ago

It would be nice to have bash completions for the supported flags and/or binaries passed as arguments.

Specifically, pressing tab for completions should lead to following output:

$ go-global-update --<tab>
--help -h --debug --dry-run --verbose --version -v
$ go-global-update go<tab>
go-global-update gofumpt gotop
Gelio commented 2 years ago

I've done some research and looks like this will be a bit more complex than I originally thought. Things we need to do:

  1. Install (copy) https://github.com/urfave/cli/blob/master/autocomplete/bash_autocomplete as /etc/bash_completion.d/go-global-update

    It would be great to offer a install-bash-autocomplete command/script to make this step easier and not expect users to copy this file manually.

  2. Set EnableBashCompletion to true in main.go, according to https://github.com/urfave/cli/blob/master/docs/v2/manual.md#distribution-and-persistent-autocompletion

  3. For the command arguments (go binary names), we would have to write a custom BashComplete function to get the list of installed binaries

To make sure all of that works, it would be great to add some integration tests that try to invoke the completion with various arguments. This can be done by running go run main.go --generate-bash-completion. We can provide existing arguments there too to simulate various inputs for completion. For example, to get a list of possible flags starting with d:

11:39 $ go run main.go --d --generate-bash-completion
--debug
--dry-run

Other shells

For ZSH, users would need to add a line to their config to enable completion: https://github.com/urfave/cli/blob/master/docs/v2/manual.md#zsh-support

For Powershell, same as for ZSH, they would need to edit the config: https://github.com/urfave/cli/blob/master/docs/v2/manual.md#powershell-support

It looks like they should work along with the custom bash completions