refactor: factor out and refactor helper functions

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
th33xitus
2022-08-14 15:16:17 +02:00
parent 8d284179d8
commit 7a7538bfbe
2 changed files with 29 additions and 27 deletions

View File

@@ -36,6 +36,12 @@ function install_menu() {
clear && print_header clear && print_header
install_ui 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 local action
while true; do while true; do
read -p "${cyan}####### Perform action:${white} " action read -p "${cyan}####### Perform action:${white} " action

View File

@@ -178,16 +178,8 @@ function init_ini() {
if ! grep -Eq "^multi_instance_names=" "${INI_FILE}"; then if ! grep -Eq "^multi_instance_names=" "${INI_FILE}"; then
echo -e "\nmulti_instance_names=\c" >> "${INI_FILE}" echo -e "\nmulti_instance_names=\c" >> "${INI_FILE}"
else
sed -i "/multi_instance_names=/s/=.*/=/" "${INI_FILE}"
fi 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 ### strip all empty lines out of the file
sed -i "/^[[:blank:]]*$/ d" "${INI_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]}" read_kiauh_ini "${FUNCNAME[0]}"
local name="${1}" local name
local names="${multi_instance_names}" local names=""
local services=$(klipper_systemd)
if ! grep -Eq "${name}" <<< "${names}"; then ###
names="${names}${name}," # if value of 'multi_instance_names' is not an empty
sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" # 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 fi
}
### for svc in ${services}; do
# loops through all installed klipper services and name=$(get_klipper_instance_name "${svc}")
# calls the 'add_to_multi_instance_names' on each one
# if ! grep -Eq "${name}" <<<"${names}"; then
function fetch_multi_instance_names() { names="${names}${name},"
for service in $(klipper_systemd); do fi
local name
name=$(get_klipper_instance_name "${service}")
add_to_multi_instance_names "${name}"
done 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 # helper function that returns all possibly available absolute
# klipper config directory paths based on their instance name. # klipper config directory paths based on their instance name.
# #
# => returns an empty string if klipper is not installed # => return an empty string if klipper is not installed
# => returns a space separated string of absolute config directory paths # => return space-separated string of absolute config directory paths
# #
function get_config_folders() { function get_config_folders() {
local cfg_dirs=() local cfg_dirs=()