Add option to append template to existing config (#1723)
* Add option to append template to existing config * Add test for append-to-config option Co-authored-by: Egor Moor <egor.moor@edag-ps.com>pull/1737/head
parent
9b837b4f59
commit
3019dc331d
|
|
@ -19,6 +19,7 @@ The install script can take the following options:
|
||||||
* ``--interactive``\ : Asks the user which aliases, completions and plugins to enable.
|
* ``--interactive``\ : Asks the user which aliases, completions and plugins to enable.
|
||||||
* ``--silent``\ : Ask nothing and install using default settings.
|
* ``--silent``\ : Ask nothing and install using default settings.
|
||||||
* ``--no-modify-config``\ : Do not modify the existing config file (\ ``~/.bash_profile`` or ``~/.bashrc``\ ).
|
* ``--no-modify-config``\ : Do not modify the existing config file (\ ``~/.bash_profile`` or ``~/.bashrc``\ ).
|
||||||
|
* ``--append-to-config``\ : Back up existing config file and append bash-it templates at the end.
|
||||||
|
|
||||||
When run without the ``--interactive`` switch, Bash-it only enables a sane default set of functionality to keep your shell clean and to avoid issues with missing dependencies.
|
When run without the ``--interactive`` switch, Bash-it only enables a sane default set of functionality to keep your shell clean and to avoid issues with missing dependencies.
|
||||||
Feel free to enable the tools you want to use after the installation.
|
Feel free to enable the tools you want to use after the installation.
|
||||||
|
|
|
||||||
38
install.sh
38
install.sh
|
|
@ -10,6 +10,7 @@ function show_usage() {
|
||||||
echo "--silent (-s): Install default settings without prompting for input";
|
echo "--silent (-s): Install default settings without prompting for input";
|
||||||
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"
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,15 +57,27 @@ function load_some() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Back up existing profile and create new one for bash-it
|
# Back up existing profile
|
||||||
function backup_new() {
|
function backup() {
|
||||||
test -w "$HOME/$CONFIG_FILE" &&
|
test -w "$HOME/$CONFIG_FILE" &&
|
||||||
cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" &&
|
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"
|
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
|
||||||
sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE"
|
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"
|
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
|
||||||
|
(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"
|
||||||
|
}
|
||||||
|
|
||||||
for param in "$@"; do
|
for param in "$@"; do
|
||||||
shift
|
shift
|
||||||
case "$param" in
|
case "$param" in
|
||||||
|
|
@ -72,18 +85,20 @@ for param in "$@"; do
|
||||||
"--silent") set -- "$@" "-s" ;;
|
"--silent") set -- "$@" "-s" ;;
|
||||||
"--interactive") set -- "$@" "-i" ;;
|
"--interactive") set -- "$@" "-i" ;;
|
||||||
"--no-modify-config") set -- "$@" "-n" ;;
|
"--no-modify-config") set -- "$@" "-n" ;;
|
||||||
|
"--append-to-config") set -- "$@" "-a" ;;
|
||||||
*) set -- "$@" "$param"
|
*) set -- "$@" "$param"
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
OPTIND=1
|
OPTIND=1
|
||||||
while getopts "hsin" opt
|
while getopts "hsina" opt
|
||||||
do
|
do
|
||||||
case "$opt" in
|
case "$opt" in
|
||||||
"h") show_usage; exit 0 ;;
|
"h") show_usage; exit 0 ;;
|
||||||
"s") silent=true ;;
|
"s") silent=true ;;
|
||||||
"i") interactive=true ;;
|
"i") interactive=true ;;
|
||||||
"n") no_modify_config=true ;;
|
"n") no_modify_config=true ;;
|
||||||
|
"a") append_to_config=true ;;
|
||||||
"?") show_usage >&2; exit 1 ;;
|
"?") show_usage >&2; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
@ -94,6 +109,11 @@ if [[ $silent ]] && [[ $interactive ]]; then
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ $no_modify_config ]] && [[ $append_to_config ]]; then
|
||||||
|
echo -e "\033[91mOptions --no-modify-config and --append-to-config are mutually exclusive. Please choose one or the other.\033[m"
|
||||||
|
exit 1;
|
||||||
|
fi
|
||||||
|
|
||||||
BASH_IT="$(cd "$(dirname "$0")" && pwd)"
|
BASH_IT="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
|
||||||
case $OSTYPE in
|
case $OSTYPE in
|
||||||
|
|
@ -127,16 +147,11 @@ if ! [[ $silent ]] && ! [[ $no_modify_config ]]; then
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
while ! [ $silent ]; 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
|
||||||
[yY])
|
[yY])
|
||||||
test -w "$HOME/$CONFIG_FILE" &&
|
backup_append
|
||||||
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" | tail -n +2) >> "$HOME/$CONFIG_FILE"
|
|
||||||
echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m"
|
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
[nN]|"")
|
[nN]|"")
|
||||||
|
|
@ -148,6 +163,9 @@ if ! [[ $silent ]] && ! [[ $no_modify_config ]]; then
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
elif [[ $append_to_config ]]; then
|
||||||
|
# backup/append
|
||||||
|
backup_append
|
||||||
elif [[ $silent ]] && ! [[ $no_modify_config ]]; then
|
elif [[ $silent ]] && ! [[ $no_modify_config ]]; then
|
||||||
# backup/new by default
|
# backup/new by default
|
||||||
backup_new
|
backup_new
|
||||||
|
|
|
||||||
|
|
@ -59,3 +59,28 @@ function local_setup {
|
||||||
|
|
||||||
assert_failure
|
assert_failure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "install: verify that no-modify-config and append-to-config can not be used at the same time" {
|
||||||
|
cd "$BASH_IT"
|
||||||
|
|
||||||
|
run ./install.sh --silent --no-modify-config --append-to-config
|
||||||
|
|
||||||
|
assert_failure
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install: verify that the template is appended" {
|
||||||
|
cd "$BASH_IT"
|
||||||
|
|
||||||
|
touch "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
|
||||||
|
echo "test file content" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
|
||||||
|
|
||||||
|
./install.sh --silent --append-to-config
|
||||||
|
|
||||||
|
assert_file_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
|
||||||
|
assert_file_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak"
|
||||||
|
|
||||||
|
run cat $BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE
|
||||||
|
|
||||||
|
assert_line "test file content"
|
||||||
|
assert_line "source \"\$BASH_IT\"/bash_it.sh"
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue