From dcef4dbda8961da9a3178a73d2eb5c1f0aa20dd6 Mon Sep 17 00:00:00 2001 From: ashishsinghbora <135435891+ashishsinghbora@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:26:49 +0530 Subject: [PATCH] fix(packages): use arrays for package commands, add dry-run, and support apk --- scripts/packages/install-package.sh | 91 +++++++++++++++++------- scripts/packages/package-info.sh | 54 +++++++++----- scripts/packages/package-manager-info.sh | 30 +++++--- scripts/packages/remove-package.sh | 91 +++++++++++++++++------- scripts/packages/search-package.sh | 69 +++++++++++------- scripts/packages/update-system.sh | 50 +++++++++---- 6 files changed, 264 insertions(+), 121 deletions(-) diff --git a/scripts/packages/install-package.sh b/scripts/packages/install-package.sh index 6bf2e87..ee0e748 100755 --- a/scripts/packages/install-package.sh +++ b/scripts/packages/install-package.sh @@ -3,65 +3,102 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [options] +Usage: install-package.sh [options] Options: - -h Show this help message and exit - -y Automatically answer yes to any prompts (use with caution) + -h, --help Show this help message and exit + -y, --yes Automatically answer yes to any prompts + -n, --dry-run Show the command that would be run without installing -Installs the specified package using the detected package manager. -The script will prompt for confirmation unless -y is provided. +Description: + Installs the specified package using the detected package manager + (pacman, apt, dnf, zypper, apk). Prompts for confirmation unless + -y or --yes is provided. EOF exit 0 } -# Default values AUTOYES=false +DRY_RUN=false +PACKAGE="" -# Parse options -while getopts "hy" opt; do - case "$opt" in - h) show_help ;; - y) AUTOYES=true ;; - *) show_help ;; +while (( "$#" )); do + case "$1" in + -h|--help) + show_help + ;; + -y|--yes) + AUTOYES=true + ;; + -n|--dry-run) + DRY_RUN=true + ;; + -*) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + *) + if [[ -z "$PACKAGE" ]]; then + PACKAGE="$1" + else + echo "Error: Unexpected additional argument: $1" >&2 + exit 1 + fi + ;; esac + shift done -shift $((OPTIND -1)) - -if [[ $# -lt 1 ]]; then - echo "Error: package name required." - show_help +if [[ -z "$PACKAGE" ]]; then + echo "Error: package name required." >&2 + echo "Run '$0 --help' for usage." >&2 + exit 1 fi -PACKAGE="$1" +declare -a INSTALL_CMD=() +PM="" -# Detect package manager if command -v pacman >/dev/null 2>&1; then PM="pacman" - INSTALL_CMD="pacman -S --noconfirm" + INSTALL_CMD=(pacman -S --noconfirm) +elif command -v apt-get >/dev/null 2>&1; then + PM="apt" + INSTALL_CMD=(apt-get install -y) elif command -v apt >/dev/null 2>&1; then PM="apt" - INSTALL_CMD="apt install -y" + INSTALL_CMD=(apt install -y) elif command -v dnf >/dev/null 2>&1; then PM="dnf" - INSTALL_CMD="dnf install -y" + INSTALL_CMD=(dnf install -y) elif command -v zypper >/dev/null 2>&1; then PM="zypper" - INSTALL_CMD="zypper install -y" + INSTALL_CMD=(zypper install -y) +elif command -v apk >/dev/null 2>&1; then + PM="apk" + INSTALL_CMD=(apk add) else - echo "Unsupported package manager." + echo "Error: Unsupported or no package manager detected." >&2 exit 1 fi echo "Detected package manager: $PM" +if $DRY_RUN; then + echo "[DRY RUN] sudo ${INSTALL_CMD[*]} $PACKAGE" + exit 0 +fi + if $AUTOYES; then - sudo "$INSTALL_CMD" "$PACKAGE" + sudo "${INSTALL_CMD[@]}" "$PACKAGE" else read -r -p "Proceed to install '$PACKAGE' using $PM? [y/N] " resp case "$resp" in - y|Y|yes|YES) sudo "$INSTALL_CMD" "$PACKAGE" ;; - *) echo "Installation aborted."; exit 0 ;; + y|Y|yes|YES) + sudo "${INSTALL_CMD[@]}" "$PACKAGE" + ;; + *) + echo "Installation aborted." + exit 0 + ;; esac fi diff --git a/scripts/packages/package-info.sh b/scripts/packages/package-info.sh index db80330..855713c 100755 --- a/scripts/packages/package-info.sh +++ b/scripts/packages/package-info.sh @@ -3,43 +3,61 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [options] +Usage: package-info.sh [options] Options: - -h Show this help message and exit + -h, --help Show this help message and exit -Shows detailed information about the specified package using the detected -package manager. The script does not modify the system. +Description: + Shows detailed information about the specified package using the detected + package manager (pacman, apt, dnf, zypper, apk). EOF exit 0 } -# Parse options -while getopts "h" opt; do - case "$opt" in - h) show_help ;; - *) show_help ;; +PKG="" + +while (( "$#" )); do + case "$1" in + -h|--help) + show_help + ;; + -*) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + *) + if [[ -z "$PKG" ]]; then + PKG="$1" + else + echo "Error: Unexpected additional argument: $1" >&2 + exit 1 + fi + ;; esac + shift done -shift $((OPTIND-1)) - -if [[ $# -lt 1 ]]; then - echo "Error: package name required." - show_help +if [[ -z "$PKG" ]]; then + echo "Error: package name required." >&2 + echo "Run '$0 --help' for usage." >&2 + exit 1 fi -PKG="$1" - if command -v pacman >/dev/null 2>&1; then - pacman -Qi "$PKG" + # Try local query first, fallback to sync database if not installed + pacman -Qi "$PKG" 2>/dev/null || pacman -Si "$PKG" elif command -v apt >/dev/null 2>&1; then apt show "$PKG" +elif command -v apt-cache >/dev/null 2>&1; then + apt-cache show "$PKG" elif command -v dnf >/dev/null 2>&1; then dnf info "$PKG" elif command -v zypper >/dev/null 2>&1; then zypper info "$PKG" +elif command -v apk >/dev/null 2>&1; then + apk info -d "$PKG" else - echo "Unsupported package manager." + echo "Error: Unsupported or no package manager detected." >&2 exit 1 fi diff --git a/scripts/packages/package-manager-info.sh b/scripts/packages/package-manager-info.sh index d1169bb..5b4c521 100755 --- a/scripts/packages/package-manager-info.sh +++ b/scripts/packages/package-manager-info.sh @@ -3,21 +3,27 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [options] +Usage: package-manager-info.sh [options] Options: - -h Show this help message and exit + -h, --help Show this help message and exit -Detects the system's package manager (pacman, apt, dnf, zypper) and prints -basic information about it (name, version, and a brief description). +Description: + Detects the system's package manager (pacman, apt, dnf, zypper, apk) + and prints basic information about it (name and version). EOF exit 0 } -while getopts "h" opt; do - case "$opt" in - h) show_help ;; - *) show_help ;; +for arg in "$@"; do + case "$arg" in + -h|--help) + show_help + ;; + *) + echo "Error: Unknown option: $arg" >&2 + exit 1 + ;; esac done @@ -27,13 +33,19 @@ if command -v pacman >/dev/null 2>&1; then elif command -v apt >/dev/null 2>&1; then echo "Package manager: apt (Debian/Ubuntu)" apt --version | head -n1 +elif command -v apt-get >/dev/null 2>&1; then + echo "Package manager: apt-get (Debian/Ubuntu)" + apt-get --version | head -n1 elif command -v dnf >/dev/null 2>&1; then echo "Package manager: dnf (Fedora/RHEL)" dnf --version | head -n1 elif command -v zypper >/dev/null 2>&1; then echo "Package manager: zypper (openSUSE)" zypper --version | head -n1 +elif command -v apk >/dev/null 2>&1; then + echo "Package manager: apk (Alpine Linux)" + apk --version | head -n1 else - echo "No supported package manager detected." + echo "Error: No supported package manager detected." >&2 exit 1 fi diff --git a/scripts/packages/remove-package.sh b/scripts/packages/remove-package.sh index b9ffb03..7c5d9fc 100755 --- a/scripts/packages/remove-package.sh +++ b/scripts/packages/remove-package.sh @@ -3,65 +3,102 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [options] +Usage: remove-package.sh [options] Options: - -h Show this help message and exit - -y Automatically answer yes to any prompts (use with caution) + -h, --help Show this help message and exit + -y, --yes Automatically answer yes to any prompts + -n, --dry-run Show the command that would be run without removing -Removes the specified package using the detected package manager. -The script will prompt for confirmation unless -y is provided. +Description: + Removes the specified package using the detected package manager + (pacman, apt, dnf, zypper, apk). Prompts for confirmation unless + -y or --yes is provided. EOF exit 0 } -# Default values AUTOYES=false +DRY_RUN=false +PACKAGE="" -# Parse options -while getopts "hy" opt; do - case "$opt" in - h) show_help ;; - y) AUTOYES=true ;; - *) show_help ;; +while (( "$#" )); do + case "$1" in + -h|--help) + show_help + ;; + -y|--yes) + AUTOYES=true + ;; + -n|--dry-run) + DRY_RUN=true + ;; + -*) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + *) + if [[ -z "$PACKAGE" ]]; then + PACKAGE="$1" + else + echo "Error: Unexpected additional argument: $1" >&2 + exit 1 + fi + ;; esac + shift done -shift $((OPTIND -1)) - -if [[ $# -lt 1 ]]; then - echo "Error: package name required." - show_help +if [[ -z "$PACKAGE" ]]; then + echo "Error: package name required." >&2 + echo "Run '$0 --help' for usage." >&2 + exit 1 fi -PACKAGE="$1" +declare -a REMOVE_CMD=() +PM="" -# Detect package manager if command -v pacman >/dev/null 2>&1; then PM="pacman" - REMOVE_CMD="pacman -R --noconfirm" + REMOVE_CMD=(pacman -R --noconfirm) +elif command -v apt-get >/dev/null 2>&1; then + PM="apt" + REMOVE_CMD=(apt-get remove -y) elif command -v apt >/dev/null 2>&1; then PM="apt" - REMOVE_CMD="apt remove -y" + REMOVE_CMD=(apt remove -y) elif command -v dnf >/dev/null 2>&1; then PM="dnf" - REMOVE_CMD="dnf remove -y" + REMOVE_CMD=(dnf remove -y) elif command -v zypper >/dev/null 2>&1; then PM="zypper" - REMOVE_CMD="zypper remove -y" + REMOVE_CMD=(zypper remove -y) +elif command -v apk >/dev/null 2>&1; then + PM="apk" + REMOVE_CMD=(apk del) else - echo "Unsupported package manager." + echo "Error: Unsupported or no package manager detected." >&2 exit 1 fi echo "Detected package manager: $PM" +if $DRY_RUN; then + echo "[DRY RUN] sudo ${REMOVE_CMD[*]} $PACKAGE" + exit 0 +fi + if $AUTOYES; then - sudo "$REMOVE_CMD" "$PACKAGE" + sudo "${REMOVE_CMD[@]}" "$PACKAGE" else read -r -p "Proceed to remove '$PACKAGE' using $PM? [y/N] " resp case "$resp" in - y|Y|yes|YES) sudo "$REMOVE_CMD" "$PACKAGE" ;; - *) echo "Removal aborted."; exit 0 ;; + y|Y|yes|YES) + sudo "${REMOVE_CMD[@]}" "$PACKAGE" + ;; + *) + echo "Removal aborted." + exit 0 + ;; esac fi diff --git a/scripts/packages/search-package.sh b/scripts/packages/search-package.sh index 97bc6d2..ae0653e 100755 --- a/scripts/packages/search-package.sh +++ b/scripts/packages/search-package.sh @@ -3,47 +3,66 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [options] +Usage: search-package.sh [options] Options: - -h Show this help message and exit + -h, --help Show this help message and exit -Searches the package repositories of the detected package manager for the given term. -The search is performed non‑interactively and does not modify the system. +Description: + Searches the package repositories of the detected package manager + (pacman, apt, dnf, zypper, apk) for the given term. EOF exit 0 } -# Parse options -while getopts "h" opt; do - case "$opt" in - h) show_help ;; - *) show_help ;; +SEARCH_TERM="" + +while (( "$#" )); do + case "$1" in + -h|--help) + show_help + ;; + -*) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + *) + if [[ -z "$SEARCH_TERM" ]]; then + SEARCH_TERM="$1" + else + echo "Error: Unexpected additional argument: $1" >&2 + exit 1 + fi + ;; esac + shift done -shift $((OPTIND-1)) - -if [[ $# -lt 1 ]]; then - echo "Error: search term required." - show_help +if [[ -z "$SEARCH_TERM" ]]; then + echo "Error: search term required." >&2 + echo "Run '$0 --help' for usage." >&2 + exit 1 fi -TERM="$1" - if command -v pacman >/dev/null 2>&1; then - echo "Searching with pacman for '$TERM'..." - pacman -Ss "$TERM" | grep -v "^\s*#" + echo "Searching with pacman for '$SEARCH_TERM'..." + pacman -Ss "$SEARCH_TERM" elif command -v apt-cache >/dev/null 2>&1; then - echo "Searching with apt for '$TERM'..." - apt-cache search "$TERM" + echo "Searching with apt-cache for '$SEARCH_TERM'..." + apt-cache search "$SEARCH_TERM" +elif command -v apt >/dev/null 2>&1; then + echo "Searching with apt for '$SEARCH_TERM'..." + apt search "$SEARCH_TERM" elif command -v dnf >/dev/null 2>&1; then - echo "Searching with dnf for '$TERM'..." - dnf search "$TERM" + echo "Searching with dnf for '$SEARCH_TERM'..." + dnf search "$SEARCH_TERM" elif command -v zypper >/dev/null 2>&1; then - echo "Searching with zypper for '$TERM'..." - zypper search "$TERM" + echo "Searching with zypper for '$SEARCH_TERM'..." + zypper search "$SEARCH_TERM" +elif command -v apk >/dev/null 2>&1; then + echo "Searching with apk for '$SEARCH_TERM'..." + apk search "$SEARCH_TERM" else - echo "Unsupported package manager." + echo "Error: Unsupported or no package manager detected." >&2 exit 1 fi diff --git a/scripts/packages/update-system.sh b/scripts/packages/update-system.sh index 21eaefd..a0fd971 100755 --- a/scripts/packages/update-system.sh +++ b/scripts/packages/update-system.sh @@ -3,32 +3,44 @@ set -euo pipefail show_help() { cat <<'EOF' -Usage: $(basename "$0") [OPTIONS] +Usage: update-system.sh [options] Options: - -h Show this help message and exit - -n Dry‑run mode: display the commands that would be run without executing them + -h, --help Show this help message and exit + -n, --dry-run Dry-run mode: display commands without executing them -Updates the system using the detected package manager (pacman, apt, dnf, zypper). +Description: + Updates the system using the detected package manager + (pacman, apt, dnf, zypper, apk). EOF exit 0 } -# Default values DRY_RUN=false -# Parse options -while getopts "hn" opt; do - case "$opt" in - h) show_help ;; - n) DRY_RUN=true ;; - *) show_help ;; +while (( "$#" )); do + case "$1" in + -h|--help) + show_help + ;; + -n|--dry-run) + DRY_RUN=true + ;; + -*) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + *) + echo "Error: Unexpected argument: $1" >&2 + exit 1 + ;; esac + shift done run_cmd() { if $DRY_RUN; then - echo "[DRY RUN] $*" + echo "[DRY RUN] sudo $*" else sudo "$@" fi @@ -37,7 +49,11 @@ run_cmd() { # Detect package manager and perform update if command -v pacman >/dev/null 2>&1; then echo "Detected Arch Linux (pacman)" - run_cmd pacman -Syu + run_cmd pacman -Syu --noconfirm +elif command -v apt-get >/dev/null 2>&1; then + echo "Detected Debian/Ubuntu (apt-get)" + run_cmd apt-get update + run_cmd apt-get upgrade -y elif command -v apt >/dev/null 2>&1; then echo "Detected Debian/Ubuntu (apt)" run_cmd apt update @@ -48,8 +64,12 @@ elif command -v dnf >/dev/null 2>&1; then elif command -v zypper >/dev/null 2>&1; then echo "Detected openSUSE (zypper)" run_cmd zypper refresh - run_cmd zypper update + run_cmd zypper update -y +elif command -v apk >/dev/null 2>&1; then + echo "Detected Alpine Linux (apk)" + run_cmd apk update + run_cmd apk upgrade else - echo "Unsupported package manager." + echo "Error: Unsupported or no package manager detected." >&2 exit 1 fi