refactor(flash_klipper.sh): fix some shellcheck warnings

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-03 23:29:44 +02:00
parent 00d63816ae
commit 1dbbcf1eb2

View File

@@ -11,7 +11,7 @@
set -e set -e
show_flash_method_help(){ function show_flash_method_help(){
top_border top_border
echo -e "| ~~~~~~~~ < ? > Help: Flash MCU < ? > ~~~~~~~~ |" echo -e "| ~~~~~~~~ < ? > Help: Flash MCU < ? > ~~~~~~~~ |"
hr hr
@@ -47,13 +47,12 @@ show_flash_method_help(){
select_flash_method select_flash_method
break;; break;;
*) *)
print_unkown_cmd print_error "Invalid command!";;
print_msg && clear_msg;;
esac esac
done done
} }
select_flash_method(){ function select_flash_method(){
top_border top_border
echo -e "| ~~~~~~~~~~~~ [ Flash MCU ] ~~~~~~~~~~~~ |" echo -e "| ~~~~~~~~~~~~ [ Flash MCU ] ~~~~~~~~~~~~ |"
hr hr
@@ -89,13 +88,13 @@ select_flash_method(){
show_flash_method_help show_flash_method_help
break;; break;;
*) *)
print_unkown_cmd print_error "Invalid command!";;
print_msg && clear_msg;;
esac esac
done done
} }
select_mcu_id(){ function select_mcu_id(){
local id=0 sel_index=0
if [ ${#mcu_list[@]} -ge 1 ]; then if [ ${#mcu_list[@]} -ge 1 ]; then
top_border top_border
echo -e "| ${red}!!! ATTENTION !!!${white} |" echo -e "| ${red}!!! ATTENTION !!!${white} |"
@@ -105,47 +104,44 @@ select_mcu_id(){
bottom_border bottom_border
echo -e "${cyan}###### List of available MCU:${white}" echo -e "${cyan}###### List of available MCU:${white}"
### list all mcus ### list all mcus
id=0
for mcu in "${mcu_list[@]}"; do for mcu in "${mcu_list[@]}"; do
let id++ id=$((id+1))
echo -e " ${id}) ${mcu}" echo -e " ${id}) ${mcu}"
done done
### verify user input ### verify user input
sel_index=""
while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${id}" ]; do while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${id}" ]; do
echo echo
read -p "${cyan}###### Select MCU to flash:${white} " sel_index read -p "${cyan}###### Select MCU to flash:${white} " sel_index
if [[ ! (${sel_index} =~ ^[1-9]+$) ]]; then if [[ ! (${sel_index} =~ ^[1-9]+$) ]]; then
warn_msg "Invalid input!" error_msg "Invalid input!"
elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${id}" ]; then elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${id}" ]; then
warn_msg "Please select a number between 1 and ${id}!" error_msg "Please select a number between 1 and ${id}!"
fi fi
mcu_index=$(echo $((sel_index - 1))) mcu_index=$((sel_index - 1))
selected_mcu_id="${mcu_list[${mcu_index}]}" selected_mcu_id="${mcu_list[${mcu_index}]}"
done done
### confirm selection ### confirm selection
while true; do while true; do
echo -e "\n###### You selected:\n ● MCU #${sel_index}: ${selected_mcu_id}\n" echo -e "\n###### You selected:\n ● MCU #${sel_index}: ${selected_mcu_id}\n"
read -p "${cyan}###### Continue? (Y/n):${white} " yn read -p "${cyan}###### Continue? (Y/n):${white} " yn
case "$yn" in case "${yn}" in
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" select_msg "Yes"
status_msg "Flashing ${selected_mcu_id} ..." status_msg "Flashing ${selected_mcu_id} ..."
CONFIRM_FLASH=true CONFIRM_FLASH=true
break;; break;;
N|n|No|no) N|n|No|no)
echo -e "###### > No" select_msg "No"
CONFIRM_FLASH=false CONFIRM_FLASH=false
break;; break;;
*) *)
print_unkown_cmd print_error "Invalid command!";;
print_msg && clear_msg;;
esac esac
done done
fi fi
} }
flash_mcu(){ function flash_mcu(){
do_action_service "stop" "klipper" do_action_service "stop" "klipper"
make flash FLASH_DEVICE="${mcu_list[${mcu_index}]}" make flash FLASH_DEVICE="${mcu_list[${mcu_index}]}"
### evaluate exit code of make flash ### evaluate exit code of make flash
@@ -158,16 +154,15 @@ flash_mcu(){
do_action_service "start" "klipper" do_action_service "start" "klipper"
} }
flash_mcu_sd(){ function flash_mcu_sd(){
flash_script="${HOME}/klipper/scripts/flash-sdcard.sh" local i=0 board_list=()
local flash_script="${HOME}/klipper/scripts/flash-sdcard.sh"
### write each supported board to the array to make it selectable ### write each supported board to the array to make it selectable
board_list=() for board in $(/bin/bash "${flash_script}" -l | tail -n +2); do
for board in $("${flash_script}" -l | tail -n +2); do
board_list+=("${board}") board_list+=("${board}")
done done
i=0
top_border top_border
echo -e "| Please select the type of board that corresponds to |" echo -e "| Please select the type of board that corresponds to |"
echo -e "| the currently selected MCU ID you chose before. |" echo -e "| the currently selected MCU ID you chose before. |"
@@ -195,7 +190,7 @@ flash_mcu_sd(){
break break
else else
clear && print_header clear && print_header
ERROR_MSG="Invalid choice!" && print_msg && clear_msg print_error "Invalid choice!"
flash_mcu_sd flash_mcu_sd
fi fi
done done
@@ -230,22 +225,22 @@ flash_mcu_sd(){
do_action_service "start" "klipper" do_action_service "start" "klipper"
} }
build_fw(){ function build_fw(){
if [ -d "${KLIPPER_DIR}" ]; then if [ -d "${KLIPPER_DIR}" ]; then
cd "${KLIPPER_DIR}" cd "${KLIPPER_DIR}"
status_msg "Initializing firmware build ..." status_msg "Initializing firmware build ..."
dep=(build-essential dpkg-dev make) dep=(build-essential dpkg-dev make)
dependency_check dependency_check "${dep[@]}"
make clean && make menuconfig make clean && make menuconfig
status_msg "Building firmware ..." status_msg "Building firmware ..."
make && ok_msg "Firmware built!" make && ok_msg "Firmware built!"
else else
ERROR_MSG="Klipper was not found!\n Can not build firmware without Klipper!" print_error "Klipper was not found!\n Can not build firmware without Klipper!"
print_msg && clear_msg && return 1 return 1
fi fi
} }
select_mcu_connection(){ function select_mcu_connection(){
echo echo
top_border top_border
echo -e "| ${yellow}Make sure to have the controller board connected now!${white} |" echo -e "| ${yellow}Make sure to have the controller board connected now!${white} |"
@@ -277,25 +272,25 @@ select_mcu_connection(){
fi fi
} }
retrieve_id(){ function retrieve_id(){
local mcu_list=() mcu_count=1
status_msg "Identifying MCU ..." status_msg "Identifying MCU ..."
sleep 1 sleep 1
mcu_list=() [ "${1}" = "USB" ] && path="/dev/serial/by-id/*"
mcu_count=1 [ "${1}" = "UART" ] && path="/dev/ttyAMA0"
[ "$1" = "USB" ] && path="/dev/serial/by-id/*"
[ "$1" = "UART" ] && path="/dev/ttyAMA0"
if [[ "$(ls "${path}")" != "" ]] ; then if [[ "$(ls "${path}")" != "" ]] ; then
for mcu in ${path}; do for mcu in ${path}; do
declare "mcu_id_${mcu_count}"="${mcu}" declare "mcu_id_${mcu_count}"="${mcu}"
mcu_id="mcu_id_${mcu_count}" mcu_id="mcu_id_${mcu_count}"
mcu_list+=("${!mcu_id}") mcu_list+=("${!mcu_id}")
echo -e " ● ($1) MCU #${mcu_count}: ${cyan}${mcu}${white}\n" echo -e " ● ($1) MCU #${mcu_count}: ${cyan}${mcu}${white}\n"
let mcu_count++ mcu_count=$((mcu_count+1))
done done
fi 2>/dev/null fi 2>/dev/null
} }
function check_usergroup_dialout(){ function check_usergroup_dialout(){
local group_dialout
if grep -q "dialout" </etc/group && ! grep -q "dialout" <(groups "${USER}"); then if grep -q "dialout" </etc/group && ! grep -q "dialout" <(groups "${USER}"); then
group_dialout=false group_dialout=false
else else
@@ -322,9 +317,9 @@ function check_usergroup_dialout(){
bottom_border bottom_border
while true; do while true; do
read -p "${cyan}###### Add user '${USER}' to group(s) now? (Y/n):${white} " yn read -p "${cyan}###### Add user '${USER}' to group(s) now? (Y/n):${white} " yn
case "$yn" in case "${yn}" in
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" select_msg "Yes"
status_msg "Adding user '${USER}' to group(s) ..." status_msg "Adding user '${USER}' to group(s) ..."
if [ "${group_tty}" == "false" ]; then if [ "${group_tty}" == "false" ]; then
sudo usermod -a -G tty "${USER}" && ok_msg "Group 'tty' assigned!" sudo usermod -a -G tty "${USER}" && ok_msg "Group 'tty' assigned!"
@@ -334,11 +329,10 @@ function check_usergroup_dialout(){
fi fi
ok_msg "You need to relog/restart for the group(s) to be applied!" && exit 0;; ok_msg "You need to relog/restart for the group(s) to be applied!" && exit 0;;
N|n|No|no) N|n|No|no)
echo -e "###### > No" select_msg "No"
break;; break;;
*) *)
print_unkown_cmd print_error "Invalid command!";;
print_msg && clear_msg;;
esac esac
done done
fi fi