From 9278241dd039270c2fa99ba1db59bb1b3ddabd48 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Sun, 6 Sep 2026 21:32:42 +0200 Subject: [PATCH] Refactor: Ja/Nein-Abfrage in gemeinsame Hilfsfunktion ausgelagert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prompt_target_user und prompt_ollama_models_path implementierten die gleiche CLI-Ja/Nein-Abfrage (read + Trim + Regex-Prüfung) unabhängig voneinander. Beide Kopien waren bereits leicht auseinandergelaufen (unterschiedliches Fallback-Zeichen, unterschiedliche Regex- Reihenfolge). confirm_yes_no() bündelt die Logik an einer Stelle. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_017WMUd2fGsPCtTCVQkSS5TA --- lib/utils.sh | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index 343e275..e462eea 100755 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -135,6 +135,23 @@ run_whiptail() { fi } +# Ask a yes/no question on the CLI. Returns 0 for yes, 1 for no. +# On read failure (e.g. closed stdin), falls back to default_answer. +# Usage: confirm_yes_no "" "" "$tty_in" "$tty_out" +confirm_yes_no() { + local prompt="$1" + local default_answer="$2" + local tty_in="$3" + local tty_out="$4" + local answer="" + read -r -p "$prompt" answer < "$tty_in" > "$tty_out" 2>&1 || answer="$default_answer" + answer="$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<< "$answer")" + if [[ -z "$answer" ]]; then + answer="$default_answer" + fi + [[ "$answer" =~ ^[YyJj] ]] +} + # Determine and prompt for the non-root target user prompt_target_user() { if [[ -n "${TARGET_USER:-}" ]]; then @@ -173,9 +190,7 @@ prompt_target_user() { if [[ -n "$candidate" && "$candidate" != "root" ]]; then printf "\n%bTarget User Detection:%b\n" "${CLR_CYAN}" "${CLR_RESET}" > "$tty_out" printf "Detected target user: %b%s%b\n" "${CLR_BOLD}" "$candidate" "${CLR_RESET}" > "$tty_out" - local answer="" - read -r -p "Use target user '$candidate'? [Y/n]: " answer < "$tty_in" > "$tty_out" 2>&1 || answer="y" - if [[ -z "$answer" || "$answer" =~ ^[YyJj] ]]; then + if confirm_yes_no "Use target user '$candidate'? [Y/n]: " "y" "$tty_in" "$tty_out"; then chosen="$candidate" fi fi @@ -296,10 +311,7 @@ prompt_ollama_models_path() { if [[ -z "$chosen" ]]; then printf "\n%bOllama KI-Modelle Speicherort:%b\n" "${CLR_CYAN}" "${CLR_RESET}" > "$tty_out" printf "Standard-Pfad: %b%s%b\n" "${CLR_BOLD}" "$default_path" "${CLR_RESET}" > "$tty_out" - local use_default="" - read -r -p "Standard-Speicherort verwenden? [J/n]: " use_default < "$tty_in" > "$tty_out" 2>&1 || use_default="j" - use_default="$(echo "$use_default" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" - if [[ -z "$use_default" || "$use_default" =~ ^[JjYy] ]]; then + if confirm_yes_no "Standard-Speicherort verwenden? [J/n]: " "j" "$tty_in" "$tty_out"; then chosen="$default_path" else local input_path=""