You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.5 KiB
82 lines
1.5 KiB
#!/usr/bin/env bash
|
|
|
|
# CONSTANTS
|
|
|
|
BASEDIR=$(dirname "$0")
|
|
CONTAINERS=()
|
|
YES=false
|
|
FORCE=false
|
|
|
|
# FUNCTIONS
|
|
|
|
function usage {
|
|
echo "$(basename "$0") <CONTAINER_NAME>... [--yes|-y] [--force|-f]"
|
|
}
|
|
|
|
function flatten_short_options {
|
|
local -n result=$1
|
|
shift
|
|
result=()
|
|
for word in "$@"; do
|
|
[[ $word == -- ]] && break
|
|
if [[ $word =~ ^-[a-z][a-z] ]]; then
|
|
word=${word:1}
|
|
for ((i = 0; i < ${#word}; i++)); do result+=("-${word:i:1}"); done
|
|
else
|
|
result+=("$word")
|
|
fi
|
|
done
|
|
}
|
|
|
|
function parse_options {
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--help | -h)
|
|
usage && exit 0
|
|
;;
|
|
--yes | -y)
|
|
YES=true
|
|
;;
|
|
--force | -f)
|
|
FORCE=true
|
|
;;
|
|
-*)
|
|
echo >&2 "Error: unknown option: $1" && usage && exit 2
|
|
;;
|
|
*)
|
|
CONTAINERS+=("$1")
|
|
;;
|
|
esac
|
|
|
|
shift 1 # Move to the next argument
|
|
done
|
|
|
|
[[ ${#CONTAINERS[@]} == 0 ]] && usage && exit 1 || true
|
|
}
|
|
|
|
function confirm_destructive {
|
|
local message="${1:-This cannot be undone!}"
|
|
echo "⚠️ WARNING: $message"
|
|
read -p "Type YES to confirm: " response
|
|
case "$response" in
|
|
[yY][eE][sS] | [yY]) return 0 ;;
|
|
*) echo "canceled!" && return 1 ;;
|
|
esac
|
|
}
|
|
|
|
function force_option {
|
|
[[ $FORCE == false ]] && return
|
|
echo '--force '
|
|
}
|
|
|
|
function destroy {
|
|
[[ $YES == false ]] && confirm_destructive "you are about to destroy ${#CONTAINERS[@]} containers <${CONTAINERS[*]}>" $(force_option)
|
|
incus delete "${CONTAINERS[@]}" $(force_option)
|
|
}
|
|
|
|
# MAIN
|
|
|
|
set -Eue
|
|
flatten_short_options flat "$@"
|
|
parse_options "${flat[@]}"
|
|
destroy
|