-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_code.go
More file actions
48 lines (42 loc) · 1.02 KB
/
compile_code.go
File metadata and controls
48 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package coderunner
import (
"context"
"os/exec"
"path/filepath"
"strings"
mapset "github.com/deckarep/golang-set"
"github.com/pkg/errors"
systemutils "github.com/thewizardplusplus/go-code-runner/system-utils"
)
// CompileCode ...
func CompileCode(
ctx context.Context,
pathToCode string,
allowedImports mapset.Set,
) (pathToExecutable string, err error) {
if _, err = exec.LookPath("goimports"); err == nil {
_, err = systemutils.RunCommand(ctx, "", "goimports", "-w", pathToCode)
if err != nil {
return "", errors.Wrap(err, "unable to prepare the code")
}
}
if allowedImports != nil {
if err = CheckImports(pathToCode, allowedImports); err != nil {
return "", errors.Wrap(err, "failed import checking")
}
}
pathToExecutable = strings.TrimSuffix(pathToCode, filepath.Ext(pathToCode))
_, err = systemutils.RunCommand(
ctx,
"",
"go",
"build",
"-o",
pathToExecutable,
pathToCode,
)
if err != nil {
return "", errors.Wrap(err, "unable to compile the code")
}
return pathToExecutable, nil
}