Skip to content

Commit 0ffb64d

Browse files
committed
cli: codapi client tool
1 parent 22f8612 commit 0ffb64d

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ builds:
1212
archives:
1313
- files:
1414
- sandboxes/*
15+
- codapi-cli
1516
- codapi.json
1617
- codapi.service
1718
- LICENSE

codapi-cli

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
sandboxes_dir="sandboxes"
5+
6+
main() {
7+
if [ "$#" -lt 2 ]; then
8+
do_help
9+
exit 1
10+
fi
11+
12+
resource="$1"
13+
command="$2"
14+
args="${@:3}"
15+
16+
case "$resource" in
17+
"sandbox")
18+
do_sandbox $command "${args[@]}"
19+
;;
20+
*)
21+
echo "Unknown resource: $resource"
22+
exit 1
23+
;;
24+
esac
25+
}
26+
27+
do_sandbox() {
28+
local command="$1"
29+
local args="${@:2}"
30+
mkdir -p "$sandboxes_dir"
31+
case "$command" in
32+
"add")
33+
do_sandbox_add "${args[@]}"
34+
;;
35+
"rm")
36+
do_sandbox_rm "${args[@]}"
37+
;;
38+
"ls")
39+
do_sandbox_ls "${args[@]}"
40+
;;
41+
*)
42+
echo "Unknown command: $command"
43+
exit 1
44+
;;
45+
esac
46+
}
47+
48+
do_sandbox_add() {
49+
# Command: codapi-cli sandbox add <path>
50+
51+
local path="$1"
52+
if [ -z "$path" ]; then
53+
echo "Usage: $0 sandbox add <path>"
54+
exit 1
55+
fi
56+
57+
# 1. Set the name of the sandbox.
58+
local filename=$(basename "$path")
59+
local archive_path="$sandboxes_dir/$filename"
60+
local name
61+
if [[ "$filename" == *.tar.gz ]]; then
62+
name="${filename%.tar.gz}"
63+
else
64+
echo "ERROR: Archive name must end with .tar.gz"
65+
rm -f "$archive_path"
66+
exit 1
67+
fi
68+
69+
# 2. Check if the sandbox already exists.
70+
local target_dir="$sandboxes_dir/$name"
71+
if [ -d "$target_dir" ]; then
72+
echo "ERROR: Sandbox '$name' already exists"
73+
echo "Remove it with 'codapi-cli sandbox rm $name' and try again"
74+
rm -f "$archive_path"
75+
exit 1
76+
fi
77+
78+
# 3. Get the sandbox archive.
79+
if [[ "$path" == http://* || "$path" == https://* ]]; then
80+
echo "Downloading from $path..."
81+
if ! curl --location --progress-bar --output "$sandboxes_dir/$filename" "$path"; then
82+
echo "ERROR: Failed to download sandbox archive from $path"
83+
exit 1
84+
fi
85+
else
86+
echo "Copying local file $path..."
87+
if [ ! -f "$path" ]; then
88+
echo "ERROR: File not found at $path"
89+
exit 1
90+
fi
91+
if ! cp "$path" "$sandboxes_dir/"; then
92+
echo "ERROR: Failed to copy sandbox archive from $path"
93+
exit 1
94+
fi
95+
fi
96+
97+
# 4. Extract the archive.
98+
echo "Extracting $archive_path to $target_dir..."
99+
mkdir -p "$target_dir"
100+
if ! tar -xzf "$archive_path" -C "$sandboxes_dir"; then
101+
echo "ERROR: Failed to extract sandbox archive $archive_path"
102+
rm -rf "$target_dir"
103+
rm -f "$archive_path"
104+
exit 1
105+
fi
106+
rm -f "$archive_path"
107+
108+
# 5. Run build.sh if it exists.
109+
local build_script="$target_dir/build.sh"
110+
if [ -f "$build_script" ]; then
111+
echo "Running build script: $build_script"
112+
if [ ! -x "$build_script" ]; then
113+
echo "Warning: $build_script is not executable, fixing..."
114+
chmod +x "$build_script"
115+
fi
116+
# Execute the script from within its directory
117+
(cd "$target_dir" && ./build.sh)
118+
if [ $? -ne 0 ]; then
119+
echo "ERROR: build.sh failed for sandbox '$name'"
120+
exit 1
121+
fi
122+
fi
123+
124+
# 6. Run setup.sh if it exists.
125+
local setup_script="$target_dir/setup.sh"
126+
if [ -f "$setup_script" ]; then
127+
echo "Running setup script: $setup_script"
128+
if [ ! -x "$setup_script" ]; then
129+
echo "Warning: $setup_script is not executable, fixing..."
130+
chmod +x "$setup_script"
131+
fi
132+
# Execute the script from within its directory
133+
(cd "$target_dir" && ./setup.sh)
134+
if [ $? -ne 0 ]; then
135+
echo "ERROR: setup.sh failed for sandbox '$name'"
136+
exit 1
137+
fi
138+
fi
139+
140+
# 7. Display success message.
141+
echo "✓ Successfully added sandbox '$name' ($target_dir)"
142+
}
143+
144+
do_sandbox_rm() {
145+
# Command: codapi-cli sandbox rm <name>
146+
147+
local name="$1"
148+
if [ -z "$name" ]; then
149+
echo "Usage: $0 sandbox rm <name>"
150+
exit 1
151+
fi
152+
153+
local target_dir="$sandboxes_dir/$name"
154+
if [ ! -d "$target_dir" ]; then
155+
echo "ERROR: Sandbox '$name' does not exist"
156+
exit 1
157+
fi
158+
159+
# 1. Stop the container if it's running.
160+
if docker ps -q --filter "name=$name" | grep -q .; then
161+
echo "Stopping container '$name'..."
162+
docker stop --time=3 "$name"
163+
fi
164+
165+
# 2. Remove the container if it exists.
166+
if docker ps -a -q --filter "name=$name" | grep -q .; then
167+
echo "Removing container '$name'..."
168+
docker rm "$name"
169+
fi
170+
171+
# 3. Remove the directory and its contents.
172+
echo "Removing sandbox '$name' from $sandboxes_dir..."
173+
rm -rf "$target_dir"
174+
echo "✓ Successfully removed sandbox '$name'"
175+
}
176+
177+
do_sandbox_ls() {
178+
# Command: codapi-cli sandbox ls
179+
180+
count=0
181+
echo "Available sandboxes:"
182+
for dir in "$sandboxes_dir"/*; do
183+
if [ -d "$dir" ]; then
184+
local name=$(basename "$dir")
185+
echo " - $name ($dir)"
186+
count=$((count + 1))
187+
fi
188+
done
189+
if [ $count -eq 0 ]; then
190+
echo "(none)"
191+
else
192+
echo "($count total)"
193+
fi
194+
}
195+
196+
do_help() {
197+
echo "Usage: $0 <resource> <command> [args...]"
198+
echo ""
199+
echo "Resources:"
200+
echo " sandbox Manage sandboxes"
201+
echo ""
202+
echo "sandbox commands:"
203+
echo " add Add a new sandbox"
204+
echo " rm Remove an existing sandbox"
205+
echo " ls List all sandboxes"
206+
}
207+
208+
main "$@"

0 commit comments

Comments
 (0)