50 lines
972 B
Go
50 lines
972 B
Go
|
|
package executor
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"os/exec"
|
||
|
|
"runtime"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Result struct {
|
||
|
|
ExitCode int
|
||
|
|
Stdout string
|
||
|
|
Stderr string
|
||
|
|
Success bool
|
||
|
|
}
|
||
|
|
|
||
|
|
func Execute(ctx context.Context, command string, timeoutSec int) *Result {
|
||
|
|
if timeoutSec <= 0 {
|
||
|
|
timeoutSec = 300
|
||
|
|
}
|
||
|
|
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSec)*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
var cmd *exec.Cmd
|
||
|
|
if runtime.GOOS == "windows" {
|
||
|
|
cmd = exec.CommandContext(ctx, "powershell", "-NoProfile", "-NonInteractive", "-Command", command)
|
||
|
|
} else {
|
||
|
|
cmd = exec.CommandContext(ctx, "/bin/sh", "-c", command)
|
||
|
|
}
|
||
|
|
|
||
|
|
var stdout, stderr bytes.Buffer
|
||
|
|
cmd.Stdout = &stdout
|
||
|
|
cmd.Stderr = &stderr
|
||
|
|
|
||
|
|
err := cmd.Run()
|
||
|
|
result := &Result{
|
||
|
|
Stdout: stdout.String(),
|
||
|
|
Stderr: stderr.String(),
|
||
|
|
Success: err == nil,
|
||
|
|
}
|
||
|
|
if cmd.ProcessState != nil {
|
||
|
|
result.ExitCode = cmd.ProcessState.ExitCode()
|
||
|
|
}
|
||
|
|
if err != nil && result.ExitCode == 0 {
|
||
|
|
result.ExitCode = -1
|
||
|
|
}
|
||
|
|
return result
|
||
|
|
}
|