From 665db11d8cb0c31d021a1780b99c2d11c0c4c290 Mon Sep 17 00:00:00 2001 From: Heavybullets8 Date: Wed, 10 Aug 2022 17:46:01 -0600 Subject: [PATCH] replace ugly if statement with case --- functions/cmd_to_container.sh | 109 +++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 34 deletions(-) diff --git a/functions/cmd_to_container.sh b/functions/cmd_to_container.sh index d1dc1870..787b1c62 100644 --- a/functions/cmd_to_container.sh +++ b/functions/cmd_to_container.sh @@ -30,41 +30,82 @@ do containers+=("$(echo "$search" | grep "$pod" | awk '{print $4}')") done -if [[ "${#containers[@]}" == 0 ]]; then - echo -e "No containers available\nAre you sure the application in running?" - exit -elif [[ "${#containers[@]}" == 1 ]]; then - container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}') - container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') -else - while true - do - clear -x - title - cont_search=$( - for i in "${containers[@]}" +case "${#containers[@]}" in + 0) + echo -e "No containers available\nAre you sure the application in running?" + exit + ;; + 1) + container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}') + container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') + ;; + + *) + while true do - echo "$i" - done | nl -s ") " | column -t - ) - echo "$cont_search" - echo - echo "0) Exit" - read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } - if [[ $selection == 0 ]]; then - echo "Exiting.." - exit - elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then - echo "Error: \"$selection\" was not an option.. Try again" - sleep 3 - continue - else - break - fi - done - container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}') - container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') -fi + clear -x + title + cont_search=$( + for i in "${containers[@]}" + do + echo "$i" + done | nl -s ") " | column -t + ) + echo "$cont_search" + echo + echo "0) Exit" + read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + if [[ $selection == 0 ]]; then + echo "Exiting.." + exit + elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then + echo "Error: \"$selection\" was not an option.. Try again" + sleep 3 + continue + else + break + fi + done + container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}') + container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') + ;; +esac + +# if [[ "${#containers[@]}" == 0 ]]; then +# echo -e "No containers available\nAre you sure the application in running?" +# exit +# elif [[ "${#containers[@]}" == 1 ]]; then +# container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}') +# container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') +# else +# while true +# do +# clear -x +# title +# cont_search=$( +# for i in "${containers[@]}" +# do +# echo "$i" +# done | nl -s ") " | column -t +# ) +# echo "$cont_search" +# echo +# echo "0) Exit" +# read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } +# if [[ $selection == 0 ]]; then +# echo "Exiting.." +# exit +# elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then +# echo "Error: \"$selection\" was not an option.. Try again" +# sleep 3 +# continue +# else +# break +# fi +# done +# container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}') +# container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}') +# fi while true do clear -x