diff options
Diffstat (limited to 'executor.go')
-rw-r--r-- | executor.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/executor.go b/executor.go new file mode 100644 index 0000000..ca8e5e2 --- /dev/null +++ b/executor.go @@ -0,0 +1,38 @@ +package monzero + +import ( + "bytes" + "context" + "fmt" + "os/exec" + "syscall" +) + +// CheckExec runs a command line string. +// The output is recorded completely and returned as one message. +func CheckExec(check Check, ctx context.Context) CheckResult { + result := CheckResult{} + + cmd := exec.CommandContext(ctx, check.Command[0], check.Command[1:]...) + output := bytes.NewBuffer([]byte{}) + cmd.Stdout = output + cmd.Stderr = output + err := cmd.Run() + if err != nil { + if cmd.ProcessState == nil { + result.Message = fmt.Sprintf("unknown error when running command: %w", err) + result.ExitCode = 3 + return result + } + + status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus) + if !ok { + result.Message = fmt.Sprintf("error running check: %w", err) + result.ExitCode = 2 + } else { + result.ExitCode = status.ExitStatus() + } + } + result.Message = output.String() + return result +} |