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
|
|
|
|
MIAOU_INCUS_DIR=${MIAOU_INCUS_DIR:-/opt/miaou-incus}
|
|
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 force_option {
|
|
[[ $FORCE == false ]] && return
|
|
echo '--force '
|
|
}
|
|
|
|
function destroy {
|
|
if [[ $YES == false ]]; then
|
|
local message count
|
|
count=${#CONTAINERS[@]}
|
|
message="you are about to destroy $(_pluralize_simple $count container): ${CONTAINERS[*]}"
|
|
_confirm_destructive "$message" $(force_option)
|
|
fi
|
|
incus delete "${CONTAINERS[@]}" $(force_option)
|
|
}
|
|
|
|
# MAIN
|
|
|
|
set -Eue -o pipefail
|
|
|
|
# shellcheck source=/opt/miaou-incus/lib/functions.bash
|
|
source "$MIAOU_INCUS_DIR/lib/functions.bash"
|
|
|
|
flatten_short_options flat "$@"
|
|
parse_options "${flat[@]}"
|
|
destroy
|