Skip to content

Commit

Permalink
handle invalid commands (kubeshop#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
huseyinbabal authored May 9, 2023
1 parent bbadf65 commit 760e70d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions pkg/execute/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
anonymizedInvalidVerb = "{invalid verb}"

lineLimitToShowFilter = 16

invalidCmdWithUsage = "error: unknown option `%s`\nusage: %s"
)

var newLinePattern = regexp.MustCompile(`\r?\n`)
Expand Down Expand Up @@ -168,8 +170,9 @@ func (e *DefaultExecutor) Execute(ctx context.Context) interactive.CoreMessage {
reportedCmd = fmt.Sprintf("%s {invalid feature}", reportedCmd)
}
e.reportCommand(ctx, "", reportedCmd, false, cmdCtx)
msg := e.cmdsMapping.HelpMessageForVerb(cmdVerb)
return respond(msg, cmdCtx)
helpMsg := e.cmdsMapping.HelpMessageForVerb(cmdVerb)
responseMsg := fmt.Sprintf(invalidCmdWithUsage, cmdRes, helpMsg)
return respond(responseMsg, cmdCtx)
} else {
cmdToReport := string(cmdVerb)
if cmdRes != "" {
Expand Down
9 changes: 7 additions & 2 deletions pkg/execute/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

const (
helpMsgHeader = "%s %s [feature]\n\nAvailable features:\n"
helpMsgHeaderWithFeatures = "%s %s [feature]\n\nAvailable features:\n"
helpMsgHeader = "%s %s"
// noFeature is used for commands that have no features defined
noFeature = ""
// incompleteCmdMsg incomplete command response message
Expand Down Expand Up @@ -127,8 +128,12 @@ func (m *CommandMapping) HelpMessageForVerb(verb command.Verb) string {
}
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 3, 0, 1, ' ', 0)
if len(cmd) > 0 && cmd[0].Name != "" {
fmt.Fprintf(w, helpMsgHeaderWithFeatures, api.MessageBotNamePlaceholder, verb)
} else {
fmt.Fprintf(w, helpMsgHeader, api.MessageBotNamePlaceholder, verb)
}

fmt.Fprintf(w, helpMsgHeader, api.MessageBotNamePlaceholder, verb)
for _, feature := range cmd {
aliases := removeEmptyFeatures(feature.Aliases)
fmtStr := fmt.Sprintf("%s\t", feature.Name)
Expand Down

0 comments on commit 760e70d

Please sign in to comment.