diff --git a/cmd/completion.go b/cmd/completion.go index 4aa4a78..b05dcde 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -19,28 +19,37 @@ func newCompletionCmd(driver summon.Lister) *cobra.Command { } c := &cobra.Command{ - Use: "completion", - Short: "Output bash completion script", - Long: `To load completion run + Use: "completion [bash|zsh]", + Short: "Generate completion script", + Long: `To load completion, run -. <(summon completion) +. <(summon completion bash) + +or + +. <(summon completion zsh) To configure your bash shell to load completions for each session add to your bashrc # ~/.bashrc or ~/.profile -. <(summon completion) +. <(summon completion bash) + +# ~/.zshrc +. <(summon completion zsh) `, + ValidArgs: []string{"bash", "zsh"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), RunE: func(cmd *cobra.Command, args []string) error { cOpts.out = cmd.OutOrStdout() cOpts.cmd = cmd - return cOpts.run() + return cOpts.run(args[0]) }, } return c } -func (c completionCmdOpts) run() error { +func (c completionCmdOpts) run(shell string) error { list, err := c.driver.List() if err != nil { return err @@ -54,6 +63,12 @@ func (c completionCmdOpts) run() error { }) } - // TODO support other shells - return c.cmd.Root().GenBashCompletionV2(c.out, true) + switch shell { + case "bash": + return c.cmd.Root().GenBashCompletionV2(c.out, true) + case "zsh": + return c.cmd.Root().GenZshCompletion(c.out) + } + + return nil } diff --git a/cmd/completion_test.go b/cmd/completion_test.go index 43f6d74..0eac722 100644 --- a/cmd/completion_test.go +++ b/cmd/completion_test.go @@ -4,19 +4,41 @@ import ( "bytes" "testing" - "github.com/davidovich/summon/pkg/summon" "github.com/stretchr/testify/assert" ) func TestCompletionCommand(t *testing.T) { - s, _ := summon.New(cmdTestFS) + tt := []struct { + name string + args []string + want string + assertion assert.ComparisonAssertionFunc + }{ + { + name: "bash", + args: []string{"completion", "bash"}, + want: "# bash completion V2 for summon", + assertion: assert.Contains, + }, + { + name: "zsh", + args: []string{"completion", "zsh"}, + want: "compdef _summon summon", + assertion: assert.Contains, + }, + } - cmd := newCompletionCmd(s) + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + _, rootCmd := makeRootCmd(false, tc.args...) - b := &bytes.Buffer{} + b := &bytes.Buffer{} - cmd.SetOutput(b) - cmd.Execute() + rootCmd.SetOut(b) + err := rootCmd.Execute() - assert.Contains(t, b.String(), "# bash completion V2 for completion") + assert.NoError(t, err) + tc.assertion(t, b.String(), tc.want) + }) + } } diff --git a/cmd/root_test.go b/cmd/root_test.go index 90bcee1..a90f95a 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -66,7 +66,7 @@ func Test_createRootCmd(t *testing.T) { }, { name: "completion_run", - rootCmd: makeRootCmd("completion"), + rootCmd: makeRootCmd("completion", "bash"), expected: "# bash completion V2 for summon", }, {