Skip to content

Commit

Permalink
feat: added shell completion command
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Feb 19, 2021
1 parent 8f42eae commit c5782a2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ tmp-docs/
dist/

coverage/

completions/
10 changes: 10 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
before:
hooks:
- go mod download
- ./tools/completions.sh
builds:
- env:
- CGO_ENABLED=0
Expand All @@ -25,6 +26,10 @@ archives:
amd64: x86_64
arm: ARM
arm64: ARM64
files:
- README.md
- LICENSE.md
- completions/*
checksum:
name_template: "checksums.txt"
snapshot:
Expand All @@ -48,5 +53,10 @@ brews:
homepage: https://github.com/lindell/multi-gitter
license: "MIT"
folder: Formula
install: |-
bin.install "multi-gitter"
bash_completion.install "completions/multi-gitter.bash" => "multi-gitter"
zsh_completion.install "completions/multi-gitter.zsh" => "_multi-gitter"
fish_completion.install "completions/multi-gitter.fish"
test: |
system "#{bin}/multi-gitter version"
48 changes: 48 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import "github.com/spf13/cobra"

// CompletionCmd generates completion scripts
func CompletionCmd() *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Print shell autocompletion scripts for multi-gitter",
Long: `To load completions:
Bash:
$ source <(multi-gitter completion bash)
# To load completions for each session, execute once:
Linux:
$ multi-gitter completion bash > /etc/bash_completion.d/multi-gitter
MacOS:
$ multi-gitter completion bash > /usr/local/etc/bash_completion.d/multi-gitter
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:
$ multi-gitter completion zsh > "${fpath[1]}/_multi-gitter"
# You will need to start a new shell for this setup to take effect.
Fish:
$ multi-gitter completion fish | source
# To load completions for each session, execute once:
$ multi-gitter completion fish > ~/.config/fish/completions/multi-gitter.fish
`,
SilenceUsage: true,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(cmd.OutOrStdout())
case "zsh":
err = cmd.Root().GenZshCompletion(cmd.OutOrStdout())
case "fish":
err = cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
}

return err
},
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func RootCmd() *cobra.Command {
cmd.AddCommand(MergeCmd())
cmd.AddCommand(CloseCmd())
cmd.AddCommand(PrintCmd())
cmd.AddCommand(CompletionCmd())
cmd.AddCommand(VersionCmd())

return cmd
Expand Down
7 changes: 7 additions & 0 deletions tools/completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
set -e
rm -rf completions
mkdir completions
for sh in bash zsh fish; do
go run main.go completion "$sh" >"completions/multi-gitter.$sh"
done

0 comments on commit c5782a2

Please sign in to comment.