1
0
chezmoi/scripts/utils.sh

127 lines
3.4 KiB
Bash
Raw Normal View History

2024-08-03 09:52:03 +00:00
#!/usr/bin/env bash
# Logging function
log() {
printf "$(tput setaf 4)$(tput bold)>>>>> %s <<<<<$(tput sgr0)\n" "$1"
}
inf() {
2024-08-03 10:27:29 +00:00
printf "$(tput setaf 2)╚═══ᐳ $(tput sgr 0 1)$(tput setaf 2)%s$(tput sgr0)\n" "$1"
2024-08-03 09:52:03 +00:00
}
err() {
printf "$(tput setaf 9)$(tput bold)>>>>> %s ! <<<<<$(tput sgr0)\n" "$1"
}
2024-08-03 15:21:54 +00:00
# Function to determine the package manager if not specified
determine_package_manager() {
2024-08-03 15:38:17 +00:00
if [ $(command -v apt-get) ]; then
2024-08-03 15:21:54 +00:00
echo "apt"
2024-08-03 15:38:17 +00:00
elif [ $(command -v pamac) ]; then
2024-08-03 15:21:54 +00:00
echo "pamac"
2024-08-03 15:38:17 +00:00
elif [ $(command -v yay) ]; then
2024-08-03 15:21:54 +00:00
echo "yay"
2024-08-03 15:38:17 +00:00
elif [ $(command -v pacman) ]; then
2024-08-03 15:21:54 +00:00
echo "pacman"
else
err "No supported package manager found."
exit 1
fi
}
# Simple abstraction to install a binary using a supported package manager
2024-08-03 09:52:03 +00:00
install_binary() {
# Example usage:
# install_binary "fzf"
# install_binary "fzf" "dnf"
local package_name="${1}"
local package_manager="${2}"
# If the package manager is not specified, determine it
if [[ -z "${package_manager}" ]]; then
package_manager=$(determine_package_manager)
fi
2024-08-03 15:21:54 +00:00
# Install the package using the determined or specified package manager
inf "installing ${package_name} using ${package_manager}..."
2024-08-03 09:52:03 +00:00
case "${package_manager}" in
2024-08-03 15:21:54 +00:00
apt)
sudo apt-get install --yes --no-install-recommends --ignore-missing --fix-broken -qq "${package_name}" || {
err "Installation failed."
exit 1
}
2024-08-03 09:52:03 +00:00
;;
2024-08-03 15:21:54 +00:00
pacman)
sudo pacman -S --needed --noconfirm --quiet "${package_name}" || {
err "Installation failed."
exit 1
}
2024-08-03 09:52:03 +00:00
;;
2024-08-03 15:21:54 +00:00
pamac)
2024-08-03 21:32:57 +00:00
sudo pamac install --no-confirm "${package_name}" || {
2024-08-03 15:21:54 +00:00
err "Installation failed."
exit 1
}
2024-08-03 09:52:03 +00:00
;;
2024-08-03 15:21:54 +00:00
yay)
2024-08-03 21:30:52 +00:00
sudo yay -S --noconfirm --removemake --quiet "${package_name}" || {
2024-08-03 15:21:54 +00:00
err "Installation failed."
exit 1
}
2024-08-03 09:52:03 +00:00
;;
2024-08-03 15:21:54 +00:00
*)
err "Unsupported package manager: ${package_manager}"
exit 1
2024-08-03 09:52:03 +00:00
;;
esac
2024-08-03 15:21:54 +00:00
2024-08-03 10:27:29 +00:00
inf "installation of ${package_name} completed successfully."
2024-08-03 09:52:03 +00:00
}
2024-08-03 15:21:54 +00:00
update_system() {
local package_manager="${1}"
2024-08-03 09:52:03 +00:00
2024-08-03 15:21:54 +00:00
# If the package manager is not specified, determine it
if [[ -z "${package_manager}" ]]; then
package_manager=$(determine_package_manager)
fi
# Install the package using the determined or specified package manager
inf "updating system using ${package_manager}..."
case "${package_manager}" in
apt)
sudo -E apt update -qq && sudo -E apt upgrade --yes -qq || {
2024-08-03 15:38:17 +00:00
err "Update failed."
2024-08-03 15:21:54 +00:00
exit 1
}
;;
pacman)
sudo pacman -Syu --noconfirm --removemake --quiet || {
2024-08-03 15:38:17 +00:00
err "Update failed."
2024-08-03 15:21:54 +00:00
exit 1
}
;;
pamac)
sudo pamac update --aur --no-confirm && sudo pamac upgrade --aur --no-confirm || {
2024-08-03 15:38:17 +00:00
err "Update failed."
2024-08-03 15:21:54 +00:00
exit 1
}
;;
yay)
2024-08-03 21:30:52 +00:00
sudo yay -Syu --noconfirm --removemake --quiet || {
2024-08-03 15:38:17 +00:00
err "Update failed."
2024-08-03 15:21:54 +00:00
exit 1
}
;;
*)
err "Unsupported package manager: ${package_manager}"
exit 1
;;
esac
inf "system update completed successfully."
}