1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-01 13:07:49 +02:00

feat: add shell autocompletion to goreleaser for bash and zsh (#1859)

This commit is contained in:
Daniel Helfand 2020-10-15 14:49:15 -05:00 committed by GitHub
parent ff2495fbd1
commit f1bd471526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

34
cmd/completion.go Normal file
View File

@ -0,0 +1,34 @@
package cmd
import (
"github.com/spf13/cobra"
)
type completionCmd struct {
cmd *cobra.Command
}
func newCompletionCmd() *completionCmd {
var root = &completionCmd{}
var cmd = &cobra.Command{
Use: "completion",
Short: "Print shell autocompletion scripts for goreleaser for bash and zsh",
SilenceUsage: true,
ValidArgs: []string{"bash", "zsh"},
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())
}
return err
},
}
root.cmd = cmd
return root
}

23
cmd/completion_test.go Normal file
View File

@ -0,0 +1,23 @@
package cmd
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
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")
}
}

View File

@ -74,6 +74,7 @@ func newRootCmd(version string, exit func(int)) *rootCmd {
newReleaseCmd().cmd,
newCheckCmd().cmd,
newInitCmd().cmd,
newCompletionCmd().cmd,
)
root.cmd = cmd