style(klipper.sh): tweak code style

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-05-14 18:24:21 +02:00
parent c1cc1f0d19
commit d4d9f971c6

View File

@@ -36,20 +36,22 @@ function klipper_exists() {
} }
function klipper_setup_dialog(){ function klipper_setup_dialog(){
local python_version="${1}" user_input=()
user_input+=("${python_version}")
status_msg "Initializing Klipper installation ..." status_msg "Initializing Klipper installation ..."
### return early if klipper already exists
local klipper_services local klipper_services
local python_version="${1}" user_input=()
klipper_services=$(klipper_exists) klipper_services=$(klipper_exists)
user_input+=("${python_version}")
### return early if klipper already exists
if [[ -n ${klipper_services} ]]; then if [[ -n ${klipper_services} ]]; then
local error="At least one Klipper service is already installed:" local error="At least one Klipper service is already installed:"
for s in ${klipper_services}; do for s in ${klipper_services}; do
log_info "Found Klipper service: ${s}" log_info "Found Klipper service: ${s}"
error="${error}\n ➔ ${s}" error="${error}\n ➔ ${s}"
done done
print_error "${error}" && return print_error "${error}" && return
fi fi
@@ -72,6 +74,7 @@ function klipper_setup_dialog(){
### error messages on invalid input ### error messages on invalid input
error_msg "Input not a number" error_msg "Input not a number"
done && select_msg "${klipper_count}" done && select_msg "${klipper_count}"
user_input+=("${klipper_count}") user_input+=("${klipper_count}")
### confirm instance amount ### confirm instance amount
@@ -118,7 +121,7 @@ function klipper_setup_dialog(){
done done
### get user input for custom names ### get user input for custom names
if [[ "${custom_names}" == "true" ]]; then if [[ ${custom_names} == "true" ]]; then
local i=1 name="" re="^[0-9a-zA-Z]+$" local i=1 name="" re="^[0-9a-zA-Z]+$"
while [[ ! ${name} =~ ${re} && ${i} -le ${klipper_count} ]]; do while [[ ! ${name} =~ ${re} && ${i} -le ${klipper_count} ]]; do
read -p "${cyan}###### Name for instance #${i}:${white} " name read -p "${cyan}###### Name for instance #${i}:${white} " name
@@ -132,15 +135,15 @@ function klipper_setup_dialog(){
done done
else else
### if no custom names are used, add the respective amount of indices to the user_input array ### if no custom names are used, add the respective amount of indices to the user_input array
for ((i=1; i <= klipper_count; i++)); do for (( i=1; i <= klipper_count; i++ )); do
user_input+=("${i}") user_input+=("${i}")
done done
fi fi
fi fi
((klipper_count > 1)) && status_msg "Installing ${klipper_count} Klipper instances ..." (( klipper_count > 1 )) && status_msg "Installing ${klipper_count} Klipper instances ..."
((klipper_count == 1)) && status_msg "Installing single Klipper instance ..." (( klipper_count == 1 )) && status_msg "Installing single Klipper instance ..."
klipper_setup "${user_input[@]}" klipper_setup "${user_input[@]}"
} }
@@ -157,7 +160,7 @@ function install_klipper_packages(){
### add dbus requirement for DietPi distro ### add dbus requirement for DietPi distro
[[ -e "/boot/dietpi/.version" ]] && packages+=" dbus" [[ -e "/boot/dietpi/.version" ]] && packages+=" dbus"
if [[ "${python_version}" == "python3" ]]; then if [[ ${python_version} == "python3" ]]; then
### replace python-dev with python3-dev if python3 was selected ### replace python-dev with python3-dev if python3 was selected
packages="${packages//python-dev/python3-dev}" packages="${packages//python-dev/python3-dev}"
else else
@@ -231,6 +234,7 @@ function klipper_setup(){
### step 1: clone klipper ### step 1: clone klipper
### force remove existing klipper dir and clone into fresh klipper dir ### force remove existing klipper dir and clone into fresh klipper dir
[[ -d ${KLIPPER_DIR} ]] && rm -rf "${KLIPPER_DIR}" [[ -d ${KLIPPER_DIR} ]] && rm -rf "${KLIPPER_DIR}"
if [[ -z ${custom_repo} ]]; then if [[ -z ${custom_repo} ]]; then
status_msg "Downloading Klipper ..." status_msg "Downloading Klipper ..."
cd "${HOME}" && git clone "${KLIPPER_REPO}" cd "${HOME}" && git clone "${KLIPPER_REPO}"
@@ -265,12 +269,14 @@ function klipper_setup(){
local confirm="" local confirm=""
(( instance_arr[0] == 1)) && confirm="Klipper has been set up!" (( instance_arr[0] == 1)) && confirm="Klipper has been set up!"
(( instance_arr[0] > 1)) && confirm="${instance_arr[0]} Klipper instances have been set up!" (( instance_arr[0] > 1)) && confirm="${instance_arr[0]} Klipper instances have been set up!"
print_confirm "${confirm}" && return print_confirm "${confirm}" && return
} }
function write_klipper_service(){ function write_klipper_service(){
local i=${1} cfg=${2} log=${3} printer=${4} uds=${5} service=${6} local i=${1} cfg=${2} log=${3} printer=${4} uds=${5} service=${6}
local service_template="${KIAUH_SRCDIR}/resources/klipper.service" local service_template="${KIAUH_SRCDIR}/resources/klipper.service"
### replace all placeholders ### replace all placeholders
if [[ ! -f ${service} ]]; then if [[ ! -f ${service} ]]; then
status_msg "Creating Klipper Service ${i} ..." status_msg "Creating Klipper Service ${i} ..."
@@ -287,13 +293,13 @@ function write_example_printer_cfg(){
local cfg_template="${KIAUH_SRCDIR}/resources/printer.cfg" local cfg_template="${KIAUH_SRCDIR}/resources/printer.cfg"
### create a config directory if it doesn't exist ### create a config directory if it doesn't exist
if [[ ! -d "${cfg_dir}" ]]; then if [[ ! -d ${cfg_dir} ]]; then
status_msg "Creating '${cfg_dir}' ..." status_msg "Creating '${cfg_dir}' ..."
mkdir -p "${cfg_dir}" mkdir -p "${cfg_dir}"
fi fi
### create a minimal config if there is no printer.cfg ### create a minimal config if there is no printer.cfg
if [[ ! -f "${cfg}" ]]; then if [[ ! -f ${cfg} ]]; then
status_msg "Creating minimal example printer.cfg ..." status_msg "Creating minimal example printer.cfg ..."
cp "${cfg_template}" "${cfg}" cp "${cfg_template}" "${cfg}"
fi fi
@@ -337,7 +343,7 @@ function create_klipper_service(){
write_klipper_service "${names[${j}]}" "${cfg}" "${log}" "${printer}" "${uds}" "${service}" write_klipper_service "${names[${j}]}" "${cfg}" "${log}" "${printer}" "${uds}" "${service}"
write_example_printer_cfg "${cfg_dir}" "${cfg}" write_example_printer_cfg "${cfg_dir}" "${cfg}"
ok_msg "Klipper instance 'klipper-${names[${j}]} created!" ok_msg "Klipper instance 'klipper-${names[${j}]} created!"
j=$((j+1)) j=$(( j + 1 ))
done && unset j done && unset j
else else
@@ -351,6 +357,7 @@ function create_klipper_service(){
function remove_klipper_sysvinit() { function remove_klipper_sysvinit() {
[[ ! -e "${INITD}/klipper" ]] && return [[ ! -e "${INITD}/klipper" ]] && return
status_msg "Removing Klipper SysVinit service ..." status_msg "Removing Klipper SysVinit service ..."
sudo systemctl stop klipper sudo systemctl stop klipper
sudo update-rc.d -f klipper remove sudo update-rc.d -f klipper remove
@@ -360,15 +367,16 @@ function remove_klipper_sysvinit() {
function remove_klipper_systemd() { function remove_klipper_systemd() {
[[ -z $(klipper_systemd) ]] && return [[ -z $(klipper_systemd) ]] && return
status_msg "Removing Klipper Systemd Services ..." status_msg "Removing Klipper Systemd Services ..."
for service in $(klipper_systemd | cut -d"/" -f5) for service in $(klipper_systemd | cut -d"/" -f5); do
do
status_msg "Removing ${service} ..." status_msg "Removing ${service} ..."
sudo systemctl stop "${service}" sudo systemctl stop "${service}"
sudo systemctl disable "${service}" sudo systemctl disable "${service}"
sudo rm -f "${SYSTEMD}/${service}" sudo rm -f "${SYSTEMD}/${service}"
ok_msg "Done!" ok_msg "Done!"
done done
### reloading units ### reloading units
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl reset-failed sudo systemctl reset-failed
@@ -378,6 +386,7 @@ function remove_klipper_systemd() {
function remove_klipper_logs() { function remove_klipper_logs() {
local files regex="klippy(-[0-9a-zA-Z]+)?\.log(.*)?" local files regex="klippy(-[0-9a-zA-Z]+)?\.log(.*)?"
files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" 2> /dev/null | sort) files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" 2> /dev/null | sort)
if [[ -n ${files} ]]; then if [[ -n ${files} ]]; then
for file in ${files}; do for file in ${files}; do
status_msg "Removing ${file} ..." status_msg "Removing ${file} ..."
@@ -390,6 +399,7 @@ function remove_klipper_logs() {
function remove_klipper_uds() { function remove_klipper_uds() {
local files local files
files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/klippy_uds(-[0-9a-zA-Z]+)?" | sort) files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/klippy_uds(-[0-9a-zA-Z]+)?" | sort)
if [[ -n ${files} ]]; then if [[ -n ${files} ]]; then
for file in ${files}; do for file in ${files}; do
status_msg "Removing ${file} ..." status_msg "Removing ${file} ..."
@@ -401,6 +411,7 @@ function remove_klipper_uds() {
function remove_klipper_printer() { function remove_klipper_printer() {
local files local files
files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/printer(-[0-9a-zA-Z]+)?" | sort) files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/printer(-[0-9a-zA-Z]+)?" | sort)
if [[ -n ${files} ]]; then if [[ -n ${files} ]]; then
for file in ${files}; do for file in ${files}; do
@@ -413,6 +424,7 @@ function remove_klipper_printer() {
function remove_klipper_dir() { function remove_klipper_dir() {
[[ ! -d ${KLIPPER_DIR} ]] && return [[ ! -d ${KLIPPER_DIR} ]] && return
status_msg "Removing Klipper directory ..." status_msg "Removing Klipper directory ..."
rm -rf "${KLIPPER_DIR}" rm -rf "${KLIPPER_DIR}"
ok_msg "Directory removed!" ok_msg "Directory removed!"
@@ -420,6 +432,7 @@ function remove_klipper_dir() {
function remove_klipper_env() { function remove_klipper_env() {
[[ ! -d ${KLIPPY_ENV} ]] && return [[ ! -d ${KLIPPY_ENV} ]] && return
status_msg "Removing klippy-env directory ..." status_msg "Removing klippy-env directory ..."
rm -rf "${KLIPPY_ENV}" rm -rf "${KLIPPY_ENV}"
ok_msg "Directory removed!" ok_msg "Directory removed!"
@@ -444,6 +457,7 @@ function remove_klipper(){
function update_klipper(){ function update_klipper(){
do_action_service "stop" "klipper" do_action_service "stop" "klipper"
if [[ ! -d ${KLIPPER_DIR} ]]; then if [[ ! -d ${KLIPPER_DIR} ]]; then
cd "${HOME}" && git clone "${KLIPPER_REPO}" cd "${HOME}" && git clone "${KLIPPER_REPO}"
else else
@@ -455,6 +469,7 @@ function update_klipper(){
### install possible new python dependencies ### install possible new python dependencies
"${KLIPPY_ENV}"/bin/pip install -r "${KLIPPER_DIR}/scripts/klippy-requirements.txt" "${KLIPPY_ENV}"/bin/pip install -r "${KLIPPER_DIR}/scripts/klippy-requirements.txt"
fi fi
ok_msg "Update complete!" ok_msg "Update complete!"
do_action_service "restart" "klipper" do_action_service "restart" "klipper"
} }
@@ -466,6 +481,7 @@ function update_klipper(){
function get_klipper_status(){ function get_klipper_status(){
local sf_count status py_ver local sf_count status py_ver
sf_count="$(klipper_systemd | wc -w)" sf_count="$(klipper_systemd | wc -w)"
### detect an existing "legacy" klipper init.d installation ### detect an existing "legacy" klipper init.d installation
if [[ $(klipper_systemd | wc -w) -eq 0 ]] \ if [[ $(klipper_systemd | wc -w) -eq 0 ]] \
&& [[ $(klipper_initd | wc -w) -ge 1 ]]; then && [[ $(klipper_initd | wc -w) -ge 1 ]]; then
@@ -495,20 +511,23 @@ function get_klipper_status(){
else else
status="Incomplete!" status="Incomplete!"
fi fi
echo "${status}" echo "${status}"
} }
function get_local_klipper_commit(){ function get_local_klipper_commit(){
local commit
[[ ! -d ${KLIPPER_DIR} || ! -d "${KLIPPER_DIR}/.git" ]] && return [[ ! -d ${KLIPPER_DIR} || ! -d "${KLIPPER_DIR}/.git" ]] && return
local commit
cd "${KLIPPER_DIR}" cd "${KLIPPER_DIR}"
commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)" commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)"
echo "${commit}" echo "${commit}"
} }
function get_remote_klipper_commit(){ function get_remote_klipper_commit(){
local commit
[[ ! -d ${KLIPPER_DIR} || ! -d "${KLIPPER_DIR}/.git" ]] && return [[ ! -d ${KLIPPER_DIR} || ! -d "${KLIPPER_DIR}/.git" ]] && return
local commit
cd "${KLIPPER_DIR}" && git fetch origin -q cd "${KLIPPER_DIR}" && git fetch origin -q
commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2)
echo "${commit}" echo "${commit}"
@@ -519,6 +538,7 @@ function compare_klipper_versions(){
local versions local_ver remote_ver local versions local_ver remote_ver
local_ver="$(get_local_klipper_commit)" local_ver="$(get_local_klipper_commit)"
remote_ver="$(get_remote_klipper_commit)" remote_ver="$(get_remote_klipper_commit)"
if [[ ${local_ver} != "${remote_ver}" ]]; then if [[ ${local_ver} != "${remote_ver}" ]]; then
versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" versions="${yellow}$(printf " %-14s" "${local_ver}")${white}"
versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}"
@@ -529,6 +549,7 @@ function compare_klipper_versions(){
versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}"
KLIPPER_UPDATE_AVAIL="false" KLIPPER_UPDATE_AVAIL="false"
fi fi
echo "${versions}" echo "${versions}"
} }
@@ -539,11 +560,13 @@ function compare_klipper_versions(){
function get_klipper_cfg_dir() { function get_klipper_cfg_dir() {
local cfg_dir local cfg_dir
read_kiauh_ini "${FUNCNAME[0]}" read_kiauh_ini "${FUNCNAME[0]}"
if [[ -z ${custom_klipper_cfg_loc} ]]; then if [[ -z ${custom_klipper_cfg_loc} ]]; then
cfg_dir="${HOME}/klipper_config" cfg_dir="${HOME}/klipper_config"
else else
cfg_dir="${custom_klipper_cfg_loc}" cfg_dir="${custom_klipper_cfg_loc}"
fi fi
echo "${cfg_dir}" echo "${cfg_dir}"
} }