Last active
September 21, 2020 01:29
-
-
Save MountainX/8efe69d987554065432c3150c583d738 to your computer and use it in GitHub Desktop.
aurutils configuration system as per #393 https://bbs.archlinux.org/viewtopic.php?pid=1906209#p1906209
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
readonly argv0=aur | |
readonly lib_dir='/usr/lib/aurutils' | |
#readonly lib_dir=${AUR_EXEC_PATH:-'AURUTILS_LIB_DIR'} | |
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config} | |
config_file=$XDG_CONFIG_HOME/aurutils/aurutils.conf | |
shopt -s nullglob | |
usage() { | |
printf >&2 'usage: %s [-K] [command]\n\n' "$argv0" | |
printf >&2 'available commands:\n' | |
for p in "$lib_dir"/aur-*; do | |
printf '%q\n' "${p##*/aur-}" | |
done | column >&2 | |
printf >&2 '\navailable user commands:\n' | |
compgen -c aur- | sort -u | while IFS= read -r; do | |
if ! [[ -f $lib_dir/$REPLY ]]; then | |
printf '%q\n' "${REPLY#aur-}" | |
fi | |
done | column >&2 | |
exit 1 | |
} | |
aurutil_lib=$1 | |
if [[ -z $aurutil_lib ]]; then | |
usage | |
fi | |
if [[ "$PATH" != "$lib_dir:"* ]]; then | |
readonly PATH=$lib_dir:$PATH | |
fi | |
options=() | |
read_options() { | |
#global options | |
local key value | |
while IFS='=' read -r key value; do | |
key=$(echo $key | xargs) #trim spaces | |
if [[ $value ]]; then | |
value=$(echo $value | xargs) #trim spaces | |
options+=("$key=$value") | |
echo "adding option $key=$value" | |
else | |
options+=("$key") | |
echo "adding option $key" | |
fi | |
done < <(pacini --section="$2" "$1") | |
} | |
case $1 in | |
-K|--config) # XXX take configuration file as argument | |
shift; | |
if [[ -z $1 ]]; then | |
usage | |
fi | |
if [[ -f $config_file ]]; then | |
config_file=$1 | |
else | |
printf >&2 '%s: no such file or directory\n' "$config_file" | |
exit 1 | |
fi | |
;; | |
*) | |
esac | |
echo "config file is: $config_file" | |
if [[ -f $config_file ]]; then | |
case $1 in | |
# aur-sync uses both aur-build and its own specific options | |
sync) | |
if pacini "$config_file" --section-list | grep -q build; then | |
read_options "$config_file" build | |
fi | |
if pacini "$config_file" --section-list | grep -q sync; then | |
read_options "$config_file" sync | |
fi | |
;; | |
# options for other tools | |
*) | |
if pacini "$config_file" --section-list | grep -q "$1"; then | |
read_options "$config_file" "$1" | |
fi | |
;; | |
esac | |
fi | |
if type -P "aur-$aurutil_lib" >/dev/null; then | |
exec "aur-$aurutil_lib" "${options[@]}" "${@:2}" | |
else | |
printf >&2 '%s: %q is not an aur command\n' "$argv0" "$1" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment