Skip to content

Instantly share code, notes, and snippets.

@khromalabs
Created May 31, 2020 15:50
Show Gist options
  • Save khromalabs/f85d691dab2cda77a467cd99bfb4a870 to your computer and use it in GitHub Desktop.
Save khromalabs/f85d691dab2cda77a467cd99bfb4a870 to your computer and use it in GitHub Desktop.
Meta package manager (bash script) to easily mess with the most usual commands (add a package, remove a package, update, upgrade, list packages, etc) on supported systems: Archlinux, Manjaro, FreeBSD, Ubuntu, Amazon Linux and Gentoo. Easily extensible.
#!/bin/bash
# pkt - Package-management Kommon inTerface - 2009-2010 (c) Ruben "Khroma" Gomez
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
# CONFIGURATION ---------------------------------------------------
# FreeBSD
# Configuration required by pkg_add -rK to store the packets, then to find'em
# with a local pkg_add, and with portupgrade and portinstall
# The suggested vars (PKGDIR and PKG_PATH) defined permanenly in .
# bash_profile resulted in some errors, ports didn't compile (blame PKGDIR)
# and pkg_info didn't list packages (blame PKG_PATH), so I defined the
# directory here and activate the corresponding var
# just on the appropiate command via "env"
FREEBSD_PACKAGES=/usr/ports/packages
# END CONFIGURATION ------------------------------------------------
VERSION="1.3.0"
# Lang
if [[ "$LANG" =~ es(.*) ]]
then
# Spanish messages
ERROR_BAD_PARAM="Uso: pkt {accion} [{paquete}]"
ERROR_NOTE=""
ERROR_BAD_OS="No se detectó un sistema operativo soportado"
ERROR_MISSING_TOOL="Falta herramienta requerida"
ERROR_BAD_PARAM_COUNT="Número de elementos incorrecto en array(s)"
ACTIONS_LEGEND=(
"[d]elete: Borrado suave paquete"
"[D]elete: Borrado duro paquete"
"[f]iles: Filtrar por grep ficheros buscados en un paquete"
"[p]acket: Buscar referencia de paquete entre instalados"
"[s]earch: Buscar un paquete en el repositorio"
"[S]earch: Buscar un paquete en el repositorio (todos)"
"[i]nstall: Instalar paquete (ligero)"
"[I]nstall: Instalar paquete (completo)"
"[u]grade: Actualizar instalacion paquete o sistema"
"[U]date: Actualizar descripcion repositorios/ports en sistema"
"[v]iew: Ver info de un paquete"
"[V]iew: Ver info de todos los paquetes instalados")
else
# English messages
ERROR_BAD_PARAM="Use: pkt {action} [{packet}]"
ERROR_NOTE=""
ERROR_BAD_OS="Can't detect a supported OS"
ERROR_MISSING_TOOL="Missing tool: "
ERROR_BAD_PARAM_COUNT="Bad elements count in array(s)"
ACTIONS_LEGEND=(
"[d]elete: soft packet delete"
"[D]elete: hard packet delete"
"[f]iles: Grep files in packet"
"[p]acket: search reference in packets"
"[s]earch: search a packet in the repository"
"[S]earch: search a packet in the repository (all)"
"[i]nstall: install a packet (light)"
"[I]nstall: install a packet (full)"
"[u]grade: upgrade system o packet installation"
"[U]date: update description of repositories/ports"
"[v]iew: view packet info"
"[V]iew: view all installed packets info")
fi
# Data
OSS=( "Ubuntu" "FreeBSD" "ARCH" "MANJARO" "amzn" "gentoo")
REQUIRED_TOOLS_ARCH=("pacman" "yaourt")
REQUIRED_TOOLS_UBUNTU=("sudo" "dpkg" "apt-cache" "apt-get")
REQUIRED_TOOLS_FREEBSD=(
"sudo" "pkg_delete" "pkg_info" "pkg_add" "portupgrade" "portsnap"
"pkg_version" "portinstall")
REQUIRED_TOOLS_AMZN=("yum" "rpm" )
REQUIRED_TOOLS_GENTOO=("emerge" "equery" "qlist" )
ACTION="$1"
shift
COMMANDS=(d D f p s S i I u U v V)
REQUIRED_PARAMETERS=(2 1 2 1 2 2 2 1 1 1 1 1)
# Diferent actions commands per SO, refer to help message to know meaning
ACTIONS_UBUNTU=(
# d
"sudo apt-get autoremove $*"
# D
"sudo apt-get remove --auto-remove --purge $*"
# f
"dpkg -L $*"
# p
"dpkg -l | egrep $1 | awk '{print \$2}'"
# s
"apt-cache search $*"
# S
"apt-cache search $*"
# i
"sudo apt-get install --no-install-recommends $*"
# I
"sudo apt-get install $*"
# u
"sudo apt-get upgrade"
# U
"sudo apt-get update && sudo apt-get upgrade"
# v
"apt-cache show $*"
# V
"dpkg -l"
)
ACTIONS_FREEBSD=(
# d
"sudo pkg_delete $*"
# D
"sudo pkg_delete -r $*"
# f
"pkg_info -L $1*"
# p
"pkg_info | egrep $1"
# s
"pushd . >/dev/null && cd /usr/ports/ && make search name=\"$*\" && popd >/dev/null"
# S
"pushd . >/dev/null && cd /usr/ports/ && make search name=\"$*\" && popd >/dev/null"
# i
# At this moment portinstall installs build-depends with run-depends, fix?
#"env PKGDIR=$FREEBSD_PACKAGES sudo pkg_add -F $* || sudo pkg_add -rK $*"
"env PACKAGES=$FREEBSD_PACKAGES sudo portinstall -PP $*"
# I
"env PACKAGES=$FREEBSD_PACKAGES sudo portinstall -PRDc $*"
# u
"env PACKAGES=$FREEBSD_PACKAGES sudo portupgrade -PRDc "
# U
"sudo portsnap fetch && sudo portsnap update"
# v
"pkg_info -d -R -r -D $1*"
# V
"pkg_version -v"
)
# Nuevas acciones
#ACTIONS_FREEBSD=(
## d
#"pkg delete $*"
## D
#"pkg delete -R $*"
## f
#"pkg info -l $1*"
## p
#"pkg info | grep $1"
## s
#"pkg search $*"
## S
#"pkg search $*"
## i
#"pkg install $*"
## I
#"pkg install $*"
## u
#"pkg upgrade $*"
## U
#"pkg update"
## v
#"pkg info -f -d -r $1"
## V
#"pkg info"
#)
ACTIONS_ARCH=(
# d
"sudo pacman -Rs $*"
# D
"sudo pacman -Rssn $*"
# f
"pacman -Qql $1"
# p
"pacman -Qs $1"
# s
"pacman -Ss $*"
# S
"yaourt -Ss $*"
# i
"sudo pacman --needed -S $*"
# I
"yaourt -S $*"
# u
"sudo pacman -Su"
# U
"sudo pacman -Syu"
# v
"pacman -Qii $*"
# V
"pacman -Q"
)
ACTIONS_MANJARO=(
# d
"sudo pacman -Rs $*"
# D
"sudo pacman -Rssn $*"
# f
"pacman -Qql $1"
# p
"pacman -Qs $1"
# s
"pacman -Ss $*"
# S
"yaourt -Ss $*"
# i
"sudo pacman --needed -S $*"
# I
"yaourt -S $*"
# u
"sudo pacman -Su"
# U
"sudo pacman -Syu"
# v
"pacman -Qii $*"
# V
"pacman -Q"
)
ACTIONS_AMZN=(
# d
"sudo yum remove $*"
# D
"sudo yum autoremove $*"
# f
"rpm -q --list $*"
# p
"rpm -q -a $*"
# s
"yum search $*"
# S
"yum search $*"
# i
"sudo yum install $*"
# I
"sudo yum install $*"
# u
"sudo yum update $*"
# U
"sudo yum upgrade $*"
# v
"yum info $*"
# V
"yum list installed"
)
ACTIONS_GENTOO=(
# d
"sudo emerge -cav $*"
# D
"sudo emerge -cav $* && sudo emerge -a --depclean"
# f
"equery files -t $* | grep 'file '"
# p
"equery list $*"
# s
"emerge -s $* | less -F -r"
# S
#"equery list -p $*"
"emerge -S $*"
# i
"sudo emerge -a --autounmask-continue $*"
# I
"sudo emerge -a --autounmask-continue $*"
# u
"sudo emerge -uDU --keep-going --with-bdeps=y --backtrack=1000 --autounmask-continue --ask $*"
# U
"sudo emaint -a sync"
# v
"equery meta $* && equery uses $*"
# V
"qlist -IRv"
)
# Special behaviour update FreeBSD
if [ "$1" = "" ]; then
ACTIONS_FREEBSD[8]=${ACTIONS_FREEBSD[7]}"-a"
else
ACTIONS_FREEBSD[8]=${ACTIONS_FREEBSD[7]}"$*"
fi
# Check arrays lengths
LEN1=${#COMMANDS[@]}
LEN2=${#REQUIRED_PARAMETERS[@]}
LEN3=${#ACTIONS_LEGEND[@]}
LEN4=${#ACTIONS_UBUNTU[@]}
LEN5=${#ACTIONS_FREEBSD[@]}
LEN6=${#ACTIONS_AMZN[@]}
LEN7=${#ACTIONS_GENTOO[@]}
if [ $LEN1 -ne $LEN2 -o $LEN1 -ne $LEN3 -o $LEN1 -ne $LEN4 -o $LEN1 -ne $LEN5 -o $LEN1 -ne $LEN6 -o $LEN1 -ne $LEN7 ]; then
echo $ERROR_BAD_PARAM_COUNT
echo -e "COMMANDS = $LEN1\nREQUIRED_PARAMETERS = $LEN2\nACTIONS_LEGEND = $LEN3\nACTIONS_UBUNTU = $LEN4\nACTIONS_FREEBSD = $LEN5\nACTIONS_AMZN = $LEN6\nACTIONS_GENTOO = $LEN7"
exit
fi
# Say hello
echo "Package-management Kommon inTerface - v.${VERSION} 2010 (c) Ruben \"Khroma\" Gomez"
# Build actions info message
LEN=${#ACTIONS_LEGEND[@]}
INFO_ACTIONS="Acciones:"
for (( i = 0; i<${LEN}; i++ ));
do
INFO_ACTIONS="$INFO_ACTIONS \n ${COMMANDS[i]} = ${ACTIONS_LEGEND[i]}"
done
INFO_ACTIONS="$INFO_ACTIONS\n"
# Evaluate parameters
if [ "$ACTION" = "" ]; then
echo $ERROR_BAD_PARAM
# echo "ACTION: $ACTION"
echo -e $INFO_ACTIONS
echo -e $ERROR_NOTE
exit
fi
# Detect OS
MY_OS=0
LEN=${#OSS[@]}
for (( i = 0; i<${LEN}; i++ ));
do
IS_OS=`uname -a | grep ${OSS[i]} | wc -l`
if [ $IS_OS = "1" ]; then
MY_OS="${OSS[i]}"
fi
done
if [ $MY_OS = "0" ]; then
echo "$ERROR_BAD_OS"
exit
fi
# Detect action
CMD_I=""
LEN=${#COMMANDS[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if [ $ACTION = "${COMMANDS[i]}" ]; then
CMD_I=$i
fi
done
if [ "$CMD_I" = "" ]; then
echo $ERROR_BAD_PARAM
echo -e $INFO_ACTIONS
exit
fi
if [ "${REQUIRED_PARAMETERS[CMD_I]}" = "2" -a "$1" = "" ]; then
echo $ERROR_BAD_PARAM
echo -e $INFO_ACTIONS
exit
fi
# Validate tools availability
if [ $MY_OS = "Ubuntu" ]; then
LEN=${#REQUIRED_TOOLS_UBUNTU[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_UBUNTU[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_UBUNTU[i]}";
exit
fi
done
ACTION="${ACTIONS_UBUNTU[CMD_I]}"
elif [ $MY_OS = "FreeBSD" ]; then
LEN=${#REQUIRED_TOOLS_FREEBSD[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_FREEBSD[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_FREEBSD[i]}";
exit
fi
done
ACTION="${ACTIONS_FREEBSD[CMD_I]}"
elif [ $MY_OS = "ARCH" ]; then
LEN=${#REQUIRED_TOOLS_ARCH[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_ARCH[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_ARCH[i]}";
exit
fi
done
ACTION="${ACTIONS_ARCH[CMD_I]}"
elif [ $MY_OS = "MANJARO" ]; then
LEN=${#REQUIRED_TOOLS_MANJARO[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_MANJARO[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_MANJARO[i]}";
exit
fi
done
ACTION="${ACTIONS_MANJARO[CMD_I]}"
elif [ $MY_OS = "amzn" ]; then
LEN=${#REQUIRED_TOOLS_AMZN[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_AMZN[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_AMZN[i]}";
exit
fi
done
ACTION="${ACTIONS_AMZN[CMD_I]}"
elif [ $MY_OS = "gentoo" ]; then
LEN=${#REQUIRED_TOOLS_GENTOO[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if ! which ${REQUIRED_TOOLS_GENTOO[i]} > /dev/null; then
echo "$ERROR_MISSING_TOOL ${REQUIRED_TOOLS_GENTOO[i]}";
exit
fi
done
ACTION="${ACTIONS_GENTOO[CMD_I]}"
else
echo "$ERROR_BAD_OS (2)"
exit
fi
# Execute the action
echo -e "## OS:'$MY_OS'\n## ACTION:'${ACTIONS_LEGEND[CMD_I]}'\n## EXECUTING: '$ACTION'\n"
eval "$ACTION"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment