feat(workspace): offer safe sandbox cleanup
This commit is contained in:
62
build-sbx.sh
62
build-sbx.sh
@@ -201,6 +201,68 @@ main() {
|
||||
exit 1
|
||||
fi
|
||||
unset api_key
|
||||
|
||||
if ((${#sandbox_names[@]} == 0)) || ! ask_yes_no 'Do you want to delete existing Docker sbx sandboxes?'; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
printf 'Existing Docker sbx sandboxes:\n'
|
||||
local index
|
||||
for index in "${!sandbox_names[@]}"; do
|
||||
printf ' %d) %s\n' "$((index + 1))" "${sandbox_names[index]}"
|
||||
done
|
||||
|
||||
local selection selection_part range_start range_end
|
||||
local valid_selection index sandbox_name
|
||||
declare -A selected_indices=()
|
||||
while true; do
|
||||
read -r -p 'Sandboxes to delete (for example, 1-3 or 1, 2, 7; press Enter to cancel): ' selection || return 0
|
||||
[[ -z "$selection" ]] && return 0
|
||||
|
||||
selected_indices=()
|
||||
valid_selection=true
|
||||
IFS=',' read -ra selection_parts <<< "$selection"
|
||||
for selection_part in "${selection_parts[@]}"; do
|
||||
selection_part="${selection_part//[[:space:]]/}"
|
||||
if [[ "$selection_part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
|
||||
range_start=$((10#${BASH_REMATCH[1]}))
|
||||
range_end=$((10#${BASH_REMATCH[2]}))
|
||||
if ((range_start < 1 || range_start > range_end || range_end > ${#sandbox_names[@]})); then
|
||||
valid_selection=false
|
||||
break
|
||||
fi
|
||||
for ((index = range_start; index <= range_end; index++)); do
|
||||
selected_indices[$index]=1
|
||||
done
|
||||
elif [[ "$selection_part" =~ ^[0-9]+$ ]]; then
|
||||
index=$((10#$selection_part))
|
||||
if ((index < 1 || index > ${#sandbox_names[@]})); then
|
||||
valid_selection=false
|
||||
break
|
||||
fi
|
||||
selected_indices[$index]=1
|
||||
else
|
||||
valid_selection=false
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "$valid_selection" != true || ${#selected_indices[@]} -eq 0 ]]; then
|
||||
printf 'Please enter valid sandbox numbers and ranges from 1 to %d.\n' "${#sandbox_names[@]}"
|
||||
continue
|
||||
fi
|
||||
|
||||
for ((index = 1; index <= ${#sandbox_names[@]}; index++)); do
|
||||
if [[ ${selected_indices[$index]:-} == 1 ]]; then
|
||||
sandbox_name="${sandbox_names[index - 1]}"
|
||||
printf 'Deleting Docker sbx sandbox: %s\n' "$sandbox_name"
|
||||
if ! sbx rm --force "$sandbox_name"; then
|
||||
printf 'Unable to delete Docker sbx sandbox: %s\n' "$sandbox_name" >&2
|
||||
fi
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
done
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
||||
Reference in New Issue
Block a user