feat: add utility to copy files and handle diffs based on user input

This commit is contained in:
Marc 2024-02-25 22:26:24 -05:00
parent 1d08869863
commit 64050ab9d3
Signed by: marc
GPG key ID: 048E042F22B5DC79

45
bootstrap.sh Normal file → Executable file
View file

@ -41,6 +41,47 @@ pre_substep() {
echo -e "\e[1m[$STEP_COUNT.$SUBSTEP_COUNT] <====== $1 =======>\e[0m"
}
# Copies a file from $1 to $2.
#
# If $2 exists, checks if $1 == $2 and asks what to do.
# From there, the user providing "O" will overwrite (proceed with copy),
# "S" will skip the file and leave the destination as-is, and anyting else
# will skip the copy altogether (i.e. "S").
copy_file() {
source_path=$1
dest_path=$2
if [ ! -e "$dest_path" ]; then
cp $source_path $dest_path
echo "Copied $source_path > $dest_path"
return 0
fi
difference="$(diff $source_path $dest_path)"
if [ -z "$difference" ]; then
echo "$dest_path already exists, but is the same as $source_path - skipping."
return 0
fi
echo "Differences detected between $source_path and $dest_path:"
diff --color $source_path $dest_path
read -p "$dest_path already exists. What should be done? [O:overwrite,S:skip] " action
if [[ $action == "O" ]]; then
cp $source_path $dest_path
echo "Overwrote $source_path > $dest_path"
return 0
fi
if [[ $action == "S" ]]; then
echo "Skipped and left $dest_path as is."
return 0
fi
echo "Unrecognized action, doing nothing."
}
##########################################################
#
# 2. Steps
@ -113,10 +154,10 @@ install_and_configure_starship() {
pre_substep "Copy configuration in configuration directory"
STARSHIP_CONFIGURATION_PATH=~/.config/starship.toml
if [ -f $STARSHIP_CONFIGURATION_PATH ]; then
cp $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old
copy_file $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old
fi
cp ./files/starship.toml $STARSHIP_CONFIGURATION_PATH
copy_file ./files/starship.toml $STARSHIP_CONFIGURATION_PATH
}
install_nvm() {