From ef10bcd151566648a81e6f33766049f1c08672cc Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 5 Feb 2021 23:25:21 +0200 Subject: [PATCH 1/5] 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 From 9e37f0d09edefa9e8c972ed109e11de56d718ee7 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 5 Feb 2021 23:28:12 +0200 Subject: [PATCH 2/5] install: Move backup check into a separate function --- install.sh | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/install.sh b/install.sh index f1585efa..16b59905 100755 --- a/install.sh +++ b/install.sh @@ -76,26 +76,30 @@ function backup_append() { echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" } +function check_for_backup() { + 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 +} + 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 + check_for_backup 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 From af1750131816e9a954539e7b8af6d8de7486d101 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 5 Feb 2021 23:41:47 +0200 Subject: [PATCH 3/5] install: Add --overwrite-backup flag This will ensure users will specify whether they want to overwrite the backup or not, and check it even when we do silent installation This does change the default behaviour and exits if the -f flag is not specified and a backup is present. --- install.sh | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/install.sh b/install.sh index 16b59905..50f31f29 100755 --- a/install.sh +++ b/install.sh @@ -11,6 +11,7 @@ function show_usage() { echo "--interactive (-i): Interactively choose plugins" echo "--no-modify-config (-n): Do not modify existing config file" echo "--append-to-config (-a): Keep existing config file and append bash-it templates at the end" + echo "--overwrite-backup (-f): Overwrite existing backup" exit 0 } @@ -77,17 +78,21 @@ function backup_append() { } function check_for_backup() { - 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 + if ! [ -e "$HOME/$BACKUP_FILE" ]; then + return + fi + echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 + + if ! [[ $overwrite_backup ]]; then 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]) + overwrite_backup=true break ;; [nN] | "") - echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" - exit 1 + break ;; *) echo -e "\033[91mPlease choose y or n.\033[m" @@ -95,12 +100,21 @@ function check_for_backup() { esac done fi + if ! [[ $overwrite_backup ]]; then + echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" + if [[ $silent ]]; then + echo -e "\033[91mUse \"-f\" flag to force overwrite of backup.\033[m" + fi + exit 1 + else + echo -e "\033[0;32mOverwriting backup...\033[m" + fi } function modify_config_files() { - if ! [[ $silent ]]; then - check_for_backup + check_for_backup + if ! [[ $silent ]]; then 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 @@ -134,12 +148,13 @@ for param in "$@"; do "--interactive") set -- "$@" "-i" ;; "--no-modify-config") set -- "$@" "-n" ;; "--append-to-config") set -- "$@" "-a" ;; + "--overwrite-backup") set -- "$@" "-f" ;; *) set -- "$@" "$param" ;; esac done OPTIND=1 -while getopts "hsina" opt; do +while getopts "hsinaf" opt; do case "$opt" in "h") show_usage @@ -149,6 +164,7 @@ while getopts "hsina" opt; do "i") interactive=true ;; "n") no_modify_config=true ;; "a") append_to_config=true ;; + "f") overwrite_backup=true ;; "?") show_usage >&2 exit 1 From 3e7adde63840ea4301291a2779bb90c6d68c996c Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Sat, 6 Feb 2021 20:25:56 +0200 Subject: [PATCH 4/5] install: Move to double brackets in check_for-backup check --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 50f31f29..28a48ebd 100755 --- a/install.sh +++ b/install.sh @@ -78,7 +78,7 @@ function backup_append() { } function check_for_backup() { - if ! [ -e "$HOME/$BACKUP_FILE" ]; then + if ! [[ -e "$HOME/$BACKUP_FILE" ]]; then return fi echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 From 5c192575cef86276067f1d1493561da105f093e0 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Sat, 6 Feb 2021 20:36:04 +0200 Subject: [PATCH 5/5] install: Add bash-it prefix to all functions --- install.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/install.sh b/install.sh index 28a48ebd..1dac7b71 100755 --- a/install.sh +++ b/install.sh @@ -2,7 +2,7 @@ # bash-it installer # Show how to use this installer -function show_usage() { +function _bash-it_show_usage() { echo -e "\n$0 : Install bash-it" echo -e "Usage:\n$0 [arguments] \n" echo "Arguments:" @@ -16,7 +16,7 @@ function show_usage() { } # enable a thing -function load_one() { +function _bash-it_load_one() { file_type=$1 file_to_enable=$2 mkdir -p "$BASH_IT/${file_type}/enabled" @@ -30,7 +30,7 @@ function load_one() { } # Interactively enable several things -function load_some() { +function _bash-it_load_some() { file_type=$1 single_type=$(echo "$file_type" | sed -e "s/aliases$/alias/g" | sed -e "s/plugins$/plugin/g") enable_func="_enable-$single_type" @@ -57,27 +57,27 @@ function load_some() { } # Back up existing profile -function backup() { +function _bash-it_backup() { test -w "$HOME/$CONFIG_FILE" \ && cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" \ && echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" } # Back up existing profile and create new one for bash-it -function backup_new() { - backup +function _bash-it_backup_new() { + _bash-it_backup sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" } # Back up existing profile and append bash-it templates at the end -function backup_append() { - backup +function _bash-it_backup_append() { + _bash-it_backup (sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" } -function check_for_backup() { +function _bash-it_check_for_backup() { if ! [[ -e "$HOME/$BACKUP_FILE" ]]; then return fi @@ -111,8 +111,8 @@ function check_for_backup() { fi } -function modify_config_files() { - check_for_backup +function _bash-it_modify_config_files() { + _bash-it_check_for_backup if ! [[ $silent ]]; then while ! [[ $append_to_config ]]; do @@ -133,10 +133,10 @@ function modify_config_files() { fi if [[ $append_to_config ]]; then # backup/append - backup_append + _bash-it_backup_append else # backup/new by default - backup_new + _bash-it_backup_new fi } @@ -157,7 +157,7 @@ OPTIND=1 while getopts "hsinaf" opt; do case "$opt" in "h") - show_usage + _bash-it_show_usage exit 0 ;; "s") silent=true ;; @@ -166,7 +166,7 @@ while getopts "hsinaf" opt; do "a") append_to_config=true ;; "f") overwrite_backup=true ;; "?") - show_usage >&2 + _bash-it_show_usage >&2 exit 1 ;; esac @@ -197,7 +197,7 @@ esac BACKUP_FILE=$CONFIG_FILE.bak echo "Installing bash-it" if ! [[ $no_modify_config ]]; then - modify_config_files + _bash-it_modify_config_files fi # Disable auto-reload in case its enabled @@ -214,7 +214,7 @@ source "$BASH_IT/lib/helpers.bash" if [[ $interactive ]] && ! [[ $silent ]]; then for type in "aliases" "plugins" "completion"; do echo -e "\033[0;32mEnabling $type\033[0m" - load_some $type + _bash-it_load_some $type done else echo ""