Fix set -e bug causing silent exit after valid input
- Replace all `[[ condition ]] && die` with `if/fi` — the && pattern exits silently when the condition is false under set -e - Removed -e from set flags (kept -uo pipefail), all error paths are now explicit - Declare `input` as local in ask/ask_yn/ask_choice to prevent leakage - Use `read -r input || true` to handle EOF safely - Fix ask_choice arithmetic to avoid (()) triggering exit on false Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
452a603cdf
commit
431e5493fc
3 changed files with 24 additions and 22 deletions
|
|
@ -41,7 +41,7 @@ ensure_git() {
|
|||
fi
|
||||
warn "git not found."
|
||||
ask_yn _install "Install git now?" "y"
|
||||
[[ "$_install" != "y" ]] && die "git is required."
|
||||
if [[ "$_install" != "y" ]]; then die "git is required."; fi
|
||||
install_pkg git
|
||||
command -v git &>/dev/null || die "git installation failed."
|
||||
success "git installed: $(git --version)"
|
||||
|
|
@ -69,7 +69,8 @@ ensure_container_runtime() {
|
|||
"podman (recommended)" \
|
||||
"docker"
|
||||
|
||||
case "$_runtime" in
|
||||
local runtime_choice="$_runtime"
|
||||
case "$runtime_choice" in
|
||||
podman*)
|
||||
_install_podman
|
||||
CONTAINER_CMD="podman"
|
||||
|
|
|
|||
19
lib/utils.sh
19
lib/utils.sh
|
|
@ -18,26 +18,27 @@ header() { echo -e "\n${BOLD}${CYAN}━━━ $* ━━━${RESET}\n"; }
|
|||
# ask VAR "prompt" "default"
|
||||
ask() {
|
||||
local var="$1" prompt="$2" default="$3"
|
||||
local input
|
||||
if [[ -n "$default" ]]; then
|
||||
echo -ne "${BOLD}${prompt}${RESET} ${CYAN}[${default}]${RESET}: "
|
||||
else
|
||||
echo -ne "${BOLD}${prompt}${RESET}: "
|
||||
fi
|
||||
read -r input
|
||||
read -r input || true
|
||||
if [[ -z "$input" && -n "$default" ]]; then
|
||||
eval "$var=\"$default\""
|
||||
eval "$var=\"\$default\""
|
||||
else
|
||||
eval "$var=\"$input\""
|
||||
eval "$var=\"\$input\""
|
||||
fi
|
||||
}
|
||||
|
||||
# ask_yn VAR "prompt" "y|n"
|
||||
ask_yn() {
|
||||
local var="$1" prompt="$2" default="$3"
|
||||
local options
|
||||
local input options
|
||||
if [[ "$default" == "y" ]]; then options="Y/n"; else options="y/N"; fi
|
||||
echo -ne "${BOLD}${prompt}${RESET} ${CYAN}[${options}]${RESET}: "
|
||||
read -r input
|
||||
read -r input || true
|
||||
input="${input:-$default}"
|
||||
if [[ "$input" =~ ^[Yy]$ ]]; then
|
||||
eval "$var=y"
|
||||
|
|
@ -50,18 +51,20 @@ ask_yn() {
|
|||
ask_choice() {
|
||||
local var="$1" prompt="$2"; shift 2
|
||||
local options=("$@")
|
||||
local input idx
|
||||
echo -e "${BOLD}${prompt}${RESET}"
|
||||
for i in "${!options[@]}"; do
|
||||
echo -e " ${CYAN}$((i+1))${RESET}) ${options[$i]}"
|
||||
done
|
||||
echo -ne "Choice ${CYAN}[1]${RESET}: "
|
||||
read -r input
|
||||
read -r input || true
|
||||
input="${input:-1}"
|
||||
if [[ "$input" =~ ^[0-9]+$ ]] && (( input >= 1 && input <= ${#options[@]} )); then
|
||||
eval "$var=\"${options[$((input-1))]}\""
|
||||
idx=$(( input - 1 ))
|
||||
else
|
||||
eval "$var=\"${options[0]}\""
|
||||
idx=0
|
||||
fi
|
||||
eval "$var=\"\${options[$idx]}\""
|
||||
}
|
||||
|
||||
require_cmd() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue