From c6723b6c545d9380acc3c55de5e79d5aa4dc1cc0 Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Sat, 14 Mar 2020 17:37:54 -0600 Subject: [PATCH] replace confirmation loops with func prompt_yn --- ab-openldap/ab-openldap.sh | 69 ++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/ab-openldap/ab-openldap.sh b/ab-openldap/ab-openldap.sh index ce33905..d504378 100755 --- a/ab-openldap/ab-openldap.sh +++ b/ab-openldap/ab-openldap.sh @@ -17,6 +17,27 @@ textblock () { printf "%s\n" "$1" | fold -w "$width" -s } +prompt_yn () { + # confirmation loop + while true; do + printf "%sAre you sure you want to continue? (y/n)%s " \ + "$cyan" "$norm" + read -r yn + case "$yn" in + [Yy]*) + break + ;; + [Nn]*) + printf "\n" + exit 0 + ;; + *) + printf "Please answer 'y' or 'n'.\n" + ;; + esac + done +} + # text formatting presets bold=$(tput bold) @@ -198,25 +219,7 @@ if [ $clean = true ]; then "$red" "$norm" printf "%sThis action CANNOT be undone!%s\n\n" \ "$red" "$norm" - - # confirmation loop - while true; do - printf "%sAre you sure you want to continue? (yes/no)%s " \ - "$cyan" "$norm" - read -r yn - case "$yn" in - [Yy]*) - break - ;; - [Nn]*) - printf "\n" - exit 0 - ;; - *) - printf "Please answer 'y' or 'n'.\n" - ;; - esac - done + prompt_yn # get all ab-openldap containers containers=$(docker ps -a --no-trunc --filter "label=org.label-schema.name=ab-openldap" --format "{{ .Names }}") @@ -251,28 +254,12 @@ if [ $clean = true ]; then printf "%s...done%s\n" "$cyan" "$norm" done elif [ $restore = true ]; then -# restore backup -printf "%s\n*** Restoring Backup ***\n\n%s" "$magenta" "$norm" -printf "To avoid errors due to existing files, this script will delete any volumes that have the following names (based on --data and --ldif):\n" -printf "\t%s\n\t%s\n" "$volume_data" "$volume_ldif" - # confirmation loop - while true; do - printf "%sDo you want to continue? (yes/no)%s " \ - "$cyan" "$norm" - read -r yn - case "$yn" in - [Yy]*) - break - ;; - [Nn]*) - printf "\n" - exit 0 - ;; - *) - printf "Please answer 'y' or 'n'.\n" - ;; - esac - done + # restore backup + printf "%s\n*** Restoring Backup ***\n\n%s" "$magenta" "$norm" + printf "To avoid errors due to existing files, this script will delete any volumes that have the following names (based on --data and --ldif):\n" + printf "\t%s\n\t%s\n" "$volume_data" "$volume_ldif" + prompt_yn + # delete any conflicting volumes docker volume rm ${volume_data} > /dev/null 2>&1 docker volume rm ${volume_ldif} > /dev/null 2>&1