marcosnils / bin

Effortless binary manager
MIT License
640 stars 44 forks source link

add completion command #156

Closed matheuscscp closed 1 year ago

matheuscscp commented 1 year ago

fixes #155

Hi @marcosnils,

First I tried just upgrading the version of cobra, which indeed adds the default completion command:

➜  bin git:(completion) ✗ go run . -h
Effortless binary manager

Usage:
  bin [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  ensure      Ensures that all binaries listed in the configuration are present
  help        Help about any command
  install     Installs the specified project from a url
  list        List binaries managed by bin
  prune       Prunes binaries that no longer exist in the system
  remove      Removes binaries managed by bin
  update      Updates one or multiple binaries managed by bin

Flags:
      --debug     Enable debug mode
  -h, --help      help for bin
  -v, --version   version for bin

Use "bin [command] --help" for more information about a command.
➜  bin git:(completion) ✗

However, the command does not seem to work. It invokes bin's first command instead (list):

➜  bin git:(completion) ✗ go run . completion -h
List binaries managed by bin

Usage:
  bin list [flags]

Aliases:
  list, ls

Flags:
  -h, --help   help for list

Global Flags:
      --debug   Enable debug mode
➜  bin git:(completion) ✗

So I followed the instructions here: https://github.com/spf13/cobra/blob/main/shell_completions.md#creating-your-own-completion-command

And added cmd/completion.go with pretty much a copy of the example in those instructions, fixing minor differences to fit bin's command definition style.

Now it works:

➜  bin git:(completion) ✗ go run . completion -h
To load completions:

Bash:

  $ source <(bin completion bash)

  # To load completions for each session, execute once:
  # Linux:
  $ bin completion bash > /etc/bash_completion.d/bin
  # macOS:
  $ bin completion bash > $(brew --prefix)/etc/bash_completion.d/bin

Zsh:

  # If shell completion is not already enabled in your environment,
  # you will need to enable it.  You can execute the following once:

  $ echo "autoload -U compinit; compinit" >> ~/.zshrc

  # To load completions for each session, execute once:
  $ bin completion zsh > "${fpath[1]}/_bin"

  # You will need to start a new shell for this setup to take effect.

fish:

  $ bin completion fish | source

  # To load completions for each session, execute once:
  $ bin completion fish > ~/.config/fish/completions/bin.fish

PowerShell:

  PS> bin completion powershell | Out-String | Invoke-Expression

  # To load completions for every new session, run:
  PS> bin completion powershell > bin.ps1
  # and source this file from your PowerShell profile.

Usage:
  bin completion [bash|zsh|fish|powershell]

Flags:
  -h, --help   help for completion

Global Flags:
      --debug   Enable debug mode
➜  bin git:(completion) ✗ 

go run . completion zsh also seems to print the completion scripts for zsh. However, they don't seem to work. When I press tab, this is what I see:

➜  bin git:(completion) ✗ ./bin
bin*                CODE_OF_CONDUCT.md  go.mod              LICENSE             Makefile            README.md                                                                                   
cmd/                CONTRIBUTING.md     go.sum              main.go             pkg/                                                                                                          

I wonder if this is because of how bin sets up the cobra commands, which is slightly different than usual (via Go init() functions)... I will investigate this further soon :ok_hand:

marcosnils commented 1 year ago

. I will investigate this further soon

:pray: thx!

matheuscscp commented 1 year ago

Hi @marcosnils, I tried to hackily instantiate the rootCmd in a init() function just to see if it would work, but I'm seeing the same behavior as if I just upgraded the version of cobra, so I need more investigation.

In the meantime I also asked a question in the cobra repo to see if I can get some help: https://github.com/spf13/cobra/issues/1915

matheuscscp commented 1 year ago

hi @marcosnils! I got enough help from cobra and was able to add the default completion command :grin:

➜  bin git:(completion) go build
➜  bin git:(completion) source <(./bin completion zsh); compdef _bin bin
➜  bin git:(completion) ./bin
completion  -- Generate the autocompletion script for the specified shell
ensure      -- Ensures that all binaries listed in the configuration are present
help        -- Help about any command
install     -- Installs the specified project from a url
list        -- List binaries managed by bin
prune       -- Prunes binaries that no longer exist in the system
remove      -- Removes binaries managed by bin
update      -- Updates one or multiple binaries managed by bin
marcosnils commented 1 year ago

:tada: thx for the contribution @matheuscscp !