aboutsummaryrefslogtreecommitdiff
path: root/executor.go
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2021-12-02 15:55:48 +0100
committerGibheer <gibheer+git@zero-knowledge.org>2021-12-02 15:55:48 +0100
commit0639a504ebcc48030f8eefd80839738cfb89a583 (patch)
treef7d8ad5e16f995088c48fc2aec4341bf4784666e /executor.go
parente2b479c34fcee4c41009fc9e8fe869831fbd494f (diff)
add basic infrastructure for writing a checker
These are the first components to interact with the database in a predefined way to write a checker instance. CheckExec serves as an example implementing the Executor interface.
Diffstat (limited to 'executor.go')
-rw-r--r--executor.go38
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
+}