Skip to content

Instantly share code, notes, and snippets.

@udkyo
Created August 12, 2023 01:42
Show Gist options
  • Save udkyo/0e66d5e09624085181dc2f9b914b7d0c to your computer and use it in GitHub Desktop.
Save udkyo/0e66d5e09624085181dc2f9b914b7d0c to your computer and use it in GitHub Desktop.
wakka
#!/bin/sh
# Wakka: A package manager wrapper.
# Provides a consistent interface across multiple package managers.
# Currently supports: apt (Debian/Ubuntu), yum (Red Hat/CentOS),
# dnf (Fedora), apk (Alpine), and brew (macOS).
WAKKA_VERSION="1.0.0"
print_help() {
echo "Wakka: Package manager wrapper."
echo "Version: $WAKKA_VERSION"
echo ""
echo "Usage:"
echo " wakka <action> [package] [version]"
echo ""
echo "Actions:"
echo " search/list - Search or list available packages. Optionally filter with [package]"
echo " installed - List installed packages. Optionally filter with [package]"
echo " versions <package> - List available versions of a package."
echo " install <package> - Install a package. Optionally specify [version]"
echo " remove <package> - Remove a package."
echo " upgrade <package> - Upgrade a package."
echo " clean - Clean package manager cache."
echo " --help - Display this help message."
echo " --version - Display wakka version."
}
action="$1"
package="$2"
version="$3"
if [ "$action" = "--help" ]; then
print_help
exit 0
elif [ "$action" = "--version" ]; then
echo "Wakka version: $WAKKA_VERSION"
exit 0
fi
detect_manager() {
OS="$(uname -s)"
case "$OS" in
Darwin)
if command -v brew >/dev/null 2>&1; then
echo "brew"
else
echo "Unsupported"
fi
;;
Linux)
if command -v apt >/dev/null 2>&1; then
echo "apt"
elif command -v yum >/dev/null 2>&1; then
echo "yum"
elif command -v dnf >/dev/null 2>&1; then
echo "dnf"
elif command -v apk >/dev/null 2>&1; then
echo "apk"
else
echo "Unsupported"
fi
;;
*)
echo "Unsupported OS"
;;
esac
}
apt_maybe_update() {
if [ "$(find /var/lib/apt/lists/ -type f 2>/dev/null | wc -l)" -lt 3 ]; then
apt update
fi
}
apk_maybe_update() {
if [ "$(find /var/cache/apk -type f 2>/dev/null | wc -l)" -lt 3 ]; then
apk update
fi
}
manager=$(detect_manager)
case "$manager" in
apt)
apt_maybe_update
case "$action" in
search|list) apt search "$package";;
installed) dpkg -l | grep "$package";;
versions) apt show "$package" | grep Version;;
install) [ -n "$version" ] && echo "apt does not support specific version install" || apt install -y "$package";;
remove) apt remove -y "$package";;
upgrade) apt upgrade -y "$package";;
clean) apt clean;;
esac
;;
apk)
apk_maybe_update
case "$action" in
search|list) apk search "$package";;
installed) apk info | grep "$package";;
versions) apk policy "$package";;
install) apk add --no-cache "$package";;
remove) apk del "$package";;
upgrade) apk add --no-cache --upgrade "$package";;
clean) rm -rf /var/cache/apk/*;;
esac
;;
yum|dnf)
case "$action" in
search|list) $manager list | grep "$package";;
installed) $manager list installed | grep "$package";;
versions) $manager list available "$package" | grep "$package";;
install) $manager install -y "$package";;
remove) $manager remove -y "$package";;
upgrade) $manager upgrade -y "$package";;
clean) $manager clean all;;
esac
;;
brew)
case "$action" in
search|list) brew search "$package";;
installed) brew list | grep "$package";;
versions) brew info "$package";;
install) [ -n "$version" ] && brew install "$package"@"$version" || brew install "$package";;
remove) brew uninstall "$package";;
upgrade) brew upgrade "$package";;
clean) brew cleanup;;
esac
;;
*)
echo "Package manager not supported."
exit 1
;;
esac
@electblake
Copy link

neat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment