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:
parent
ff2495fbd1
commit
f1bd471526
34
cmd/completion.go
Normal file
34
cmd/completion.go
Normal 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
23
cmd/completion_test.go
Normal 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")
|
||||
}
|
||||
}
|
@ -74,6 +74,7 @@ func newRootCmd(version string, exit func(int)) *rootCmd {
|
||||
newReleaseCmd().cmd,
|
||||
newCheckCmd().cmd,
|
||||
newInitCmd().cmd,
|
||||
newCompletionCmd().cmd,
|
||||
)
|
||||
|
||||
root.cmd = cmd
|
||||
|
Loading…
x
Reference in New Issue
Block a user