From e4d0468cfecb443ce3d36920422da3e65b87a6b3 Mon Sep 17 00:00:00 2001 From: Lucius Hu Date: Fri, 1 May 2020 02:31:53 -0400 Subject: [PATCH] swupd.bash: Multiple improvements - All options and subcommands are automatically generated from help messages. - Used `_init_completion` to set $cur, $prev, $words variables, which are equivalent to $2, $3, $COMP_WORD, but far more readable. - It also allows possibility to use other functions provided in `/usr/share/bash-completion/bash_completion`, for future - A minor change on getting current OS version via `swupd info --quiet` from reading `/var/lib/swupd/version`, which is non-readable to non-root users - Though MoM file is still non-readable to non-root users - We only ship modern Bash, so the test on Bash version near the end of the script is removed --- swupd.bash | 249 +++++++++++++++++++---------------------------------- 1 file changed, 88 insertions(+), 161 deletions(-) diff --git a/swupd.bash b/swupd.bash index beb86eea..2e141465 100644 --- a/swupd.bash +++ b/swupd.bash @@ -18,167 +18,94 @@ #declares the completion function -_swupd() -{ - # $1 is the command being completed, $2 is the current word being expanded - local opts IFS=$' \t\n' - local -i i installed - local global="--help --url --contenturl --versionurl --port --path --format --nosigcheck --nosigcheck-latest --ignore-time --statedir --certpath --time --no-scripts --no-boot-update --max-parallel-downloads --max-retries --retry-delay --json-output --allow-insecure-http --debug --verbose --quiet --no-progress --wait-for-scripts --assume --yes" - COMPREPLY=() - for ((i=COMP_CWORD-1;i>=0;i--)) - do case "${COMP_WORDS[$i]}" in - ("$1") - opts="--help --version autoupdate bundle-add bundle-remove - bundle-list bundle-info hashdump update diagnose check-update search - search-file info clean mirror os-install repair 3rd-party" - break;; - ("info") - opts="$global " - break;; - ("autoupdate") - opts="$global --help --enable --disable " - break;; - ("check-update") - opts="$global " - break;; - ("update") - opts="$global --download --status --force --keepcache --update-search-file-index --3rd-party " - break;; - ("bundle-add") - opts="$global --skip-diskspace-check --skip-optional " - break;; - ("bundle-remove") - opts="$global --force --recursive " - break;; - ("bundle-list") - opts="$global --all --deps --has-dep --status " - break;; - ("bundle-info") - opts="$global --dependencies --files --version " - break;; - ("search") - opts="--help --all --quiet --verbose " - break;; - ("search-file") - opts="$global --version --library --binary --top --csv --init --order " - break;; - ("diagnose") - opts="$global --version --picky --picky-tree --picky-whitelist --quick --force --extra-files-only --bundles --file " - break;; - ("repair") - opts="$global --version --picky --picky-tree --picky-whitelist --quick --force --extra-files-only --bundles --file " - break;; - ("os-install") - opts="$global --version --force --bundles --statedir-cache --download --skip-optional" - break;; - ("mirror") - opts="$global --set --unset " - break;; - ("clean") - opts="$global --all --dry-run " - break;; - ("hashdump") - opts="--help --no-xattrs --path --debug --quiet " - break;; - ("3rd-party") - opts="$global add remove list bundle-add bundle-list bundle-remove bundle-info update diagnose repair check-update clean info " - break;; - ("add") - opts="$global --repo --force" - break;; - ("remove") - opts="$global --repo --force" - break;; - esac - done - # Add in additional completion options if we need to - if (( i >= 0 )) - then - case "${COMP_WORDS[$i]}" in - ("bundle-add") - # only show the list of upstream bundles if not using "3rd-party bundle-add" - if [ "${COMP_WORDS[$i - 1]}" != "3rd-party" ]; then - MoM="" - if [ -r /var/tmp/swupd/Manifest.MoM ] - then MoM=/var/tmp/swupd/Manifest.MoM - elif [ -r /var/lib/swupd/version ] && - installed=$(