1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-30 04:50:45 +02:00

feat: add fish completion (#1860)

refs #1858 # #1859

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2020-10-16 08:01:36 -03:00 committed by GitHub
parent f1bd471526
commit afda24a1e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 18 deletions

View File

@ -1,8 +1,6 @@
package cmd
import (
"github.com/spf13/cobra"
)
import "github.com/spf13/cobra"
type completionCmd struct {
cmd *cobra.Command
@ -12,9 +10,9 @@ func newCompletionCmd() *completionCmd {
var root = &completionCmd{}
var cmd = &cobra.Command{
Use: "completion",
Short: "Print shell autocompletion scripts for goreleaser for bash and zsh",
Short: "Print shell autocompletion scripts for goreleaser",
SilenceUsage: true,
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
@ -23,6 +21,8 @@ func newCompletionCmd() *completionCmd {
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

View File

@ -8,16 +8,18 @@ import (
)
func TestCompletionGeneration(t *testing.T) {
for _, shell := range []string{"bash", "zsh"} {
completionCmd := newCompletionCmd().cmd
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
completionCmd.SetOut(stdout)
completionCmd.SetErr(stderr)
completionCmd.SetArgs([]string{shell})
err := completionCmd.Execute()
require.NoError(t, err, shell+" arg experienced error with goreleaser completion:\n"+stderr.String())
require.Equal(t, "", stderr.String(), shell+" arg experienced error with goreleaser completion:\n"+stderr.String())
require.NotEmpty(t, stdout.String(), shell+" arg reported nothing to stdout with goreleaser completion")
for _, shell := range []string{"bash", "zsh", "fish"} {
t.Run(shell, func(t *testing.T) {
completionCmd := newCompletionCmd().cmd
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
completionCmd.SetOut(stdout)
completionCmd.SetErr(stderr)
completionCmd.SetArgs([]string{shell})
err := completionCmd.Execute()
require.NoError(t, err, shell+" arg experienced error with goreleaser completion:\n"+stderr.String())
require.Equal(t, "", stderr.String(), shell+" arg experienced error with goreleaser completion:\n"+stderr.String())
require.NotEmpty(t, stdout.String(), shell+" arg reported nothing to stdout with goreleaser completion")
})
}
}

View File

@ -89,8 +89,8 @@ func shouldPrependRelease(cmd *cobra.Command, args []string) bool {
return false
}
// allow help command.
if len(args) > 0 && args[0] == "help" {
// allow help and __complete commands.
if len(args) > 0 && (args[0] == "help" || args[0] == "__complete") {
return false
}

View File

@ -88,4 +88,8 @@ func TestShouldPrependRelease(t *testing.T) {
t.Run("help", func(t *testing.T) {
require.False(t, result([]string{"help"}))
})
t.Run("__complete", func(t *testing.T) {
require.False(t, result([]string{"help"}))
})
}