Skip to content

Commit

Permalink
Merge pull request #110 from rsteube/snippet-return-error
Browse files Browse the repository at this point in the history
return error from Snippet
  • Loading branch information
rsteube authored Nov 18, 2020
2 parents c17d323 + 00f4536 commit 6c6fd64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
19 changes: 14 additions & 5 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package carapace

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (c Carapace) Standalone() {
c.cmd.Root().SetHelpCommand(&cobra.Command{Hidden: true})
}

func (c Carapace) Snippet(shell string, lazy bool) string {
func (c Carapace) Snippet(shell string, lazy bool) (string, error) {
var snippet func(cmd *cobra.Command, actions map[string]string, lazy bool) string

if shell == "" {
Expand All @@ -92,9 +93,9 @@ func (c Carapace) Snippet(shell string, lazy bool) string {
case "zsh":
snippet = zsh.Snippet
default:
return fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'oil', 'powershell', 'xonsh' or 'zsh' [was: %v]", shell)
return "", errors.New(fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'oil', 'powershell', 'xonsh' or 'zsh' [was: %v]", shell))
}
return snippet(c.cmd.Root(), completions.actions.Shell(shell), lazy)
return snippet(c.cmd.Root(), completions.actions.Shell(shell), lazy), nil
}

var completions = Completions{
Expand All @@ -112,7 +113,11 @@ func addCompletionCommand(cmd *cobra.Command) {
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println(Gen(cmd).Snippet(determineShell(), true))
if s, err := Gen(cmd).Snippet(determineShell(), true); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
} else {
fmt.Println(s)
}
} else {
if len(args) == 1 {
switch args[0] {
Expand All @@ -121,7 +126,11 @@ func addCompletionCommand(cmd *cobra.Command) {
fmt.Printf("%v:\t%v\n", uid, action)
}
default:
fmt.Println(Gen(cmd).Snippet(args[0], false))
if s, err := Gen(cmd).Snippet(args[0], false); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
} else {
fmt.Println(s)
}
}
} else {
targetCmd, targetArgs := findTarget(cmd)
Expand Down
18 changes: 12 additions & 6 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ _example_completions() {
complete -F _example_completions example
`
rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("bash", false))
s, _ := carapace.Gen(rootCmd).Snippet("bash", false)
assert.Equal(t, expected, s)
}

func TestElvish(t *testing.T) {
Expand Down Expand Up @@ -460,7 +461,8 @@ edit:complex-candidate 'invalid' &display='invalid' }]
}
`
rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("elvish", false))
s, _ := carapace.Gen(rootCmd).Snippet("elvish", false)
assert.Equal(t, expected, s)
}

func TestFish(t *testing.T) {
Expand Down Expand Up @@ -544,7 +546,8 @@ complete -c 'example' -f -n '_example_state _example__multiparts' -l 'slash' -d
complete -c 'example' -f -n '_example_state _example__multiparts' -a '(_example_callback _)'
`
rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("fish", false))
s, _ := carapace.Gen(rootCmd).Snippet("fish", false)
assert.Equal(t, expected, s)
}

func TestPowershell(t *testing.T) {
Expand Down Expand Up @@ -896,7 +899,8 @@ $_example_completer = {
Register-ArgumentCompleter -Native -CommandName 'example' -ScriptBlock $_example_completer
`
rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("powershell", false))
s, _ := carapace.Gen(rootCmd).Snippet("powershell", false)
assert.Equal(t, expected, s)
}

func TestXonsh(t *testing.T) {
Expand Down Expand Up @@ -1226,7 +1230,8 @@ _add_one_completer('example', _example_completer, 'start')
`

rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("xonsh", false))
s, _ := carapace.Gen(rootCmd).Snippet("xonsh", false)
assert.Equal(t, expected, s)
}

func TestZsh(t *testing.T) {
Expand Down Expand Up @@ -1356,5 +1361,6 @@ compquote '' 2>/dev/null && _example
compdef _example example
`
rootCmd.InitDefaultHelpCmd()
assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("zsh", false))
s, _ := carapace.Gen(rootCmd).Snippet("zsh", false)
assert.Equal(t, expected, s)
}

0 comments on commit 6c6fd64

Please sign in to comment.