[IMP] factor out user confirmation prompts into reusable function

Add confirm_or_exit() function to lib/common.sh to eliminate duplicated
confirmation dialog code in prepare_db.sh.

Before: Two 10-line case statements with identical logic
After: Two single-line function calls

The function provides consistent behavior:
- Displays the question with Y/N options
- Returns 0 on Y/y (continue execution)
- Exits with error on any other input

This follows DRY principle and ensures all confirmation prompts
behave identically across the codebase.
This commit is contained in:
Stéphan Sainléger
2026-02-02 21:59:47 +01:00
parent 60d25124c4
commit f07a654c22
2 changed files with 21 additions and 36 deletions

View File

@@ -28,6 +28,21 @@ log_warn() { printf "[WARN] %s\n" "$*" >&2; }
log_error() { printf "[ERROR] %s\n" "$*" >&2; } log_error() { printf "[ERROR] %s\n" "$*" >&2; }
log_step() { printf "\n===== %s =====\n" "$*"; } log_step() { printf "\n===== %s =====\n" "$*"; }
confirm_or_exit() {
local message="$1"
local choice
echo ""
echo "$message"
echo "Y - Yes, continue"
echo "N - No, cancel"
read -r -n 1 -p "Your choice: " choice
echo ""
case "$choice" in
[Yy]) return 0 ;;
*) log_error "Cancelled by user."; exit 1 ;;
esac
}
query_postgres_container() { query_postgres_container() {
local query="$1" local query="$1"
local db_name="$2" local db_name="$2"
@@ -76,6 +91,6 @@ exec_python_script_in_odoo_shell() {
} }
export DATASTORE_PATH FILESTORE_SUBPATH export DATASTORE_PATH FILESTORE_SUBPATH
export -f log_info log_warn log_error log_step export -f log_info log_warn log_error log_step confirm_or_exit
export -f check_required_commands export -f check_required_commands
export -f query_postgres_container copy_database copy_filestore exec_python_script_in_odoo_shell export -f query_postgres_container copy_database copy_filestore exec_python_script_in_odoo_shell

View File

@@ -59,45 +59,15 @@ EOF
echo "Retrieve missing addons..." echo "Retrieve missing addons..."
missing_addons=$(query_postgres_container "$SQL_MISSING_ADDONS" "$DB_NAME") missing_addons=$(query_postgres_container "$SQL_MISSING_ADDONS" "$DB_NAME")
echo " log_step "ADD-ONS CHECK"
==== ADD-ONS CHECK ==== echo "Installed add-ons not available in final Odoo version:"
Installed add-ons not available in final Odoo version:
"
echo "$missing_addons" echo "$missing_addons"
confirm_or_exit "Do you accept to migrate with these add-ons still installed?"
echo "
Do you accept to migrate the database with all these add-ons still installed? (Y/N/R)"
echo "Y - Yes, let's go on with the upgrade."
echo "N - No, stop the upgrade"
read -n 1 -p "Your choice: " choice
case "$choice" in
[Yy] ) echo "
Let's go on!";;
[Nn] ) echo "
Upgrade cancelled!"; exit 1;;
* ) echo "
Please answer by Y or N.";;
esac
# Check the views
PYTHON_SCRIPT=pre_migration_view_checking.py PYTHON_SCRIPT=pre_migration_view_checking.py
echo "Check views with script $PYTHON_SCRIPT ..." echo "Check views with script $PYTHON_SCRIPT ..."
exec_python_script_in_odoo_shell "$DB_NAME" "$DB_NAME" "$PYTHON_SCRIPT" || exit 1 exec_python_script_in_odoo_shell "$DB_NAME" "$DB_NAME" "$PYTHON_SCRIPT"
echo " confirm_or_exit "Do you accept to migrate with the current views state?"
Do you accept to migrate the database with the current views states? (Y/N/R)"
echo "Y - Yes, let's go on with the upgrade."
echo "N - No, stop the upgrade"
read -n 1 -p "Your choice: " choice
case "$choice" in
[Yy] ) echo "
Upgrade confirmed!";;
[Nn] ) echo "
Upgrade cancelled!"; exit 1;;
* ) echo "
Please answer by Y or N.";;
esac
echo "Database successfully prepared!" echo "Database successfully prepared!"