Fix and improve Golang runner
This commit is contained in:
parent
0c40717eab
commit
46da5b8076
3 changed files with 22 additions and 8 deletions
|
@ -118,9 +118,12 @@ func readResultsFromCommand(cmd *exec.Cmd, cleanupFn func()) chan ResultOrError
|
|||
break readerLoop
|
||||
default:
|
||||
}
|
||||
|
||||
}
|
||||
close(c)
|
||||
|
||||
_ = cmd.Process.Kill()
|
||||
|
||||
if cleanupFn != nil {
|
||||
cleanupFn()
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ func newGolangRunner(dir string) Runner {
|
|||
}
|
||||
|
||||
func (g *golangRunner) Queue(task *Task) {
|
||||
|
||||
g.tasks = append(g.tasks, task)
|
||||
}
|
||||
|
||||
//go:embed interface/go.go
|
||||
|
@ -55,13 +55,14 @@ func (g *golangRunner) Run() chan ResultOrError {
|
|||
var wrapperContent []byte
|
||||
{
|
||||
tpl := template.Must(template.New("").Parse(string(golangInterface)))
|
||||
b := bytes.NewBuffer(wrapperContent)
|
||||
b := new(bytes.Buffer)
|
||||
err := tpl.Execute(b, struct {
|
||||
ImportPath string
|
||||
}{importPath})
|
||||
if err != nil {
|
||||
return makeErrorChan(err)
|
||||
}
|
||||
wrapperContent = b.Bytes()
|
||||
}
|
||||
|
||||
// save interaction code
|
||||
|
@ -71,18 +72,26 @@ func (g *golangRunner) Run() chan ResultOrError {
|
|||
}
|
||||
|
||||
// compile executable
|
||||
cmd := exec.Command(golangInstallation, "build", "-O", wrapperExecutableFilepath, buildPath)
|
||||
stderrBuffer := new(bytes.Buffer)
|
||||
|
||||
cmd := exec.Command(golangInstallation, "build", "-tags", "runtime", "-o", wrapperExecutableFilepath, buildPath)
|
||||
cmd.Stderr = stderrBuffer
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return makeErrorChan(err)
|
||||
return makeErrorChan(fmt.Errorf("compilation failed: %s: %s", err, stderrBuffer.String()))
|
||||
}
|
||||
|
||||
if !cmd.ProcessState.Success() {
|
||||
return makeErrorChan(errors.New("compilation failed, hence cannot continue"))
|
||||
}
|
||||
|
||||
absExecPath, err := filepath.Abs(wrapperExecutableFilepath)
|
||||
if err != nil {
|
||||
return makeErrorChan(err)
|
||||
}
|
||||
|
||||
// run executable
|
||||
cmd = exec.Command(wrapperExecutableFilepath)
|
||||
cmd = exec.Command(absExecPath)
|
||||
cmd.Dir = g.dir
|
||||
|
||||
cmd.Stdin = bytes.NewReader(append(taskJSON, '\n'))
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//+build runtime
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -44,15 +46,15 @@ func run() error {
|
|||
switch task.Part {
|
||||
case runners.PartOne:
|
||||
run = func() (interface{}, error) {
|
||||
return chcode.Challenge{}.One(task.Input)
|
||||
return (&chcode.Challenge{}).One(task.Input)
|
||||
}
|
||||
case runners.PartTwo:
|
||||
run = func() (interface{}, error) {
|
||||
return chcode.Challenge{}.Two(task.Input)
|
||||
return (&chcode.Challenge{}).Two(task.Input)
|
||||
}
|
||||
case runners.Visualise:
|
||||
run = func() (interface{}, error) {
|
||||
return chcode.Challenge{}.Vis(task.Input, task.OutputDir)
|
||||
return "", (&chcode.Challenge{}).Vis(task.Input, task.OutputDir)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue