Skip to content

Commit 92d8b18

Browse files
committed
Validate input and handle errors in RunBangCommand
Add input validation to reject empty commands and properly handle the error from exec.CommandContext instead of silently discarding it. When the command fails and produces no output, the error message is shown to the user. Fixes #1716 Assisted-By: cagent
1 parent ece3271 commit 92d8b18

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

pkg/app/app.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,18 @@ func (a *App) RunWithMessage(ctx context.Context, cancel context.CancelFunc, msg
446446
}
447447

448448
func (a *App) RunBangCommand(ctx context.Context, command string) {
449-
out, _ := exec.CommandContext(ctx, "/bin/sh", "-c", command).CombinedOutput()
450-
a.events <- runtime.ShellOutput("$ " + command + "\n" + string(out))
449+
command = strings.TrimSpace(command)
450+
if command == "" {
451+
a.events <- runtime.ShellOutput("Error: empty command")
452+
return
453+
}
454+
455+
out, err := exec.CommandContext(ctx, "/bin/sh", "-c", command).CombinedOutput()
456+
output := "$ " + command + "\n" + string(out)
457+
if err != nil && len(out) == 0 {
458+
output = "$ " + command + "\nError: " + err.Error()
459+
}
460+
a.events <- runtime.ShellOutput(output)
451461
}
452462

453463
func (a *App) Subscribe(ctx context.Context, program *tea.Program) {

0 commit comments

Comments
 (0)