@@ -3,31 +3,89 @@ set -euo pipefail
33
44cd " $( dirname " $0 " ) "
55sandboxes_dir=" sandboxes"
6+ codapi_url=" ${CODAPI_URL:- localhost: 1313} "
67
78main () {
8- if [[ " $# " -eq 1 && (" $1 " == " -h" || " $1 " == " --help" ) ]]; then
9+ # Check the prerequisites.
10+ if [[ " $# " -lt 1 || " $1 " == " -h" || " $1 " == " --help" ]]; then
911 do_help
1012 exit 0
1113 fi
12- if [[ " $# " -lt 2 ]]; then
13- echo " Usage: $0 <resource> <command> [args...]"
14- exit 1
15- fi
1614
17- local resource= " $1 " ; shift
15+ # Route according to the command.
1816 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+ ;;
2124 " sandbox" )
22- do_sandbox " $command " " $ @"
25+ do_sandbox " $@ "
2326 ;;
2427 * )
25- echo " Unknown resource : $resource "
28+ echo " Unknown command : $command "
2629 exit 1
2730 ;;
2831 esac
2932}
3033
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+
3189do_sandbox () {
3290 local command=" $1 " ; shift
3391 mkdir -p " $sandboxes_dir "
@@ -199,15 +257,13 @@ do_sandbox_ls() {
199257}
200258
201259do_help () {
202- echo " Usage: $0 <resource> <command> [args...]"
203- echo " "
204- echo " Resources:"
205- echo " sandbox Manage sandboxes"
260+ echo " Usage: $0 <command> [args...]"
206261 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"
211267}
212268
213269main " $@ "
0 commit comments