diff options
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2014-08-20 22:46:07 +0200 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2014-09-16 12:05:42 +0200 |
commit | e4773b1b82a737a74434d778c1043367a480205a (patch) | |
tree | c441babae1785b6dc4c9ed41196901b17f74cb41 /scripts | |
parent | 8bf2c753f57bfb6f8cd0271613000af64ce58020 (diff) | |
download | pacman-e4773b1b82a737a74434d778c1043367a480205a.tar.gz pacman-e4773b1b82a737a74434d778c1043367a480205a.tar.xz |
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>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/Makefile.am | 1 | ||||
-rw-r--r-- | scripts/pacman-db-upgrade.sh.in | 73 |
2 files changed, 53 insertions, 21 deletions
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 0b756ad6..bc1dc10c 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -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' \ diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index b0b0ac80..70562e09 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -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,26 +85,39 @@ 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 - -if [[ $1 = "--nocolor" ]]; then - USE_COLOR='n' +set -- "${OPTRET[@]}" +unset OPT_SHORT OPT_LONG OPTRET + +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 -m4_include(library/term_colors.sh) +conffile=${conffile:-@sysconfdir@/pacman.conf} +[[ -z $pacroot ]] && pacroot="$(get_opt_from_config "RootDir" "$conffile")" +[[ -z $dbroot ]] && dbroot="$(get_opt_from_config "DBPath" "$conffile")" -if [[ -n $1 ]]; then - dbroot="$1" -fi +[[ -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 [[ ! -d $dbroot ]]; then die "$(gettext "%s does not exist or is not a directory.")" "$dbroot" |