[pacman-dev,4/4] use pacman-conf in scripts

Message ID 20180113165000.29311-5-andrew.gregory.8@gmail.com
State Accepted, archived
Headers show
Series add pacman-conf utility | expand

Commit Message

Andrew Gregory Jan. 13, 2018, 4:50 p.m. UTC
Because parsing pacman.conf is so difficult that even we can't do it
right.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
---
 scripts/completion/zsh_completion.in |  4 ++--
 scripts/pacman-db-upgrade.sh.in      | 21 ++-------------------
 scripts/pacman-key.sh.in             |  2 +-
 3 files changed, 5 insertions(+), 22 deletions(-)

Comments

Eli Schwartz Jan. 13, 2018, 11:21 p.m. UTC | #1
On 01/13/2018 11:50 AM, Andrew Gregory wrote:
> Because parsing pacman.conf is so difficult that even we can't do it
> right.
> 
> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
> ---
>  scripts/completion/zsh_completion.in |  4 ++--
>  scripts/pacman-db-upgrade.sh.in      | 21 ++-------------------
>  scripts/pacman-key.sh.in             |  2 +-
>  3 files changed, 5 insertions(+), 22 deletions(-)

Our bash completion should also be updated to use pacman-conf, as
currently it uses a glorious hack to find the completions for -Sl which
involves verbosely printing all packages ever and using `cut|sort -u` to
extract repo names and dedupe them.

Of course, this was still more accurate than the zsh config file parsing.

I'd also like to see some way of listing group names for completing -Qg,
I guess, although that isn't really something to solve via better
*configuration* parsing, so it is probably offtopic for this.
Allan McRae Jan. 13, 2018, 11:34 p.m. UTC | #2
On 14/01/18 09:21, Eli Schwartz wrote:
> On 01/13/2018 11:50 AM, Andrew Gregory wrote:
>> Because parsing pacman.conf is so difficult that even we can't do it
>> right.
>>
>> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
>> ---
>>  scripts/completion/zsh_completion.in |  4 ++--
>>  scripts/pacman-db-upgrade.sh.in      | 21 ++-------------------
>>  scripts/pacman-key.sh.in             |  2 +-
>>  3 files changed, 5 insertions(+), 22 deletions(-)
> 
> Our bash completion should also be updated to use pacman-conf, as
> currently it uses a glorious hack to find the completions for -Sl which
> involves verbosely printing all packages ever and using `cut|sort -u` to
> extract repo names and dedupe them.
> 

patches welcome.

Patch

diff --git a/scripts/completion/zsh_completion.in b/scripts/completion/zsh_completion.in
index f74fa297..77449955 100644
--- a/scripts/completion/zsh_completion.in
+++ b/scripts/completion/zsh_completion.in
@@ -316,7 +316,7 @@  _pacman_completions_all_packages() {
 		typeset -U packages
 		${seq} _wanted packages expl "packages" compadd ${sep[@]} - "${(@)packages}"
 
-		repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
+		repositories=($(pacman-conf --repo-list))
 		typeset -U repositories
 		_wanted repo_packages expl "repository/package" compadd -S "/" $repositories
 	fi
@@ -348,7 +348,7 @@  _pacman_all_packages() {
 # provides completions for repository names
 _pacman_completions_repositories() {
 	local -a cmd repositories
-	repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
+	repositories=($(pacman-conf --repo-list))
 	# Uniq the array
 	typeset -U repositories
 	compadd "$@" -a repositories
diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in
index a6ff3b25..0629cc1c 100644
--- a/scripts/pacman-db-upgrade.sh.in
+++ b/scripts/pacman-db-upgrade.sh.in
@@ -70,18 +70,6 @@  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"
-}
-
 resolve_dir() {
 	local d="$(cd "$1"; pwd -P)"
 	[[ $d == */ ]] || d+=/
@@ -121,13 +109,8 @@  while true; do
 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/"
+[[ -z $pacroot ]] && pacroot=$(pacman-conf --config="$conffile" rootdir)
+[[ -z $dbroot ]] && dbroot=$(pacman-conf --config="$conffile" --rootdir="$pacroot" dbpath)
 
 m4_include(library/term_colors.sh)
 
diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in
index 7b158da7..293a42de 100644
--- a/scripts/pacman-key.sh.in
+++ b/scripts/pacman-key.sh.in
@@ -580,7 +580,7 @@  fi
 
 # if PACMAN_KEYRING_DIR isn't assigned, try to get it from the config
 # file, falling back on a hard default
-PACMAN_KEYRING_DIR=${PACMAN_KEYRING_DIR:-$(get_from "$CONFIG" "GPGDir" "@sysconfdir@/pacman.d/gnupg")}
+PACMAN_KEYRING_DIR=${PACMAN_KEYRING_DIR:-$(pacman-conf --config="$CONFIG" gpgdir)}
 
 GPG_PACMAN=(gpg --homedir "${PACMAN_KEYRING_DIR}" --no-permission-warning)
 if [[ -n ${KEYSERVER} ]]; then