From ad8f2dd7bc7182d41454481cea49f9316d4a6fed Mon Sep 17 00:00:00 2001 From: eshanized Date: Sun, 22 Dec 2024 01:38:04 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf(=5Fimprove):=20add=20?= =?UTF-8?q?verbose=20output=20&&=20dry=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/local/bin/snigdhaos-uninstall-package.sh | 133 +++++++++++++------ 1 file changed, 92 insertions(+), 41 deletions(-) diff --git a/usr/local/bin/snigdhaos-uninstall-package.sh b/usr/local/bin/snigdhaos-uninstall-package.sh index 36ef5d7..cc3fca4 100755 --- a/usr/local/bin/snigdhaos-uninstall-package.sh +++ b/usr/local/bin/snigdhaos-uninstall-package.sh @@ -1,82 +1,133 @@ #!/bin/bash -# Author : Eshan Roy -# Author URL : https://eshanized.github.io/ +# author : eshan roy +# 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" GREEN="\033[1;32m" YELLOW="\033[1;33m" BLUE="\033[1;34m" 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() { - 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 "\n${RED}๐Ÿšจ an error occurred during the uninstallation process.${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 } -# Trap errors and call error_handler +# trap errors and call error_handler trap 'error_handler $LINENO' ERR -# Function to display help message +# function to display help message show_help() { - echo -e "${BLUE}Usage: ${RESET}$0 " + echo -e "${BLUE}usage: ${RESET}$0 [options]" 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 "${YELLOW}Options:${RESET}" - echo -e " ${GREEN}-h, --help${RESET} Show this help message." - echo -e " ${GREEN}${RESET} The package to uninstall." + echo -e "${YELLOW}options:${RESET}" + echo -e " ${GREEN}-h, --help${RESET} show this help message." + 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 "${YELLOW}Example usage:${RESET}" - echo -e " $0 firefox # Uninstalls the Firefox package and its cache." - echo -e " $0 -h # Displays this help message." + echo -e "${YELLOW}example usage:${RESET}" + echo -e " $0 firefox # uninstalls the firefox package and its cache." + 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 -if [[ "$1" == "-h" || "$1" == "--help" ]]; then - show_help - exit 0 -fi +# parse command line options +DRY_RUN=false +FORCE=false +BACKUP=false -# 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 - 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 fi -# Check if a package name is provided +# check if a package name is provided if [ -z "$1" ]; then - echo -e "${RED}โŒ No package specified.${RESET}" - echo -e "Usage: $0 " + echo -e "${RED}โŒ no package specified.${RESET}" + echo -e "usage: $0 " exit 1 fi PACKAGE=$1 -# 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 +# check if package is installed +if ! pacman -Qi $PACKAGE > /dev/null; then + 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 - echo -e "${RED}โŒ Operation cancelled.${RESET}" + echo -e "${RED}โŒ operation cancelled.${RESET}" exit 0 fi -# 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; } +# backup configuration files (optional) +if $BACKUP; then + 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 -echo -e "${BLUE}๐Ÿ—‘๏ธ Removing cache for $PACKAGE...${RESET}" -sudo pacman -Sc --noconfirm || echo -e "${YELLOW}โ— Cache removal skipped.${RESET}" +# dry-run mode (only show what would be done) +if $DRY_RUN; then + echo -e "${YELLOW}dry-run mode enabled: the following actions would be performed.${RESET}" + pacman -Qi $PACKAGE + echo -e "${BLUE}would uninstall $PACKAGE, remove its cache, and clean orphaned packages.${RESET}" + exit 0 +fi -# 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}" +# 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 -echo -e "\n${GREEN}โœ… Package $PACKAGE successfully uninstalled!${RESET}" +# 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