mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
refactor(display): abstract shell command runs
This commit is contained in:
+25
-17
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user