termux-packages/packages/termux-tools/pkg
Efreak 63733da336 Don't run apt update if it's been run recently (#4135)
When installing or searching for packages, the `apt update` will run only if it
was not executed within the last 5 minutes. Command `pkg upgrade` will
always execute `apt update` to ensure that latest package versions were
picked during upgrade.
2019-11-10 16:29:05 +02:00

54 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
set -e -u
show_help() {
echo 'Usage: pkg command [arguments]'
echo ''
echo 'A tool for managing packages. Commands:'
echo ''
echo ' files <packages>'
echo ' install <packages>'
echo ' list-all'
echo ' list-installed'
echo ' reinstall <packages>'
echo ' search <query>'
echo ' show <packages>'
echo ' uninstall <packages>'
echo ' upgrade'
exit 1
}
assert_not_root() {
if [ $(id -u) -eq 0 ]; then
echo "This must NOT be run as root as it will break your environment (root will be required for nearly everything due to changed file permissions)"
exit 1
fi
}
check_pkgcache() {
if [ -z "$(find @TERMUX_PREFIX@/var/cache/apt/pkgcache.bin -mmin -5)" ]; then
apt update
fi
}
if [ $# = 0 ]; then
show_help
fi
CMD="$1"
shift 1
case "$CMD" in
f*) dpkg -L "$@";;
h*) show_help;;
add|i*) assert_not_root; check_pkgcache; apt install "$@";;
list-a*) apt list "$@";;
list-i*) apt list --installed "$@";;
rei*) assert_not_root; apt install --reinstall "$@";;
se*) assert_not_root; check_pkgcache; apt search "$@";;
sh*) apt show "$@";;
un*|rem*|rm|del*) assert_not_root; apt remove "$@";;
up*) assert_not_root; apt update; apt full-upgrade "$@";;
*) echo "Unknown command: '$CMD' (run 'pkg help' for usage information)"; exit 1;;
esac