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.
pull/1830/head
Noah Gorny 2021-02-05 23:41:47 +02:00
parent 9e37f0d09e
commit af17501318
1 changed files with 23 additions and 7 deletions

View File

@ -11,6 +11,7 @@ function show_usage() {
echo "--interactive (-i): Interactively choose plugins" echo "--interactive (-i): Interactively choose plugins"
echo "--no-modify-config (-n): Do not modify existing config file" 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 "--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 exit 0
} }
@ -77,17 +78,21 @@ function backup_append() {
} }
function check_for_backup() { function check_for_backup() {
if [ -e "$HOME/$BACKUP_FILE" ]; 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 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 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 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 case $RESP in
[yY]) [yY])
overwrite_backup=true
break break
;; ;;
[nN] | "") [nN] | "")
echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" break
exit 1
;; ;;
*) *)
echo -e "\033[91mPlease choose y or n.\033[m" echo -e "\033[91mPlease choose y or n.\033[m"
@ -95,12 +100,21 @@ function check_for_backup() {
esac esac
done done
fi 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() { function modify_config_files() {
if ! [[ $silent ]]; then check_for_backup
check_for_backup
if ! [[ $silent ]]; then
while ! [[ $append_to_config ]]; do 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 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 case $choice in
@ -134,12 +148,13 @@ for param in "$@"; do
"--interactive") set -- "$@" "-i" ;; "--interactive") set -- "$@" "-i" ;;
"--no-modify-config") set -- "$@" "-n" ;; "--no-modify-config") set -- "$@" "-n" ;;
"--append-to-config") set -- "$@" "-a" ;; "--append-to-config") set -- "$@" "-a" ;;
"--overwrite-backup") set -- "$@" "-f" ;;
*) set -- "$@" "$param" ;; *) set -- "$@" "$param" ;;
esac esac
done done
OPTIND=1 OPTIND=1
while getopts "hsina" opt; do while getopts "hsinaf" opt; do
case "$opt" in case "$opt" in
"h") "h")
show_usage show_usage
@ -149,6 +164,7 @@ while getopts "hsina" opt; do
"i") interactive=true ;; "i") interactive=true ;;
"n") no_modify_config=true ;; "n") no_modify_config=true ;;
"a") append_to_config=true ;; "a") append_to_config=true ;;
"f") overwrite_backup=true ;;
"?") "?")
show_usage >&2 show_usage >&2
exit 1 exit 1