Extend ask() to support an optional configuration variable name.

This commit is contained in:
rmm 2026-01-11 18:57:23 +01:00
parent e1004e456f
commit ca047d3444

View File

@ -71,8 +71,13 @@ function err() {
# Prompts (cyan or bold) # Prompts (cyan or bold)
function ask() { function ask() {
local prompt="$1" local prompt="$1"
local configVarName="$2"
local response local response
read -p $'\n\e[1;36m'"${prompt}:"$'\e[0m ' response if [[ -n configVarName && -n "${!configVarName}" ]]; then
response="${!configVarName}"
else
read -p $'\n\e[1;36m'"${prompt}:"$'\e[0m ' response
fi
echo "$response" echo "$response"
} }
#endregion message tools #endregion message tools
@ -113,13 +118,9 @@ function load_settings {
function install_packages { function install_packages {
# Check if the 'sensors' command is available on the system # Check if the 'sensors' command is available on the system
if (! command -v sensors &>/dev/null); then if (! command -v sensors &>/dev/null); then
local choice=
if [ -n "$CONFIG_INSTALL_LM_SENSORS" ]; then # If the 'sensors' command is not available, prompt the user to install lm-sensors
choice = $CONFIG_INSTALL_LM_SENSORS local choice=$(ask "lm-sensors is not installed. Would you like to install it? (y/n)" CONFIG_INSTALL_LM_SENSORS)
else
# If the 'sensors' command is not available, prompt the user to install lm-sensors
choice=$(ask "lm-sensors is not installed. Would you like to install it? (y/n)")
fi
case "$choice" in case "$choice" in
[yY]) [yY])
# If the user chooses to install lm-sensors, update the package list and install the package # If the user chooses to install lm-sensors, update the package list and install the package
@ -200,13 +201,8 @@ function configure {
info "Detected CPU sensors ($cpuCount): $cpuList" info "Detected CPU sensors ($cpuCount): $cpuList"
SENSORS_DETECTED=true SENSORS_DETECTED=true
local choice=
while true; do while true; do
if [ -n "$CONFIG_CPU_TEMP_MODE" ]; then local choice=$(ask "Display temperatures for all cores [C] or average per CPU [a] (some newer AMD variants support per die)? (C/a)" CONFIG_CPU_TEMP_MODE)
choice=$CONFIG_CPU_TEMP_MODE
else
choice=$(ask "Display temperatures for all cores [C] or average per CPU [a] (some newer AMD variants support per die)? (C/a)")
fi
case "$choice" in case "$choice" in
[cC]|"") [cC]|"")
CPU_TEMP_TARGET="Core" CPU_TEMP_TARGET="Core"
@ -292,12 +288,7 @@ function configure {
ENABLE_FAN_SPEED=true ENABLE_FAN_SPEED=true
SENSORS_DETECTED=true SENSORS_DETECTED=true
local choice= local choice=$(ask "Display fans reporting zero speed? (Y/n)" CONFIG_FAN_ZERO_SPEED_DISPLAY)
if [ -n "$CONFIG_FAN_ZERO_SPEED_DISPLAY" ]; then
choice=$CONFIG_FAN_ZERO_SPEED_DISPLAY
else
choice=$(ask "Display fans reporting zero speed? (Y/n)")
fi
case "$choice" in case "$choice" in
[yY]|"") [yY]|"")
DISPLAY_ZERO_SPEED_FANS=true DISPLAY_ZERO_SPEED_FANS=true
@ -326,12 +317,7 @@ function configure {
#region temp unit setup #region temp unit setup
msgb "\n=== Display temperature ===" msgb "\n=== Display temperature ==="
if [ "$SENSORS_DETECTED" = true ]; then if [ "$SENSORS_DETECTED" = true ]; then
local unit= local unit=$(ask "Display temperatures in Celsius [C] or Fahrenheit [f]? (C/f)" CONFIG_TEMP_UNIT)
if [ -n "$CONFIG_TEMP_UNIT" ]; then
unit=$CONFIG_TEMP_UNIT
else
unit=$(ask "Display temperatures in Celsius [C] or Fahrenheit [f]? (C/f)")
fi
case "$unit" in case "$unit" in
[cC]|"") [cC]|"")
TEMP_UNIT="C" TEMP_UNIT="C"
@ -356,12 +342,7 @@ function configure {
#### UPS #### #### UPS ####
#region ups setup #region ups setup
msgb "\n=== UPS setup ===" msgb "\n=== UPS setup ==="
local choice= local choice=$(ask "Enable UPS information? (y/N)" CONFIG_UPS_SHOW_INFO)
if [ -n "$CONFIG_UPS_SHOW_INFO" ]; then
choice=$CONFIG_UPS_SHOW_INFO
else
choice=$(ask "Enable UPS information? (y/N)")
fi
case "$choice" in case "$choice" in
[yY]) [yY])
local upsConnection= local upsConnection=
@ -370,11 +351,7 @@ function configure {
info "Remote debugging: UPS readings from $DEBUG_UPS_FILE" info "Remote debugging: UPS readings from $DEBUG_UPS_FILE"
upsConnection="DEBUG_UPS" upsConnection="DEBUG_UPS"
else else
if [ -n "$CONFIG_UPS_CONNECTION" ]; then upsConnection=$(ask "Enter UPS connection (e.g., upsname[@hostname[:port]])" CONFIG_UPS_CONNECTION)
upsConnection=$CONFIG_UPS_CONNECTION
else
upsConnection=$(ask "Enter UPS connection (e.g., upsname[@hostname[:port]])")
fi
if ! command -v upsc &>/dev/null; then if ! command -v upsc &>/dev/null; then
err "The 'upsc' command is not available. Install 'nut-client'." err "The 'upsc' command is not available. Install 'nut-client'."
fi fi
@ -412,12 +389,7 @@ function configure {
echo "type ${i})" echo "type ${i})"
dmidecode -t "$i" | awk -F': ' '/Manufacturer|Product Name|Serial Number/ {print $1": "$2}' dmidecode -t "$i" | awk -F': ' '/Manufacturer|Product Name|Serial Number/ {print $1": "$2}'
done done
local choiceSysInfo= loacal choiceSysInfo=$(ask "Enable system information? (1/2/n)" CONFIG_SYSINFO_DISPLAY_MODE)
if [ -n "$CONFIG_SYSINFO_DISPLAY_MODE" ]; then
choiceSysInfo=$CONFIG_SYSINFO_DISPLAY_MODE
else
choiceSysInfo=$(ask "Enable system information? (1/2/n)")
fi
case "$choiceSysInfo" in case "$choiceSysInfo" in
[1]|"") [1]|"")
ENABLE_SYSTEM_INFO=true ENABLE_SYSTEM_INFO=true