feat: save multi instance klipper names/identifier to kiauh.ini

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
th33xitus
2022-07-20 22:33:33 +02:00
parent 0bdf61a714
commit 8bbe2f79ea

View File

@@ -138,6 +138,7 @@ function init_ini() {
echo -e "# DO NOT edit this file! #"
echo -e "#=================================================#"
echo -e "# KIAUH v4.0.0"
echo -e "#"
} >> "${INI_FILE}"
fi
@@ -175,7 +176,20 @@ function init_ini() {
echo -e "\nfluidd_install_unstable=false\c" >> "${INI_FILE}"
fi
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}"
}
function change_klipper_cfg_folder() {
@@ -668,4 +682,55 @@ function get_instance_name() {
local instance=${1} name
name=$(echo "${instance}" | rev | cut -d"/" -f1 | rev | cut -d"-" -f2 | cut -d"." -f1)
echo "${name}"
}
}
###
# returns the instance name/identifier of the klipper service
# if the klipper service is part of a multi instance setup
# otherwise returns an emtpy string
#
# @param {string}: name - klipper service name (e.g. klipper-name.service)
#
function get_klipper_instance_name() {
local instance=${1}
local name
name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | rev)
local regex="^klipper-[0-9a-zA-Z]+$"
if [[ ${name} =~ ${regex} ]]; then
name=$(echo "${name}" | cut -d"-" -f2)
else
name=""
fi
echo "${name}"
}
###
# combines and saves each instance name/identifier
# to the kiauh.ini file in a comma separated format
#
function add_to_multi_instance_names() {
read_kiauh_ini "${FUNCNAME[0]}"
local name="${1}"
local names="${multi_instance_names}"
if ! grep -Eq "${name}" <<< "${names}"; then
names="${names}${name},"
sed -i "/multi_instance_names=/s/=.*/=${names}/" "${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}"
done
}