From 7a7538bfbec3c9f482772d78f36fb25f58f2c529 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sun, 14 Aug 2022 15:16:17 +0200 Subject: [PATCH] refactor: factor out and refactor helper functions Signed-off-by: Dominik Willner --- scripts/ui/install_menu.sh | 6 +++++ scripts/utilities.sh | 50 ++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 04f56c6..9f60e7f 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -36,6 +36,12 @@ function install_menu() { clear && print_header install_ui + ### save all installed webinterface ports to the ini file + fetch_webui_ports + + ### save all klipper multi-instance names to the ini file + set_multi_instance_names + local action while true; do read -p "${cyan}####### Perform action:${white} " action diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 301cef0..1a08646 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -178,16 +178,8 @@ function init_ini() { if ! grep -Eq "^multi_instance_names=" "${INI_FILE}"; then echo -e "\nmulti_instance_names=\c" >> "${INI_FILE}" - else - sed -i "/multi_instance_names=/s/=.*/=/" "${INI_FILE}" fi - ### save all installed webinterface ports to the ini file - fetch_webui_ports - - ### save all klipper multi-instance names to the ini file - fetch_multi_instance_names - ### strip all empty lines out of the file sed -i "/^[[:blank:]]*$/ d" "${INI_FILE}" } @@ -712,30 +704,34 @@ function get_klipper_instance_name() { } ### -# save all instance names in a comma separated format to the kiauh.ini +# loops through all installed klipper services and saves +# each instances name in a comma separated format to the kiauh.ini # -function add_to_multi_instance_names() { +function set_multi_instance_names() { read_kiauh_ini "${FUNCNAME[0]}" - local name="${1}" - local names="${multi_instance_names}" + local name + local names="" + local services=$(klipper_systemd) - if ! grep -Eq "${name}" <<< "${names}"; then - names="${names}${name}," - sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" + ### + # if value of 'multi_instance_names' is not an empty + # string, delete its value, so it can be re-written + if [[ -n ${multi_instance_names} ]]; then + sed -i "/multi_instance_names=/s/=.*/=/" "${INI_FILE}" fi -} -### -# loops through all installed klipper services and -# calls the 'add_to_multi_instance_names' on each one -# -function fetch_multi_instance_names() { - for service in $(klipper_systemd); do - local name - name=$(get_klipper_instance_name "${service}") - add_to_multi_instance_names "${name}" + for svc in ${services}; do + name=$(get_klipper_instance_name "${svc}") + + if ! grep -Eq "${name}" <<<"${names}"; then + names="${names}${name}," + fi + done + + # write up-to-date instance name string to kiauh.ini + sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" } ### @@ -762,8 +758,8 @@ function get_multi_instance_names() { # helper function that returns all possibly available absolute # klipper config directory paths based on their instance name. # -# => returns an empty string if klipper is not installed -# => returns a space separated string of absolute config directory paths +# => return an empty string if klipper is not installed +# => return space-separated string of absolute config directory paths # function get_config_folders() { local cfg_dirs=()