Skip to content

Commit e38d0a6

Browse files
authored
fix: panic when prompt cannot parse (#27)
1 parent 17abe0d commit e38d0a6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

cmd/prompt.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
"github.com/spf13/cobra"
1818
)
1919

20+
var (
21+
ErrMalformedCommand = errors.New("malformed command")
22+
)
23+
2024
type prompt struct {
2125
promptColor goprompt.Color
2226
history []string
@@ -42,8 +46,7 @@ func (p *prompt) completionsFromCommand(subCommand *cobra.Command, completionsAr
4246
if err != nil {
4347
return []goprompt.Suggest{}
4448
}
45-
46-
return goprompt.FilterHasPrefix(fctl.Map(completions, func(src string) goprompt.Suggest {
49+
suggestion := goprompt.FilterHasPrefix(fctl.Map(completions, func(src string) goprompt.Suggest {
4750
parts := strings.SplitN(src, "\t", 2)
4851
description := ""
4952
if len(parts) > 1 {
@@ -54,6 +57,12 @@ func (p *prompt) completionsFromCommand(subCommand *cobra.Command, completionsAr
5457
Description: description,
5558
}
5659
}), d.GetWordBeforeCursor(), true)
60+
61+
suggestion = append(suggestion, goprompt.Suggest{
62+
Text: ":",
63+
Description: "Custom command",
64+
})
65+
return suggestion
5766
}
5867

5968
func (p *prompt) completions(cfg *fctl.Config, d goprompt.Document) []goprompt.Suggest {
@@ -124,7 +133,7 @@ func (p *prompt) startPrompt(prompt string, cfg *fctl.Config, opts ...goprompt.O
124133
func (p *prompt) executeCommand(cmd *cobra.Command, t string) error {
125134
parse, err := shellwords.Parse(t)
126135
if err != nil {
127-
panic(err)
136+
return fmt.Errorf("%w: %w", errors.ErrUnsupported, err)
128137
}
129138

130139
subCommand := NewRootCommand()
@@ -146,7 +155,7 @@ func (p *prompt) executePromptCommand(cmd *cobra.Command, t string) error {
146155
v = strings.TrimRight(v, " ")
147156
parts := strings.SplitN(v, " ", 2)
148157
if len(parts) != 2 {
149-
return errors.New("malformed command")
158+
return ErrMalformedCommand
150159
} else {
151160
if v := parts[0]; v != fctl.ProfileFlag && v != fctl.DebugFlag && v != fctl.InsecureTlsFlag {
152161
return fmt.Errorf("unknown configuration: %s", v)
@@ -156,7 +165,7 @@ func (p *prompt) executePromptCommand(cmd *cobra.Command, t string) error {
156165
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Set %s=%s", parts[0], parts[1])
157166
}
158167
default:
159-
return errors.New("malformed command")
168+
return ErrMalformedCommand
160169
}
161170
return nil
162171
}

0 commit comments

Comments
 (0)