mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
Execute() runs step commands via bash -e -c, captures stdout/stderr separately, extracts exit codes via syscall.WaitStatus, and handles context timeout/cancellation. Merged code blocks (---) are joined before execution. Six tests cover echo, exit codes, multiline, timeout, stderr capture, and merged blocks.
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Execute runs a bash command from a Step and returns the result.
|
|
func Execute(ctx context.Context, s Step) StepResult {
|
|
start := time.Now()
|
|
|
|
command := s.Command
|
|
// Merge consecutive code blocks separated by "---".
|
|
command = strings.ReplaceAll(command, "\n---\n", "\n")
|
|
|
|
cmd := exec.CommandContext(ctx, "bash", "-e", "-c", command)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
elapsed := time.Since(start)
|
|
|
|
result := StepResult{
|
|
Step: s,
|
|
DurationMs: msDuration(elapsed),
|
|
Stdout: stdout.String(),
|
|
Stderr: stderr.String(),
|
|
}
|
|
|
|
if err != nil {
|
|
var exitErr *exec.ExitError
|
|
if errors.As(err, &exitErr) {
|
|
if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
result.ExitCode = ws.ExitStatus()
|
|
} else {
|
|
result.ExitCode = exitErr.ExitCode()
|
|
}
|
|
result.Status = "failed"
|
|
} else if ctx.Err() != nil {
|
|
// Context timeout or cancellation.
|
|
result.Status = "failed"
|
|
result.ExitCode = -1
|
|
result.Error = ctx.Err().Error()
|
|
} else {
|
|
result.Status = "failed"
|
|
result.ExitCode = -1
|
|
result.Error = err.Error()
|
|
}
|
|
return result
|
|
}
|
|
|
|
result.Status = "passed"
|
|
result.ExitCode = 0
|
|
return result
|
|
}
|