Refactor: Ja/Nein-Abfrage in gemeinsame Hilfsfunktion ausgelagert
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017WMUd2fGsPCtTCVQkSS5TA
This commit is contained in:
+19
-7
@@ -135,6 +135,23 @@ run_whiptail() {
|
|||||||
fi
|
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 "<prompt>" "<y|n default answer>" "$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
|
# Determine and prompt for the non-root target user
|
||||||
prompt_target_user() {
|
prompt_target_user() {
|
||||||
if [[ -n "${TARGET_USER:-}" ]]; then
|
if [[ -n "${TARGET_USER:-}" ]]; then
|
||||||
@@ -173,9 +190,7 @@ prompt_target_user() {
|
|||||||
if [[ -n "$candidate" && "$candidate" != "root" ]]; then
|
if [[ -n "$candidate" && "$candidate" != "root" ]]; then
|
||||||
printf "\n%bTarget User Detection:%b\n" "${CLR_CYAN}" "${CLR_RESET}" > "$tty_out"
|
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"
|
printf "Detected target user: %b%s%b\n" "${CLR_BOLD}" "$candidate" "${CLR_RESET}" > "$tty_out"
|
||||||
local answer=""
|
if confirm_yes_no "Use target user '$candidate'? [Y/n]: " "y" "$tty_in" "$tty_out"; then
|
||||||
read -r -p "Use target user '$candidate'? [Y/n]: " answer < "$tty_in" > "$tty_out" 2>&1 || answer="y"
|
|
||||||
if [[ -z "$answer" || "$answer" =~ ^[YyJj] ]]; then
|
|
||||||
chosen="$candidate"
|
chosen="$candidate"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -296,10 +311,7 @@ prompt_ollama_models_path() {
|
|||||||
if [[ -z "$chosen" ]]; then
|
if [[ -z "$chosen" ]]; then
|
||||||
printf "\n%bOllama KI-Modelle Speicherort:%b\n" "${CLR_CYAN}" "${CLR_RESET}" > "$tty_out"
|
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"
|
printf "Standard-Pfad: %b%s%b\n" "${CLR_BOLD}" "$default_path" "${CLR_RESET}" > "$tty_out"
|
||||||
local use_default=""
|
if confirm_yes_no "Standard-Speicherort verwenden? [J/n]: " "j" "$tty_in" "$tty_out"; then
|
||||||
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
|
|
||||||
chosen="$default_path"
|
chosen="$default_path"
|
||||||
else
|
else
|
||||||
local input_path=""
|
local input_path=""
|
||||||
|
|||||||
Reference in New Issue
Block a user