optional backup when cleaning
This commit is contained in:
56
clean.sh
56
clean.sh
@@ -2,25 +2,46 @@
|
|||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
TAG=.bak-$(date +%Y%m%d)
|
|
||||||
|
|
||||||
SRC_DIRS=(
|
SRC_DIRS=(
|
||||||
"$HOME/.local/share/nvim"
|
"$HOME/.local/share/nvim"
|
||||||
"$HOME/.local/state/nvim"
|
"$HOME/.local/state/nvim"
|
||||||
"$HOME/.cache/nvim"
|
"$HOME/.cache/nvim"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Show plan and confirm
|
# Ask if user wants to backup
|
||||||
printf "This will back up and clean your Neovim directories.\n"
|
printf "Do you want to backup before cleaning? [Y/n]: "
|
||||||
printf "Backup suffix to be appended: %s\n\n" "$TAG"
|
read -r BACKUP_CHOICE
|
||||||
|
case ${BACKUP_CHOICE:-} in
|
||||||
|
[nN]|[nN][oO])
|
||||||
|
BACKUP=false
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
BACKUP=true
|
||||||
|
TAG=.bak-$(date +%Y-%m-%d_%H-%M-%S)
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
printf "Plan:\n"
|
# Show plan and confirm
|
||||||
for SRC in "${SRC_DIRS[@]}"; do
|
printf "\n"
|
||||||
if [ -e "$SRC" ]; then
|
if [ "$BACKUP" = true ]; then
|
||||||
DEST="${SRC}${TAG}"
|
printf "This will back up and clean your Neovim directories.\n"
|
||||||
printf " %s -> %s\n" "$SRC" "$DEST"
|
printf "Backup suffix to be appended: %s\n\n" "$TAG"
|
||||||
fi
|
printf "Plan:\n"
|
||||||
done
|
for SRC in "${SRC_DIRS[@]}"; do
|
||||||
|
if [ -e "$SRC" ]; then
|
||||||
|
DEST="${SRC}${TAG}"
|
||||||
|
printf " %s ➡️ %s\n" "$SRC" "$DEST"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
printf "This will remove your Neovim directories WITHOUT backup.\n\n"
|
||||||
|
printf "Plan:\n"
|
||||||
|
for SRC in "${SRC_DIRS[@]}"; do
|
||||||
|
if [ -e "$SRC" ]; then
|
||||||
|
printf " %s ❌\n" "$SRC"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
read -r -p "Are you sure you want to proceed? [y/N]: " CONFIRM
|
read -r -p "Are you sure you want to proceed? [y/N]: " CONFIRM
|
||||||
@@ -29,12 +50,15 @@ case ${CONFIRM:-} in
|
|||||||
*) printf "Aborted!\n"; exit 0;;
|
*) printf "Aborted!\n"; exit 0;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Perform moves, skipping missing sources
|
# Perform action based on backup choice
|
||||||
for SRC in "${SRC_DIRS[@]}"; do
|
for SRC in "${SRC_DIRS[@]}"; do
|
||||||
if [ -e "$SRC" ]; then
|
if [ -e "$SRC" ]; then
|
||||||
DEST="${SRC}${TAG}"
|
if [ "$BACKUP" = true ]; then
|
||||||
printf "Moving %s -> %s\n" "$SRC" "$DEST"
|
DEST="${SRC}${TAG}"
|
||||||
mv "$SRC" "$DEST"
|
mv "$SRC" "$DEST"
|
||||||
|
else
|
||||||
|
rm -rf "$SRC"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user