-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcodapi-cli
executable file
·213 lines (189 loc) · 5.6 KB
/
codapi-cli
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
sandboxes_dir="sandboxes"
main() {
if [[ "$#" -eq 1 && ("$1" == "-h" || "$1" == "--help") ]]; then
do_help
exit 0
fi
if [[ "$#" -lt 2 ]]; then
echo "Usage: $0 <resource> <command> [args...]"
exit 1
fi
local resource="$1"; shift
local command="$1"; shift
case "$resource" in
"sandbox")
do_sandbox "$command" "$@"
;;
*)
echo "Unknown resource: $resource"
exit 1
;;
esac
}
do_sandbox() {
local command="$1"; shift
mkdir -p "$sandboxes_dir"
case "$command" in
"add")
do_sandbox_add "$@"
;;
"rm")
do_sandbox_rm "$@"
;;
"ls")
do_sandbox_ls "$@"
;;
*)
echo "Unknown command: $command"
exit 1
;;
esac
}
do_sandbox_add() {
# Command: codapi-cli sandbox add <path>
local path="${1:-}"
if [[ -z "$path" ]]; then
echo "Usage: $0 sandbox add <path>"
exit 1
fi
# 0. Check if the path a package name.
if [[ "$path" =~ ^[a-zA-Z0-9_-]*$ ]]; then
# This is a package name, so we need to download it from the registry.
path="https://github.com/nalgeon/sandboxes/releases/download/latest/$path.tar.gz"
fi
# 1. Set the name of the sandbox.
local filename
filename=$(basename "$path")
local archive_path="$sandboxes_dir/$filename"
local name
if [[ "$filename" == *.tar.gz ]]; then
name="${filename%.tar.gz}"
else
echo "ERROR: Archive name must end with .tar.gz"
rm -f "$archive_path"
exit 1
fi
# 2. Check if the sandbox already exists.
local target_dir="$sandboxes_dir/$name"
if [[ -d "$target_dir" ]]; then
echo "ERROR: Sandbox '$name' already exists"
echo "Remove it with 'codapi-cli sandbox rm $name' and try again"
rm -f "$archive_path"
exit 1
fi
# 3. Get the sandbox archive.
if [[ "$path" == http://* || "$path" == https://* ]]; then
echo "Downloading from $path..."
if ! curl --location --progress-bar --output "$sandboxes_dir/$filename" "$path"; then
echo "ERROR: Failed to download sandbox archive from $path"
exit 1
fi
else
echo "Copying local file $path..."
if [[ ! -f "$path" ]]; then
echo "ERROR: File not found at $path"
exit 1
fi
if ! cp "$path" "$sandboxes_dir/"; then
echo "ERROR: Failed to copy sandbox archive from $path"
exit 1
fi
fi
# 4. Extract the archive.
echo "Extracting $archive_path to $target_dir..."
mkdir -p "$target_dir"
if ! tar -xzf "$archive_path" -C "$sandboxes_dir"; then
echo "ERROR: Failed to extract sandbox archive $archive_path"
rm -rf "$target_dir"
rm -f "$archive_path"
exit 1
fi
rm -f "$archive_path"
# 5. Run build.sh if it exists.
local build_script="$target_dir/build.sh"
if [[ -f "$build_script" ]]; then
echo "Running build script: $build_script"
if [[ ! -x "$build_script" ]]; then
echo "Warning: $build_script is not executable, fixing..."
chmod +x "$build_script"
fi
# Execute the script from within its directory
(cd "$target_dir" && ./build.sh)
if [[ $? -ne 0 ]]; then
echo "ERROR: build.sh failed for sandbox '$name'"
exit 1
fi
fi
# 6. Run setup.sh if it exists.
local setup_script="$target_dir/setup.sh"
if [[ -f "$setup_script" ]]; then
echo "Running setup script: $setup_script"
if [[ ! -x "$setup_script" ]]; then
echo "Warning: $setup_script is not executable, fixing..."
chmod +x "$setup_script"
fi
# Execute the script from within its directory
(cd "$target_dir" && ./setup.sh)
if [[ $? -ne 0 ]]; then
echo "ERROR: setup.sh failed for sandbox '$name'"
exit 1
fi
fi
# 7. Display success message.
echo "✓ Successfully added sandbox '$name' ($target_dir)"
}
do_sandbox_rm() {
# Command: codapi-cli sandbox rm <name>
local name="${1:-}"
if [[ -z "$name" ]]; then
echo "Usage: $0 sandbox rm <name>"
exit 1
fi
local target_dir="$sandboxes_dir/$name"
if [[ ! -d "$target_dir" ]]; then
echo "ERROR: Sandbox '$name' does not exist"
exit 1
fi
# 1. Remove the container if it exists.
if docker ps -a -q --filter "name=$name" | grep -q .; then
echo "Removing container '$name'..."
docker rm -f "$name"
fi
# 2. Remove the directory and its contents.
echo "Removing sandbox '$name' from $sandboxes_dir..."
rm -rf "$target_dir"
echo "✓ Successfully removed sandbox '$name'"
}
do_sandbox_ls() {
# Command: codapi-cli sandbox ls
local name
local count=0
echo "Available sandboxes:"
for dir in "$sandboxes_dir"/*; do
if [[ -d "$dir" ]]; then
name=$(basename "$dir")
echo " - $name ($dir)"
count=$((count + 1))
fi
done
if [[ $count -eq 0 ]]; then
echo "(none)"
else
echo "($count total)"
fi
}
do_help() {
echo "Usage: $0 <resource> <command> [args...]"
echo ""
echo "Resources:"
echo " sandbox Manage sandboxes"
echo ""
echo "sandbox commands:"
echo " add Add a new sandbox"
echo " rm Remove an existing sandbox"
echo " ls List all sandboxes"
}
main "$@"