Skip to content

Instantly share code, notes, and snippets.

@mekb-turtle
Last active March 13, 2023 08:24
Show Gist options
  • Save mekb-turtle/18f31f4b0b65c188349a55a03b0ef548 to your computer and use it in GitHub Desktop.
Save mekb-turtle/18f31f4b0b65c188349a55a03b0ef548 to your computer and use it in GitHub Desktop.
Pacman-like front-end for apt
#!/usr/bin/bash
ROOT_PROGRAM=(sudo)
APT=(apt)
function pacman-run-apt(){
"${APT[@]}" "$@"
}
function pacman-run-apt-with-root(){
IFS=
if [[ "$(id -u)" == "0" ]]; then
"${APT[@]}" "$@"
else
if [[ "${#ROOT_PROGRAM[@]}" -gt 0 ]]; then
"${ROOT_PROGRAM[@]}" "${APT[@]}" "$@"
else
echo "you need to be root to use this"
return 1
fi
fi
}
function pacman-apt(){
IFS=
S=0
R=0
Q=0
y=0
u=0
h=0
s=0
a=0
packages=()
end=0
for arg in "$@"; do
if [[ "$arg" == "--" ]]; then end=1; fi
if [[ "$end" == 0 && "$arg" == -* ]]; then
E=0
for z in S R Q y u h s a; do
if [[ "$arg" == *"$z"* && "$arg" != \-\-* ]]; then
eval "$z=1"
E=1
fi
done
if [[ "$E" != 1 ]]; then
echo "invalid flag"
return
fi
else
packages+=( "$arg" )
fi
done
if [[ "$h" == 1 ]]; then
echo "pacman-apt"
echo " -S : apt install [packages...]"
echo " -y : apt update"
echo " -u : apt upgrade"
echo " -s : apt search"
echo " -R : apt remove [packages...]"
echo " -u : apt autoremove"
echo " -Q : apt show [packages...]"
echo " -a : apt show -a [packages...]"
return
fi
if [[ "$R" == 1 ]]; then
if [[ "$S" == 1 ]]; then
echo "can not have multiple operations at once"
return
fi
if [[ "$Q" == 1 ]]; then
echo "can not have multiple operations at once"
return
fi
if [[ "$a" == 1 ]]; then
echo "invalid flag"
return
fi
if [[ "$y" == 1 ]]; then
echo "invalid flag"
return
fi
if [[ "$s" == 1 ]]; then
echo "invalid flag"
return
fi
else
if [[ "$Q" == 1 ]]; then
if [[ "$S" == 1 ]]; then
echo "can not have multiple operations at once"
return
fi
if [[ "$u" == 1 ]]; then
echo "invalid flag"
return
fi
if [[ "$y" == 1 ]]; then
echo "invalid flag"
return
fi
if [[ "$s" == 1 ]]; then
echo "invalid flag"
return
fi
else
if [[ "$S" == 1 ]]; then
if [[ "$a" == 1 ]]; then
echo "invalid flag"
return
fi
else
echo "no operation specified"
return
fi
fi
fi
if [[ "$s" == 1 ]]; then
if [[ "$y" == 1 ]]; then
echo "-y and -s can not go together"
return
fi
if [[ "$u" == 1 ]]; then
echo "-u and -s can not go together"
return
fi
fi
if [[ "$Q" == 1 ]]; then
if [[ "${#packages[@]}" -gt 0 ]]; then
if [[ "$a" == 1 ]]; then
pacman-run-apt show -a "${packages[@]}" || return "$?"
else
pacman-run-apt show "${packages[@]}" || return "$?"
fi
else
echo "no packages specified"
return 1
fi
return 0
fi
if [[ "$R" == 1 ]]; then
if [[ "$u" == 1 ]]; then
if [[ "${#packages[@]}" -gt 0 ]]; then
pacman-run-apt-with-root remove "${packages[@]}" || return "$?"
fi
pacman-run-apt-with-root autoremove || return "$?"
else
if [[ "${#packages[@]}" -gt 0 ]]; then
pacman-run-apt-with-root remove "${packages[@]}" || return "$?"
else
echo "no packages specified"
return 1
fi
fi
return 0
fi
if [[ "$S" == 1 ]]; then
if [[ "$s" == 1 ]]; then
if [[ "${#packages[@]}" -gt 0 ]]; then
pacman-run-apt search "${packages[@]}" || return "$?"
else
echo "no packages specified"
return 1
fi
elif [[ "$u" == 1 ]] || [[ "$y" == 1 ]]; then
if [[ "$y" == 1 ]]; then
pacman-run-apt-with-root update || return "$?"
fi
if [[ "$u" == 1 ]]; then
pacman-run-apt-with-root upgrade || return "$?"
fi
if [[ "${#packages[@]}" -gt 0 ]]; then
pacman-run-apt-with-root install "${packages[@]}" || return "$?"
fi
else
if [[ "${#packages[@]}" -gt 0 ]]; then
pacman-run-apt-with-root install "${packages[@]}" || return "$?"
else
echo "no packages specified"
return 1
fi
fi
return 0
fi
return 1
}
IFS= pacman-apt "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment