Refactor menu command handling for direct execution (#22791)

This commit is contained in:
Yehonal
2025-09-04 11:47:44 +02:00
committed by GitHub
parent 300ed47bc4
commit c9e1782d7a
5 changed files with 114 additions and 55 deletions

View File

@@ -80,12 +80,30 @@ jobs:
# Configure dashboard
sed -i 's/MTHREADS=.*/MTHREADS="4"/' conf/config.sh
- name: Test module commands
run: |
./acore.sh module install mod-autobalance
./acore.sh module install mod-duel-reset
./acore.sh module list
./acore.sh module install --all
./acore.sh module update mod-autobalance
./acore.sh module update --all
- name: Run complete installation (deps, compile, database, client-data)
run: |
# This runs: install-deps, compile, database setup, client-data download
./acore.sh init
timeout-minutes: 120
- name: Test module removal
run: |
./acore.sh module remove mod-autobalance
./acore.sh module list
./acore.sh module remove mod-duel-reset
./acore.sh module list
- name: Test authserver dry-run
run: |
cd env/dist/bin

View File

@@ -102,13 +102,6 @@ function menu_direct_execute() {
local user_input="$1"
shift
# Handle help requests directly
if [[ "$user_input" == "--help" || "$user_input" == "help" || "$user_input" == "-h" ]]; then
echo "Available commands:"
printf '%s\n' "${_MENU_OPTIONS[@]}"
return 0
fi
# Disable numeric selection in direct mode
if [[ "$user_input" =~ ^[0-9]+$ ]]; then
echo "Invalid option. Numeric selection is not allowed when passing arguments."
@@ -118,11 +111,25 @@ function menu_direct_execute() {
# Find command and execute
local idx
# try-catch
{
idx=$(menu_find_index "$user_input")
} ||
{
idx=-1
}
if [[ $idx -ge 0 ]]; then
"$callback" "${_MENU_KEYS[$idx]}" "$@"
return $?
else
# Handle help requests directly
if [[ "$user_input" == "--help" || "$user_input" == "help" || "$user_input" == "-h" ]]; then
echo "Available commands:"
printf '%s\n' "${_MENU_OPTIONS[@]}"
return 0
fi
echo "Invalid option. Use --help to see available commands." >&2
return 1
fi
@@ -138,6 +145,24 @@ function menu_interactive() {
menu_display "$title"
read -r -p "Please enter your choice: " REPLY
# Parse input to separate command from arguments
local input_parts=()
read -r -a input_parts <<< "$REPLY"
local user_command="${input_parts[0]}"
local user_args=("${input_parts[@]:1}")
# Find and execute command
local idx
idx=$(menu_find_index "$user_command")
if [[ $idx -ge 0 ]]; then
# Pass the command key and any additional arguments
"$callback" "${_MENU_KEYS[$idx]}" "${user_args[@]}"
local exit_code=$?
# Exit loop if callback returns 0 (e.g., quit command)
if [[ $exit_code -eq 0 && "${_MENU_KEYS[$idx]}" == "quit" ]]; then
break
fi
else
# Handle help request
if [[ "$REPLY" == "--help" || "$REPLY" == "help" || "$REPLY" == "h" ]]; then
echo "Available commands:"
@@ -146,12 +171,6 @@ function menu_interactive() {
continue
fi
# Find and execute command
local idx
idx=$(menu_find_index "$REPLY")
if [[ $idx -ge 0 ]]; then
"$callback" "${_MENU_KEYS[$idx]}"
else
echo "Invalid option. Please try again or use 'help' for available commands." >&2
echo ""
fi
@@ -159,35 +178,65 @@ function menu_interactive() {
}
# Main menu runner function
# Usage: menu_run "Menu Title" callback_function menu_item1 menu_item2 ... "$@"
# Usage: menu_run "Menu Title" callback_function "$@"
# The menu items array should be defined globally before calling this function
function menu_run() {
local title="$1"
local callback="$2"
shift 2
# Extract menu items (all arguments until we find command line args)
local menu_items=()
local found_args=false
# Define menu from globally available menu items array
# This expects the calling script to have set up the menu items
# Separate menu items from command line arguments
while [[ $# -gt 0 ]]; do
if [[ "$1" =~ \| ]]; then
# This looks like a menu item (contains pipe)
menu_items+=("$1")
# Handle direct execution if arguments provided
if [[ $# -gt 0 ]]; then
menu_direct_execute "$callback" "$@"
return $?
fi
# Run interactive menu
menu_interactive "$callback" "$title"
}
# Alternative menu runner that accepts menu items directly
# Usage: menu_run_with_items "Menu Title" callback_function -- "${menu_items_array[@]}" -- "$@"
function menu_run_with_items() {
local title="$1"
local callback="$2"
shift 2
# Parse parameters: menu items are between first and second "--"
local menu_items=()
local script_args=()
# Skip first "--"
if [[ "$1" == "--" ]]; then
shift
else
# This is a command line argument
found_args=true
break
echo "Error: menu_run_with_items requires -- separator before menu items" >&2
return 1
fi
# Collect menu items until second "--"
while [[ $# -gt 0 && "$1" != "--" ]]; do
menu_items+=("$1")
shift
done
# Define menu from collected items
# Skip second "--" if present
if [[ "$1" == "--" ]]; then
shift
fi
# Remaining args are script arguments
script_args=("$@")
# Define menu from provided array
menu_define "${menu_items[@]}"
# Handle direct execution if arguments provided
if [[ $found_args == true ]]; then
menu_direct_execute "$callback" "$@"
if [[ ${#script_args[@]} -gt 0 ]]; then
menu_direct_execute "$callback" "${script_args[@]}"
return $?
fi

View File

@@ -49,7 +49,7 @@ function handle_compiler_command() {
;;
"quit")
echo "Closing compiler menu..."
exit 0
return 0
;;
*)
echo "Invalid option. Use --help to see available commands."
@@ -61,20 +61,5 @@ function handle_compiler_command() {
# Hook support (preserved from original)
runHooks "ON_AFTER_OPTIONS" # you can create your custom options
# Legacy switch function (preserved for compatibility)
function _switch() {
local reply="$1"
local opt="$2"
case "$reply" in
""|"--help")
menu_show_help
;;
*)
run_option "$reply" "$opt"
;;
esac
}
# Run the menu system
menu_run "ACORE COMPILER" handle_compiler_command "${comp_menu_items[@]}" "$@"
menu_run_with_items "ACORE COMPILER" handle_compiler_command -- "${comp_menu_items[@]}" -- "$@"

View File

@@ -25,6 +25,7 @@
CURRENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd )
source "$CURRENT_PATH/../../../bash_shared/includes.sh"
source "$CURRENT_PATH/../includes.sh"
source "$AC_PATH_APPS/bash_shared/menu_system.sh"
# Module management menu definition
@@ -65,7 +66,7 @@ function handle_module_command() {
;;
"quit")
echo "Exiting module manager..."
exit 0
return 0
;;
*)
echo "Invalid option. Use 'help' to see available commands."
@@ -129,7 +130,7 @@ function inst_module_list() {
function inst_module() {
# If no arguments provided, start interactive menu
if [[ $# -eq 0 ]]; then
menu_run "MODULE MANAGER" handle_module_command "${module_menu_items[@]}"
menu_run_with_items "MODULE MANAGER" handle_module_command -- "${module_menu_items[@]}" --
return $?
fi
@@ -727,6 +728,12 @@ function inst_module_install {
# Update one or more modules
function inst_module_update {
# Handle help request
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
inst_module_help
return 0
fi
# Support multiple modules and the --all flag; prompt if none specified.
local args=("$@")
local use_all=false

View File

@@ -104,4 +104,4 @@ function handle_menu_command() {
}
# Run the menu system
menu_run "ACORE DASHBOARD" handle_menu_command "${menu_items[@]}" "$@"
menu_run_with_items "ACORE DASHBOARD" handle_menu_command -- "${menu_items[@]}" -- "$@"