refactor(display): abstract shell command runs

This commit is contained in:
2025-10-15 16:02:00 +02:00
parent 9ff951e22f
commit 2e0af663c4
+25 -17
View File
@@ -251,26 +251,11 @@ func shellCommandRoute(ctx echo.Context) error {
return ctx.JSON(http.StatusBadRequest, ErrorResponse{Error: "Invalid JSON request"})
}
var stdout, stderr bytes.Buffer
cmd := exec.Command("bash", "-c", "-r", commandInput.Command)
cmd.Dir = storagePath
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
commandOutput := CommandResponse{
Stdout: stdout.String(),
Stderr: stderr.String(),
ExitCode: cmd.ProcessState.ExitCode(),
}
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
commandOutput.ExitCode = exitErr.ExitCode()
} else {
commandOutput.Stderr = err.Error()
}
commandOutput := runShellCommand(cmd)
if commandOutput.ExitCode != 0 {
slog.Error("Shell command execution error", "error", commandOutput.Stderr)
}
@@ -445,3 +430,26 @@ func resetView() error {
return nil
}
func runShellCommand(cmd *exec.Cmd) CommandResponse {
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
commandOutput := CommandResponse{
Stdout: stdout.String(),
Stderr: stderr.String(),
ExitCode: cmd.ProcessState.ExitCode(),
}
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
commandOutput.ExitCode = exitErr.ExitCode()
} else {
commandOutput.Stderr = err.Error()
}
}
return commandOutput
}