Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better parsing of command-line options #90

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 74 additions & 13 deletions dom0-updates/qubes-dom0-update
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/bin/bash

find_regex_in_args() {
local regex="${1}"
shift 1

for arg in "${@}"; do
if echo "${arg}" | grep -q -e "${regex}"; then
return 0
fi
#!/bin/bash --
set -euo pipefail
shopt -s assoc_expand_once
unset YUM_ACTION UPDATEVM

check_template_in_args () {
local pkg
for pkg; do
if [[ "$pkg" =~ ^qubes-template- ]]; then return 0; fi
done

return 1
Expand Down Expand Up @@ -57,8 +56,70 @@ TEMPLATE=
TEMPLATE_BACKUP=
FORCE_XEN_UPGRADE=
REBOOT_REQUIRED=
declare -A options_with_args
options_with_args=(
[action]=
[advisory]=
[advisories]=
[bz]=
[bzs]=
[color]=
[comment]=
[cve]=
[cves]=
[debuglevel]=
[disableexcludes]=
[disableexcludepkgs]=
[disableplugin]=
[disablerepo]=
[downloaddir]=
[destdir]=
[errorlevel]=
[enableplugin]=
[enablerepo]=
[exclude]=
[excludepkgs]=
[forcearch]=
[installroot]=
[releasever]=
[repofrompath]=
[repo]=
[repoid]=
[rpmverbosity]=
[sec-severity]=
[secseverity]=
[setopt]=
)

# Filter out some dnf options and collect packages list
while [ $# -gt 0 ]; do
case $1 in
(--*)
if [[ -v options_with_args["${1:2}"] ]]; then
if [[ "$#" -lt 2 ]]; then
printf 'Missing argument to %s\n' "$1"
exit 1
else
printf 'WARNING: %s %q must be passed as %s=%q\n' "$1" "$2" "$1" "$2"
printf 'This will be an error in a future version!\n'
fi >&2
set -- "$1=$2" "${@:3:$# - 2}"
fi
;;
(-[!-]*)
if [[ "$1" =~ ^-[^dexR]*[dexR]$ ]]; then
if [[ "$#" -lt 2 ]]; then
printf 'Missing argument to %q\n' "$1"
exit 1
else
printf 'WARNING: %q %q must be written as %q%q\n' "$1" "$2" "$1" "$2"
printf 'This will be an error in a future version!\n'
fi >&2
set -- "$1$2" "${@:3:$# - 2}"
fi
;;
esac
printf %q\\n "$1"
case "$1" in
--enablerepo=*|\
--disablerepo=*)
Expand Down Expand Up @@ -105,7 +166,7 @@ while [ $# -gt 0 ]; do
fi
;;
-*)
YUM_OPTS+=( "${1}" )
YUM_OPTS+=( "$1" )
UPDATEVM_OPTS+=( "$1" )
QVMTEMPLATE_OPTS+=( "$1" )
;;
Expand Down Expand Up @@ -137,7 +198,7 @@ case ${YUM_ACTION=upgrade} in
esac

# Redirect operations on templates to qvm-template command
if find_regex_in_args '^qubes-template-' "${PKGS[@]}"; then
if check_template_in_args "${PKGS[@]}"; then
if [[ ${#PKGS[@]} -ne 1 ]]; then
echo "ERROR: Specify only one package to reinstall template"
exit 2
Expand Down Expand Up @@ -201,7 +262,7 @@ if [ "$GUI" == "1" ]; then
fi

# Do not start VM automatically when running from cron (only checking for updates)
if [ "$CHECK_ONLY" == "1" ] && ! qvm-check -q --running "$UPDATEVM" > /dev/null 2>&1; then
if [ "$CHECK_ONLY" = "1" ] && ! qvm-check -q --running -- "$UPDATEVM" > /dev/null 2>&1; then
echo "ERROR: UpdateVM not running, not starting it in non-interactive mode" >&2
exit 1
fi
Expand Down