️ perf(_improve): add verbose output && dry run

This commit is contained in:
eshanized
2024-12-22 01:38:04 +05:30
parent ab69aa5d51
commit ad8f2dd7bc

View File

@@ -1,82 +1,133 @@
#!/bin/bash #!/bin/bash
# Author : Eshan Roy # author : eshan roy
# Author URL : https://eshanized.github.io/ # author url : https://eshanized.github.io/
set -e # Exit immediately if a command exits with a non-zero status set -e # exit immediately if a command exits with a non-zero status
# Colors # colors
RED="\033[1;31m" RED="\033[1;31m"
GREEN="\033[1;32m" GREEN="\033[1;32m"
YELLOW="\033[1;33m" YELLOW="\033[1;33m"
BLUE="\033[1;34m" BLUE="\033[1;34m"
RESET="\033[0m" RESET="\033[0m"
# Function to display error message and exit # log file path
LOG_FILE="/var/log/package_uninstall.log"
# function to display error message and exit
error_handler() { error_handler() {
echo -e "\n${RED}🚨 An error occurred during the uninstallation process.${RESET}" echo -e "\n${RED}🚨 an error occurred during the uninstallation process.${RESET}"
echo -e "${RED}Error on line $1. Please check your system and try again.${RESET}" echo -e "${RED}command: $BASH_COMMAND${RESET}"
echo -e "${RED}error on line $1. please check your system and try again.${RESET}"
echo -e "$(date) - ERROR: an error occurred during the uninstallation process for $PACKAGE" >> $LOG_FILE
exit 1 exit 1
} }
# Trap errors and call error_handler # trap errors and call error_handler
trap 'error_handler $LINENO' ERR trap 'error_handler $LINENO' ERR
# Function to display help message # function to display help message
show_help() { show_help() {
echo -e "${BLUE}Usage: ${RESET}$0 <package_name>" echo -e "${BLUE}usage: ${RESET}$0 <package_name> [options]"
echo -e "" echo -e ""
echo -e "${GREEN}This script will uninstall the specified package, remove its cache, and clean up orphaned packages.${RESET}" echo -e "${GREEN}this script will uninstall the specified package, remove its cache, and clean up orphaned packages.${RESET}"
echo -e "" echo -e ""
echo -e "${YELLOW}Options:${RESET}" echo -e "${YELLOW}options:${RESET}"
echo -e " ${GREEN}-h, --help${RESET} Show this help message." echo -e " ${GREEN}-h, --help${RESET} show this help message."
echo -e " ${GREEN}<package_name>${RESET} The package to uninstall." echo -e " ${GREEN}-d, --dry-run${RESET} show what will be done without performing any actions."
echo -e " ${GREEN}-f, --force${RESET} force removal of the package even if it has dependencies."
echo -e " ${GREEN}-b, --backup${RESET} backup configuration files before uninstallation."
echo -e "" echo -e ""
echo -e "${YELLOW}Example usage:${RESET}" echo -e "${YELLOW}example usage:${RESET}"
echo -e " $0 firefox # Uninstalls the Firefox package and its cache." echo -e " $0 firefox # uninstalls the firefox package and its cache."
echo -e " $0 -h # Displays this help message." echo -e " $0 -d firefox # show the dry-run actions for firefox."
echo -e " $0 -f firefox # force remove firefox package."
echo -e " $0 -b firefox # backup configuration before uninstalling firefox."
echo -e " $0 -h # displays this help message."
} }
# Parse command line options # parse command line options
if [[ "$1" == "-h" || "$1" == "--help" ]]; then DRY_RUN=false
show_help FORCE=false
exit 0 BACKUP=false
fi
# Ensure the script is run as root for certain actions while [[ "$1" == -* ]]; do
case "$1" in
-h|--help) show_help; exit 0 ;;
-d|--dry-run) DRY_RUN=true; shift ;;
-f|--force) FORCE=true; shift ;;
-b|--backup) BACKUP=true; shift ;;
*) echo -e "${RED}❌ invalid option $1.${RESET}"; show_help; exit 1 ;;
esac
done
# ensure the script is run as root for certain actions
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Please run this script as root or with sudo.${RESET}" echo -e "${RED}please run this script as root or with sudo.${RESET}"
exit 1 exit 1
fi fi
# Check if a package name is provided # check if a package name is provided
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo -e "${RED}No package specified.${RESET}" echo -e "${RED}no package specified.${RESET}"
echo -e "Usage: $0 <package_name>" echo -e "usage: $0 <package_name>"
exit 1 exit 1
fi fi
PACKAGE=$1 PACKAGE=$1
# Confirm with the user # check if package is installed
echo -e "${YELLOW}⚠️ WARNING: This script will uninstall the specified package and remove its cache.${RESET}" if ! pacman -Qi $PACKAGE > /dev/null; then
read -p "$(echo -e "${BLUE}Are you sure you want to uninstall $PACKAGE and remove its cache? [y/N]: ${RESET}")" CONFIRM echo -e "${RED}❌ the package $PACKAGE is not installed on your system.${RESET}"
exit 1
fi
# confirm with the user
echo -e "${YELLOW}⚠️ warning: this script will uninstall the specified package and remove its cache.${RESET}"
read -p "$(echo -e "${BLUE}are you sure you want to uninstall $PACKAGE and remove its cache? [y/N]: ${RESET}")" CONFIRM
if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
echo -e "${RED}Operation cancelled.${RESET}" echo -e "${RED}operation cancelled.${RESET}"
exit 0 exit 0
fi fi
# Uninstall the package and its dependencies # backup configuration files (optional)
echo -e "${BLUE}📦 Uninstalling $PACKAGE...${RESET}" if $BACKUP; then
sudo pacman -Rns $PACKAGE --noconfirm || { echo -e "${RED}❌ Failed to uninstall $PACKAGE.${RESET}"; exit 1; } echo -e "${BLUE}🔄 backing up configuration files for $PACKAGE...${RESET}"
backup_dir="/path/to/backup/$PACKAGE"
mkdir -p "$backup_dir"
cp /etc/$PACKAGE/* "$backup_dir" || { echo -e "${RED}❌ backup failed.${RESET}"; exit 1; }
echo -e "${GREEN}✅ backup completed!${RESET}"
fi
# Remove the package's cache # dry-run mode (only show what would be done)
echo -e "${BLUE}🗑️ Removing cache for $PACKAGE...${RESET}" if $DRY_RUN; then
sudo pacman -Sc --noconfirm || echo -e "${YELLOW}❗ Cache removal skipped.${RESET}" echo -e "${YELLOW}dry-run mode enabled: the following actions would be performed.${RESET}"
pacman -Qi $PACKAGE
# Clean up orphaned packages echo -e "${BLUE}would uninstall $PACKAGE, remove its cache, and clean orphaned packages.${RESET}"
echo -e "${BLUE}🧹 Cleaning up orphaned packages...${RESET}" exit 0
sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo -e "${YELLOW}No orphaned packages to remove.${RESET}" fi
echo -e "\n${GREEN}✅ Package $PACKAGE successfully uninstalled!${RESET}" # force removal option
if $FORCE; then
echo -e "${YELLOW}⚠️ force removal enabled for $PACKAGE.${RESET}"
sudo pacman -Rdd $PACKAGE --noconfirm || { echo -e "${RED}❌ failed to force remove $PACKAGE.${RESET}"; exit 1; }
else
# uninstall the package and its dependencies
echo -e "${BLUE}📦 uninstalling $PACKAGE...${RESET}"
sudo pacman -Rns $PACKAGE --noconfirm || { echo -e "${RED}❌ failed to uninstall $PACKAGE.${RESET}"; exit 1; }
fi
# remove the package's cache
echo -e "${BLUE}🗑️ removing cache for $PACKAGE...${RESET}"
sudo pacman -Sc --noconfirm || echo -e "${YELLOW}❗ cache removal skipped.${RESET}"
# clean up orphaned packages
echo -e "${BLUE}🧹 cleaning up orphaned packages...${RESET}"
sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo -e "${YELLOW}no orphaned packages to remove.${RESET}"
# log the uninstallation
echo -e "$(date) - successfully uninstalled $PACKAGE" >> $LOG_FILE
echo -e "\n${GREEN}✅ package $PACKAGE successfully uninstalled!${RESET}"
exit 0 exit 0