From ef10bcd151566648a81e6f33766049f1c08672cc Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 5 Feb 2021 23:25:21 +0200 Subject: [PATCH] install: Move modify config check into separate function This helps us make the logic easier to read, and remove unneeded confusion. --- install.sh | 90 +++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/install.sh b/install.sh index 9a666716..f1585efa 100755 --- a/install.sh +++ b/install.sh @@ -76,6 +76,52 @@ function backup_append() { echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" } +function modify_config_files() { + if ! [[ $silent ]]; then + if [ -e "$HOME/$BACKUP_FILE" ]; then + echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 + while ! [[ $silent ]]; do + read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP + case $RESP in + [yY]) + break + ;; + [nN] | "") + echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" + exit 1 + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + done + fi + + while ! [[ $append_to_config ]]; do + read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice + case $choice in + [yY]) + append_to_config=true + break + ;; + [nN] | "") + break + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + done + fi + if [[ $append_to_config ]]; then + # backup/append + backup_append + else + # backup/new by default + backup_new + fi +} + for param in "$@"; do shift case "$param" in @@ -130,48 +176,8 @@ esac BACKUP_FILE=$CONFIG_FILE.bak echo "Installing bash-it" -if ! [[ $silent ]] && ! [[ $no_modify_config ]]; then - if [ -e "$HOME/$BACKUP_FILE" ]; then - echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 - while ! [ $silent ]; do - read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP - case $RESP in - [yY]) - break - ;; - [nN] | "") - echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" - exit 1 - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac - done - fi - - while ! [ $append_to_config ]; do - read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice - case $choice in - [yY]) - backup_append - break - ;; - [nN] | "") - backup_new - break - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac - done -elif [[ $append_to_config ]]; then - # backup/append - backup_append -elif [[ $silent ]] && ! [[ $no_modify_config ]]; then - # backup/new by default - backup_new +if ! [[ $no_modify_config ]]; then + modify_config_files fi # Disable auto-reload in case its enabled