few improvements
- Send info/error messages to stderr. - Option to suppress all info messages except errors. - Allow to upgrade database before processing input. - Verify user input, show errors when file does not exist or package name is not valid. - Get rid of 'getopts' - don't like it.
This commit is contained in:
parent
2ebb0d3b2f
commit
451422bc8f
136
whatprovides
136
whatprovides
|
@ -6,66 +6,130 @@ DB_PATH="/data/data/com.termux/files/usr/var/lib/whatprovides/whatprovides.db"
|
||||||
DB_UPDATES_URL="https://dl.bintray.com/termux/metadata/whatprovides-db/whatprovides.db.gz"
|
DB_UPDATES_URL="https://dl.bintray.com/termux/metadata/whatprovides-db/whatprovides.db.gz"
|
||||||
|
|
||||||
show_usage () {
|
show_usage () {
|
||||||
echo
|
{
|
||||||
echo "Usage: $SCRIPT_NAME [options] path/to/file"
|
echo
|
||||||
echo
|
echo "Usage: $SCRIPT_NAME [-u] path/to/file"
|
||||||
echo "Find out packages using specific files."
|
echo " $SCRIPT_NAME -p [-u] package"
|
||||||
echo
|
echo
|
||||||
echo "Options:"
|
echo "Find out packages using specific files."
|
||||||
echo
|
echo
|
||||||
echo " -h Show this help."
|
echo "Options:"
|
||||||
echo " -p [package] List files of package."
|
echo
|
||||||
echo " -u Update the database."
|
echo " -h Show this help."
|
||||||
echo
|
echo
|
||||||
|
echo " -p Reverse mode. List all files owned by specified"
|
||||||
|
echo " package."
|
||||||
|
echo
|
||||||
|
echo " -u Update the database."
|
||||||
|
echo
|
||||||
|
echo " -q Quiet mode. Suppress informational messages."
|
||||||
|
echo
|
||||||
|
} >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
update_database() {
|
update_database() {
|
||||||
if [ -e "${DB_PATH}.gz" ]; then
|
if [ -e "${DB_PATH}.gz" ]; then
|
||||||
echo "[*] Cleaning up the remaining temporary files..."
|
if ! ${QUIET}; then
|
||||||
|
echo "[*] Cleaning up the remaining temporary files..." >&2
|
||||||
|
fi
|
||||||
rm -f "${DB_PATH}.gz"
|
rm -f "${DB_PATH}.gz"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "[*] Downloading the new database..."
|
if ! ${QUIET}; then
|
||||||
echo
|
echo "[*] Downloading the new database..." >&2
|
||||||
curl --fail --retry 3 --retry-connrefused --retry-delay 1 --location \
|
echo >&2
|
||||||
--output "${DB_PATH}.gz" "${DB_UPDATES_URL}"
|
|
||||||
echo
|
curl --fail --retry 3 --retry-connrefused --retry-delay 1 --location \
|
||||||
|
--output "${DB_PATH}.gz" "${DB_UPDATES_URL}"
|
||||||
|
|
||||||
|
echo >&2
|
||||||
|
echo "[*] Installing..." >&2
|
||||||
|
else
|
||||||
|
curl --silent --fail --retry 3 --retry-connrefused --retry-delay 1 \
|
||||||
|
--location --output "${DB_PATH}.gz" "${DB_UPDATES_URL}"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "[*] Installing..."
|
|
||||||
rm -f "${DB_PATH}"
|
rm -f "${DB_PATH}"
|
||||||
zcat "${DB_PATH}.gz" > "${DB_PATH}"
|
zcat "${DB_PATH}.gz" > "${DB_PATH}"
|
||||||
rm -f "${DB_PATH}.gz"
|
rm -f "${DB_PATH}.gz"
|
||||||
|
|
||||||
echo "[*] Finished."
|
if ! ${QUIET}; then
|
||||||
|
echo "[*] Finished." >&2
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
check_database() {
|
check_database() {
|
||||||
if [ ! -e "${DB_PATH}" ]; then
|
if [ ! -e "${DB_PATH}" ]; then
|
||||||
echo "Error: database is not available."
|
{
|
||||||
echo "Please run '${SCRIPT_NAME} -u' to create it."
|
echo "Error: database is not available."
|
||||||
|
echo "Please run '${SCRIPT_NAME} -u' to create it."
|
||||||
|
} >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
while getopts :hp:u option; do
|
REVERSE_MODE=false
|
||||||
case "$option" in
|
DO_UPDATE=false
|
||||||
p)
|
QUIET=false
|
||||||
check_database
|
while (($# > 0)); do
|
||||||
echo "SELECT owned_file FROM 'whatprovides' WHERE package_name == '${OPTARG}' ORDER BY owned_file;" | \
|
case "$1" in
|
||||||
sqlite3 "${DB_PATH}" | awk "{ print \"${OPTARG}: \"\$0 }"
|
-h) show_usage; exit 0;;
|
||||||
exit 0
|
-p) REVERSE_MODE=true;;
|
||||||
|
-q) QUIET=true;;
|
||||||
|
-u) DO_UPDATE=true;;
|
||||||
|
-*)
|
||||||
|
echo >&2
|
||||||
|
echo "Unknown option '$1'." >&2
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
break
|
||||||
;;
|
;;
|
||||||
u) update_database; exit 0;;
|
|
||||||
h) show_usage; exit 0;;
|
|
||||||
*) show_usage; exit 1;;
|
|
||||||
esac
|
esac
|
||||||
|
shift 1
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $# -ge 1 ]; then
|
if ${DO_UPDATE}; then
|
||||||
check_database
|
update_database
|
||||||
echo "SELECT package_name FROM 'whatprovides' WHERE owned_file == '${1}' ORDER BY package_name;" | \
|
[ $# -lt 1 ] && exit 0
|
||||||
sqlite3 "${DB_PATH}" | awk "{ print \$0\": ${1}\" }"
|
|
||||||
else
|
else
|
||||||
show_usage
|
check_database
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $# -lt 1 ]; then
|
||||||
|
{
|
||||||
|
echo
|
||||||
|
echo "Error: you have not specified the file or package."
|
||||||
|
echo
|
||||||
|
} >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if ${REVERSE_MODE}; then
|
||||||
|
if ! grep -qP '^[a-z0-9_+\-]+$' <<< "$1"; then
|
||||||
|
{
|
||||||
|
echo
|
||||||
|
echo "Error: package name '${1}' is not valid."
|
||||||
|
echo
|
||||||
|
} >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "SELECT owned_file FROM 'whatprovides' WHERE package_name == '${1}' ORDER BY owned_file;" | \
|
||||||
|
sqlite3 "${DB_PATH}" | awk "{ print \"${1}: \"\$0 }"
|
||||||
|
else
|
||||||
|
if [ ! -e "$1" ]; then
|
||||||
|
{
|
||||||
|
echo
|
||||||
|
echo "Error: file '$1' is not found."
|
||||||
|
echo
|
||||||
|
} >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "SELECT package_name FROM 'whatprovides' WHERE owned_file == '$(realpath "${1}")' ORDER BY package_name;" | \
|
||||||
|
sqlite3 "${DB_PATH}" | awk "{ print \$0\": $(realpath "${1}")\" }"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|
Loading…
Reference in New Issue