This repository was archived by the owner on Dec 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathrunner.go
More file actions
130 lines (107 loc) · 2.78 KB
/
Copy pathrunner.go
File metadata and controls
130 lines (107 loc) · 2.78 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"fmt"
"github.com/prasmussen/glot-code-runner/cmd"
"github.com/prasmussen/glot-code-runner/language"
"io/ioutil"
"os"
"path/filepath"
)
type Payload struct {
Language string `json:"language"`
Files []*InMemoryFile `json:"files"`
Stdin string `json:"stdin"`
Command string `json:"command"`
}
type InMemoryFile struct {
Name string `json:"name"`
Content string `json:"content"`
}
type Result struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
Error string `json:"error"`
}
func main() {
payload := &Payload{}
err := json.NewDecoder(os.Stdin).Decode(payload)
if err != nil {
exitF("Failed to parse input json (%s)\n", err.Error())
}
// Ensure that we have at least one file
if len(payload.Files) == 0 {
exitF("No files given\n")
}
// Check if we support given language
if !language.IsSupported(payload.Language) {
exitF("Language '%s' is not supported\n", payload.Language)
}
// Write files to disk
filepaths, err := writeFiles(payload.Files)
if err != nil {
exitF("Failed to write file to disk (%s)", err.Error())
}
var stdout, stderr string
// Execute the given command or run the code with
// the language runner if no command is given
if payload.Command == "" {
stdout, stderr, err = language.Run(payload.Language, filepaths, payload.Stdin)
} else {
workDir := filepath.Dir(filepaths[0])
stdout, stderr, err = cmd.RunBashStdin(workDir, payload.Command, payload.Stdin)
}
printResult(stdout, stderr, err)
}
// Writes files to disk, returns list of absolute filepaths
func writeFiles(files []*InMemoryFile) ([]string, error) {
// Create temp dir
tmpPath, err := ioutil.TempDir("", "")
if err != nil {
return nil, err
}
paths := make([]string, len(files), len(files))
for i, file := range files {
path, err := writeFile(tmpPath, file)
if err != nil {
return nil, err
}
paths[i] = path
}
return paths, nil
}
// Writes a single file to disk
func writeFile(basePath string, file *InMemoryFile) (string, error) {
// Get absolute path to file inside basePath
absPath := filepath.Join(basePath, file.Name)
// Create all parent dirs
err := os.MkdirAll(filepath.Dir(absPath), 0775)
if err != nil {
return "", err
}
// Write file to disk
err = ioutil.WriteFile(absPath, []byte(file.Content), 0664)
if err != nil {
return "", err
}
// Return absolute path to file
return absPath, nil
}
func exitF(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}
func printResult(stdout, stderr string, err error) {
result := &Result{
Stdout: stdout,
Stderr: stderr,
Error: errToStr(err),
}
json.NewEncoder(os.Stdout).Encode(result)
}
func errToStr(err error) string {
if err != nil {
return err.Error()
}
return ""
}