From c5782a2e377ecfc071c82c1db0a775e45215a0cc Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Fri, 19 Feb 2021 21:50:40 +0100 Subject: [PATCH] feat: added shell completion command --- .gitignore | 2 ++ .goreleaser.yml | 10 +++++++++ cmd/completion.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + tools/completions.sh | 7 +++++++ 5 files changed, 68 insertions(+) create mode 100644 cmd/completion.go create mode 100755 tools/completions.sh diff --git a/.gitignore b/.gitignore index 7a2448d3..c3e94d22 100755 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ tmp-docs/ dist/ coverage/ + +completions/ diff --git a/.goreleaser.yml b/.goreleaser.yml index 1465d1ca..73c5a445 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,6 +1,7 @@ before: hooks: - go mod download + - ./tools/completions.sh builds: - env: - CGO_ENABLED=0 @@ -25,6 +26,10 @@ archives: amd64: x86_64 arm: ARM arm64: ARM64 + files: + - README.md + - LICENSE.md + - completions/* checksum: name_template: "checksums.txt" snapshot: @@ -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" diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 00000000..d2d1df58 --- /dev/null +++ b/cmd/completion.go @@ -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 + }, + } +} diff --git a/cmd/root.go b/cmd/root.go index 5713d7c4..d3620829 100755 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 diff --git a/tools/completions.sh b/tools/completions.sh new file mode 100755 index 00000000..2b03b70a --- /dev/null +++ b/tools/completions.sh @@ -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