Reduce code duplication
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
6821184fe1
commit
de0a327423
4 changed files with 45 additions and 60 deletions
|
@ -2,12 +2,16 @@ package runners
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
au "github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
|
@ -80,7 +84,7 @@ func checkWait(cmd *exec.Cmd) ([]byte, error) {
|
|||
if err == nil {
|
||||
return e, nil
|
||||
}
|
||||
|
||||
|
||||
if cmd.ProcessState != nil {
|
||||
// this is only populated after program exit - we have an issue
|
||||
return nil, fmt.Errorf("run failed with exit code %d: %s", cmd.ProcessState.ExitCode(), cmd.Stderr.(*bytes.Buffer).String())
|
||||
|
@ -89,3 +93,23 @@ func checkWait(cmd *exec.Cmd) ([]byte, error) {
|
|||
time.Sleep(time.Millisecond * 10)
|
||||
}
|
||||
}
|
||||
|
||||
func readJSONFromCommand(res interface{}, cmd *exec.Cmd) error {
|
||||
|
||||
for {
|
||||
inp, err := checkWait(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(inp, res)
|
||||
if err != nil {
|
||||
// echo anything that won't parse to stdout (this lets us add debug print statements)
|
||||
fmt.Printf("[%s] %v\n", au.BrightRed("DBG"), strings.TrimSpace(string(inp)))
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,28 +6,26 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
au "github.com/logrusorgru/aurora"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
const (
|
||||
golangInstallation = "go"
|
||||
golangWrapperFilename = "runtime-wrapper.go"
|
||||
golangInstallation = "go"
|
||||
golangWrapperFilename = "runtime-wrapper.go"
|
||||
golangWrapperExecutableFilename = "runtime-wrapper"
|
||||
)
|
||||
|
||||
type golangRunner struct {
|
||||
dir string
|
||||
cmd *exec.Cmd
|
||||
wrapperFilepath string
|
||||
dir string
|
||||
cmd *exec.Cmd
|
||||
wrapperFilepath string
|
||||
executableFilepath string
|
||||
stdin io.WriteCloser
|
||||
stdin io.WriteCloser
|
||||
}
|
||||
|
||||
func newGolangRunner(dir string) Runner {
|
||||
|
@ -123,19 +121,8 @@ func (g *golangRunner) Run(task *Task) (*Result, error) {
|
|||
_, _ = g.stdin.Write(append(taskJSON, '\n'))
|
||||
|
||||
res := new(Result)
|
||||
for {
|
||||
inp, err := checkWait(g.cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(inp, res)
|
||||
if err != nil {
|
||||
// echo anything that won't parse to stdout (this lets us add debug print statements)
|
||||
fmt.Printf("[%s] %v\n", au.BrightRed("DBG"), strings.TrimSpace(string(inp)))
|
||||
} else {
|
||||
break
|
||||
}
|
||||
if err := readJSONFromCommand(res, g.cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,27 +6,25 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
au "github.com/logrusorgru/aurora"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
nimInstallation = "nim"
|
||||
nimWrapperFilename = "runtimeWrapper.nim"
|
||||
nimInstallation = "nim"
|
||||
nimWrapperFilename = "runtimeWrapper.nim"
|
||||
nimWrapperExecutableFilename = "runtimeWrapper"
|
||||
)
|
||||
|
||||
type nimRunner struct {
|
||||
dir string
|
||||
cmd *exec.Cmd
|
||||
wrapperFilepath string
|
||||
dir string
|
||||
cmd *exec.Cmd
|
||||
wrapperFilepath string
|
||||
executableFilepath string
|
||||
stdin io.WriteCloser
|
||||
stdin io.WriteCloser
|
||||
}
|
||||
|
||||
func newNimRunner(dir string) Runner {
|
||||
|
@ -104,19 +102,8 @@ func (n *nimRunner) Run(task *Task) (*Result, error) {
|
|||
_, _ = n.stdin.Write(append(taskJSON, '\n'))
|
||||
|
||||
res := new(Result)
|
||||
for {
|
||||
inp, err := checkWait(n.cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(inp, res)
|
||||
if err != nil {
|
||||
// echo anything that won't parse to stdout (this lets us add debug print statements)
|
||||
fmt.Printf("[%s] %v\n", au.BrightRed("DBG"), strings.TrimSpace(string(inp)))
|
||||
} else {
|
||||
break
|
||||
}
|
||||
if err := readJSONFromCommand(res, n.cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@ package runners
|
|||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
au "github.com/logrusorgru/aurora"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -94,19 +92,8 @@ func (p *pythonRunner) Run(task *Task) (*Result, error) {
|
|||
_, _ = p.stdin.Write(append(taskJSON, '\n'))
|
||||
|
||||
res := new(Result)
|
||||
for {
|
||||
inp, err := checkWait(p.cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(inp, res)
|
||||
if err != nil {
|
||||
// echo anything that won't parse to stdout (this lets us add debug print statements)
|
||||
fmt.Printf("[%s] %v\n", au.BrightRed("DBG"), strings.TrimSpace(string(inp)))
|
||||
} else {
|
||||
break
|
||||
}
|
||||
if err := readJSONFromCommand(res, p.cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue