sconnect/lib/pm
2024-06-06 02:00:49 +02:00

135 lines
2.8 KiB
Bash

#!/bin/sh
# sc.requires: logger
pm_detect_package_manager() {
if command -v apt >/dev/null; then
echo apt
elif command -v pacman >/dev/null; then
echo pacman
elif command -v dnf >/dev/null; then
echo dnf
elif command -v yum >/dev/null; then
echo yum
elif command -v zypper >/dev/null; then
echo zypper
elif command -v brew >/dev/null; then
echo brew
elif command -v apk >/dev/null; then
echo apk
elif command -v nix-env >/dev/null; then
echo nix
else
fatal_log "no supported package manager found"
exit 1
fi
}
SC_PACKAGE_MANAGER="${SC_PACKAGE_MANAGER:-$(pm_detect_package_manager)}"
pm_upgrade_packages() {
LOGI "Upgrading packages using ${SC_PACKAGE_MANAGER}..."
case "$SC_PACKAGE_MANAGER" in
apt)
sudo apt update && sudo apt upgrade -y
;;
pacman)
sudo pacman -Syu
;;
dnf)
sudo dnf upgrade --refresh -y
;;
yum)
sudo yum update -y
;;
zypper)
sudo zypper refresh && sudo zypper update -y
;;
brew)
brew update && brew upgrade
;;
apk)
sudo apk update && sudo apk upgrade
;;
nix)
nix-env --upgrade
;;
*)
fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}"
exit 1
;;
esac
}
pm_install_packages() {
LOGI "Installing packages using ${SC_PACKAGE_MANAGER}..."
packages="$@"
case "$SC_PACKAGE_MANAGER" in
apt)
sudo apt install -y $packages
;;
pacman)
sudo pacman -S --noconfirm $packages
;;
dnf)
sudo dnf install -y $packages
;;
yum)
sudo yum install -y $packages
;;
zypper)
sudo zypper install -y $packages
;;
brew)
brew install $packages
;;
apk)
sudo apk add $packages
;;
nix)
nix-env -i $packages
;;
*)
fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}"
exit 1
;;
esac
}
pm_remove_packages() {
LOGI "Removing packages using ${SC_PACKAGE_MANAGER}..."
packages="$@"
case "$SC_PACKAGE_MANAGER" in
apt)
sudo apt remove -y $packages
;;
pacman)
sudo pacman -Rns --noconfirm $packages
;;
dnf)
sudo dnf remove -y $packages
;;
yum)
sudo yum remove -y $packages
;;
zypper)
sudo zypper remove -y $packages
;;
brew)
brew uninstall $packages
;;
apk)
sudo apk del $packages
;;
nix)
nix-env -e $packages
;;
*)
fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}"
exit 1
;;
esac
}