pacman-db-upgrade: use pacman-style options

* convert dbpath from argument to option
* add --config and --root options
* read dbpath and root from config file
* if root is set but not dbpath, dbpath is set relative to root

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2014-08-20 20:46:07 +00:00 committed by Allan McRae
parent 8bf2c753f5
commit e4773b1b82
2 changed files with 52 additions and 20 deletions

View File

@ -54,6 +54,7 @@ endif
#### Taken from the autoconf scripts Makefile.am ####
edit = sed \
-e 's|@rootdir[@]|$(ROOTDIR)|g' \
-e 's|@localedir[@]|$(localedir)|g' \
-e 's|@sysconfdir[@]|$(sysconfdir)|g' \
-e 's|@localstatedir[@]|$(localstatedir)|g' \

View File

@ -25,19 +25,25 @@ export TEXTDOMAINDIR='@localedir@'
declare -r myver='@PACKAGE_VERSION@'
eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
dbroot="${DBPath:-@localstatedir@/lib/pacman/}"
USE_COLOR='y'
m4_include(library/output_format.sh)
m4_include(library/parseopts.sh)
usage() {
printf "pacman-db-upgrade (pacman) %s\n" "${myver}"
echo
printf -- "$(gettext "Upgrade the local pacman database to a newer format")\n"
echo
printf -- "$(gettext "Usage: %s [--nocolor] [pacman_db_root]")\n" "$0"
printf -- "$(gettext "Usage: %s [options]")\n" "$0"
echo
printf -- "$(gettext "options:")\n"
printf -- "$(gettext " -b, --dbpath <path> set an alternate database location")\n"
printf -- "$(gettext " -h, --help show this help message and exit")\n"
printf -- "$(gettext " -r, --root <path> set an alternate installation root")\n"
printf -- "$(gettext " -V, --version show version information and exit")\n"
printf -- "$(gettext " --config <path> set an alternate configuration file")\n"
printf -- "$(gettext " --nocolor disable colorized output messages")\n"
echo
}
version() {
@ -58,6 +64,18 @@ die_r() {
die "$@"
}
get_opt_from_config() {
local keyname="$1" conffile="$2"
local key value
while IFS=$'= \t' read -r key value _; do
if [[ $key = $keyname ]]; then
echo "$value"
return
fi
done <"$conffile"
}
# PROGRAM START
# determine whether we have gettext; make it a no-op if we do not
@ -67,27 +85,40 @@ if ! type gettext &>/dev/null; then
}
fi
if [[ $1 = "-h" || $1 = "--help" ]]; then
usage
exit 0
fi
USE_COLOR='y'
if [[ $1 = "-V" || $1 = "--version" ]]; then
version
exit 0
OPT_SHORT="d:hr:V"
OPT_LONG=('confg' 'dbpath:' 'help' 'nocolor' 'root:' 'version')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
exit 1 # E_INVALID_OPTION
fi
set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPTRET
if [[ $1 = "--nocolor" ]]; then
USE_COLOR='n'
while true; do
case "$1" in
--config) shift; conffile="$1" ;;
-d|--dbpath) shift; dbroot="$1" ;;
-r|--root) shift; pacroot="$1" ;;
-h|--help) usage; exit 0 ;;
--nocolor) USE_COLOR='n' ;;
-V|--version) version; exit 0 ;;
-- ) shift; break 2 ;;
esac
shift
fi
done
conffile=${conffile:-@sysconfdir@/pacman.conf}
[[ -z $pacroot ]] && pacroot="$(get_opt_from_config "RootDir" "$conffile")"
[[ -z $dbroot ]] && dbroot="$(get_opt_from_config "DBPath" "$conffile")"
[[ -z $dbroot && -n $pacroot ]] && dbroot="$pacroot/@localstatedir@/lib/pacman"
[[ -z $pacroot ]] && pacroot="@rootdir@"
[[ -z $dbroot ]] && dbroot="@localstatedir@/lib/pacman/"
m4_include(library/term_colors.sh)
if [[ -n $1 ]]; then
dbroot="$1"
fi
if [[ ! -d $dbroot ]]; then
die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
fi