From 11311b17f6e108d1b201f47f46f6a2619031fbfd Mon Sep 17 00:00:00 2001 From: ravenhall Date: Thu, 19 May 2016 22:08:46 -0500 Subject: [PATCH 1/3] Implemented solution to issue #676 --- install.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index 17250eb0..ff354b19 100755 --- a/install.sh +++ b/install.sh @@ -1,4 +1,44 @@ #!/usr/bin/env bash +# bash-it installer +show_usage() { + echo -e "\n$0 : Install bash-it" + echo -e "Usage:\n$0 [arguments] \n" + echo "Arguments:" + echo "--help (-h): Display this help message" + echo "--silent (-s): Install default settings without prompting for input"; + echo "--interactive (-i): Interactively choose plugins" + exit 0; +} + +echo "Installing bash-it" + +for param in "$@"; do + shift + case "$param" in + "--help") set -- "$@" "-h" ;; + "--silent") set -- "$@" "-s" ;; + "--interactive") set -- "$@" "-i" ;; + *) set -- "$@" "$param" + esac +done + +OPTIND=1 +while getopts "hsi" opt +do + case "$opt" in + "h") show_usage; exit 0 ;; + "s") silent=true ;; + "i") interactive=true ;; + "?") show_usage >&2; exit 1 ;; + esac +done +shift $(expr $OPTIND - 1) + +if [[ $silent ]] && [[ $interactive ]]; then + echo "Options --silent and --interactive are mutually exclusive. Please choose one or the other." + exit 1; +fi + BASH_IT="$(cd "$(dirname "$0")" && pwd)" case $OSTYPE in @@ -14,8 +54,7 @@ BACKUP_FILE=$CONFIG_FILE.bak 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 true - 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 case $RESP in [yY]) @@ -32,7 +71,7 @@ if [ -e "$HOME/$BACKUP_FILE" ]; then done fi -while true +while ! [ $silent ] 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 @@ -58,6 +97,14 @@ do esac done +if [ $silent ]; then + # backup/new by default + 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" + sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" +fi + echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" function load_one() { @@ -98,7 +145,7 @@ function load_some() { done } -if [[ "$1" == "--interactive" ]] +if [[ $interactive ]] && ! [[ $silent ]] ; then for type in "aliases" "plugins" "completion" do From c6c21abb0eec8199ff50f7b4f0875761c5d2a329 Mon Sep 17 00:00:00 2001 From: ravenhall Date: Tue, 31 May 2016 14:14:12 -0500 Subject: [PATCH 2/3] Colorized error, relocated installing message, created backup_new func, reformatted do --- install.sh | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/install.sh b/install.sh index ff354b19..ac53675a 100755 --- a/install.sh +++ b/install.sh @@ -10,8 +10,6 @@ show_usage() { exit 0; } -echo "Installing bash-it" - for param in "$@"; do shift case "$param" in @@ -35,7 +33,7 @@ done shift $(expr $OPTIND - 1) if [[ $silent ]] && [[ $interactive ]]; then - echo "Options --silent and --interactive are mutually exclusive. Please choose one or the other." + echo "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m" exit 1; fi @@ -51,7 +49,7 @@ case $OSTYPE in esac BACKUP_FILE=$CONFIG_FILE.bak - +echo "Installing bash-it" 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 @@ -71,8 +69,7 @@ if [ -e "$HOME/$BACKUP_FILE" ]; then done fi -while ! [ $silent ] -do +while ! [ $silent ]; 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]) @@ -85,10 +82,7 @@ do break ;; [nN]|"") - 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" - sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" + backup_new break ;; *) @@ -99,14 +93,9 @@ done if [ $silent ]; then # backup/new by default - 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" - sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" + backup_new fi -echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" - function load_one() { file_type=$1 file_to_enable=$2 @@ -145,6 +134,14 @@ function load_some() { done } +function backup_new() { + 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" + 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" +} + if [[ $interactive ]] && ! [[ $silent ]] ; then for type in "aliases" "plugins" "completion" From 49afeb707e385bae2368acdc22d0bd271ae2a1c8 Mon Sep 17 00:00:00 2001 From: ravenhall Date: Tue, 31 May 2016 18:45:36 -0500 Subject: [PATCH 3/3] Added forgotten -e and moved funcs to the top to prevent scoping issue --- install.sh | 101 ++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/install.sh b/install.sh index ac53675a..e6518042 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash # bash-it installer -show_usage() { + +# Show how to use this installer +function show_usage() { echo -e "\n$0 : Install bash-it" echo -e "Usage:\n$0 [arguments] \n" echo "Arguments:" @@ -10,6 +12,55 @@ show_usage() { exit 0; } +# enable a thing +function load_one() { + file_type=$1 + file_to_enable=$2 + mkdir -p "$BASH_IT/${file_type}/enabled" + + dest="${BASH_IT}/${file_type}/enabled/${file_to_enable}" + if [ ! -e "${dest}" ]; then + ln -sf "../available/${file_to_enable}" "${dest}" + else + echo "File ${dest} exists, skipping" + fi +} + +# Interactively enable several things +function load_some() { + file_type=$1 + [ -d "$BASH_IT/$file_type/enabled" ] || mkdir "$BASH_IT/$file_type/enabled" + for path in `ls $BASH_IT/${file_type}/available/[^_]*` + do + file_name=$(basename "$path") + while true + do + read -e -n 1 -p "Would you like to enable the ${file_name%%.*} $file_type? [y/N] " RESP + case $RESP in + [yY]) + ln -s "../available/${file_name}" "$BASH_IT/$file_type/enabled" + break + ;; + [nN]|"") + break + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + done + done +} + +# Back up existing profile and create new one for bash-it +function backup_new() { + 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" + 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" +} + for param in "$@"; do shift case "$param" in @@ -33,7 +84,7 @@ done shift $(expr $OPTIND - 1) if [[ $silent ]] && [[ $interactive ]]; then - echo "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m" + echo -e "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m" exit 1; fi @@ -96,52 +147,6 @@ if [ $silent ]; then backup_new fi -function load_one() { - file_type=$1 - file_to_enable=$2 - mkdir -p "$BASH_IT/${file_type}/enabled" - - dest="${BASH_IT}/${file_type}/enabled/${file_to_enable}" - if [ ! -e "${dest}" ]; then - ln -sf "../available/${file_to_enable}" "${dest}" - else - echo "File ${dest} exists, skipping" - fi -} - -function load_some() { - file_type=$1 - [ -d "$BASH_IT/$file_type/enabled" ] || mkdir "$BASH_IT/$file_type/enabled" - for path in `ls $BASH_IT/${file_type}/available/[^_]*` - do - file_name=$(basename "$path") - while true - do - read -e -n 1 -p "Would you like to enable the ${file_name%%.*} $file_type? [y/N] " RESP - case $RESP in - [yY]) - ln -s "../available/${file_name}" "$BASH_IT/$file_type/enabled" - break - ;; - [nN]|"") - break - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac - done - done -} - -function backup_new() { - 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" - 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" -} - if [[ $interactive ]] && ! [[ $silent ]] ; then for type in "aliases" "plugins" "completion"