From 2e0af663c4bdee310f716122718aa9f6e5569420 Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Wed, 15 Oct 2025 16:02:00 +0200 Subject: [PATCH] refactor(display): abstract shell command runs --- display/main.go | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/display/main.go b/display/main.go index d61df66..f11ec6a 100644 --- a/display/main.go +++ b/display/main.go @@ -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 +}