@@ -3,31 +3,89 @@ set -euo pipefail
3
3
4
4
cd " $( dirname " $0 " ) "
5
5
sandboxes_dir=" sandboxes"
6
+ codapi_url=" ${CODAPI_URL:- localhost: 1313} "
6
7
7
8
main () {
8
- if [[ " $# " -eq 1 && (" $1 " == " -h" || " $1 " == " --help" ) ]]; then
9
+ # Check the prerequisites.
10
+ if [[ " $# " -lt 1 || " $1 " == " -h" || " $1 " == " --help" ]]; then
9
11
do_help
10
12
exit 0
11
13
fi
12
- if [[ " $# " -lt 2 ]]; then
13
- echo " Usage: $0 <resource> <command> [args...]"
14
- exit 1
15
- fi
16
14
17
- local resource= " $1 " ; shift
15
+ # Route according to the command.
18
16
local command=" $1 " ; shift
19
-
20
- case " $resource " in
17
+ case " $command " in
18
+ " exec" )
19
+ do_exec " $@ "
20
+ ;;
21
+ " help" )
22
+ do_help " $@ "
23
+ ;;
21
24
" sandbox" )
22
- do_sandbox " $command " " $ @"
25
+ do_sandbox " $@ "
23
26
;;
24
27
* )
25
- echo " Unknown resource : $resource "
28
+ echo " Unknown command : $command "
26
29
exit 1
27
30
;;
28
31
esac
29
32
}
30
33
34
+ do_exec () {
35
+ # Command: codapi-cli exec <sandbox> <command> <filename>
36
+
37
+ # Check the prerequisites.
38
+ if [[ " $# " -ne 3 ]]; then
39
+ echo " Usage: $0 code <sandbox> <command> <filename>"
40
+ exit 1
41
+ fi
42
+ if ! command -v curl > /dev/null 2>&1 ; then
43
+ echo " ERROR: 'curl' is not installed. Install it and try again."
44
+ exit 1
45
+ fi
46
+ if ! command -v jq > /dev/null 2>&1 ; then
47
+ echo " ERROR: 'jq' is not installed. Install it and try again."
48
+ exit 1
49
+ fi
50
+
51
+ # Read the arguments.
52
+ local sandbox=" $1 "
53
+ local command=" $2 "
54
+ local filename=" $3 "
55
+ if [[ ! -f " $filename " ]]; then
56
+ echo " ERROR: File not found: $filename "
57
+ exit 1
58
+ fi
59
+ local contents
60
+ contents=$( < " $filename " )
61
+
62
+ # Prepare and send the exec request.
63
+ local json_payload
64
+ json_payload=$( jq -n --arg s " $sandbox " --arg c " $command " --arg f " $contents " ' {sandbox: $s, command: $c, files: {"": $f}}' )
65
+ local response http_code
66
+ response=$( mktemp)
67
+ http_code=$( curl --silent --show-error --json " $json_payload " --output " $response " --write-out ' %{http_code}' " $codapi_url /v1/exec" )
68
+
69
+ # Check the HTTP status.
70
+ if [[ " $http_code " != " 200" ]]; then
71
+ cat " $response "
72
+ rm -f " $response "
73
+ exit 1
74
+ fi
75
+
76
+ # Display the response.
77
+ local stdout stderr
78
+ stdout=$( cat " $response " | jq -r ' .stdout // empty' )
79
+ stderr=$( cat " $response " | jq -r ' .stderr // empty' )
80
+ rm -f " $response "
81
+ if [[ -n " $stdout " ]]; then
82
+ echo " $stdout "
83
+ fi
84
+ if [[ -n " $stderr " ]]; then
85
+ echo " $stderr "
86
+ fi
87
+ }
88
+
31
89
do_sandbox () {
32
90
local command=" $1 " ; shift
33
91
mkdir -p " $sandboxes_dir "
@@ -199,15 +257,13 @@ do_sandbox_ls() {
199
257
}
200
258
201
259
do_help () {
202
- echo " Usage: $0 <resource> <command> [args...]"
203
- echo " "
204
- echo " Resources:"
205
- echo " sandbox Manage sandboxes"
260
+ echo " Usage: $0 <command> [args...]"
206
261
echo " "
207
- echo " sandbox commands:"
208
- echo " add Add a new sandbox"
209
- echo " rm Remove an existing sandbox"
210
- echo " ls List all sandboxes"
262
+ echo " commands:"
263
+ echo " exec Execute code in a sandbox"
264
+ echo " sandbox add Add a new sandbox"
265
+ echo " sandbox rm Remove an existing sandbox"
266
+ echo " sandbox ls List all sandboxes"
211
267
}
212
268
213
269
main " $@ "
0 commit comments