Update swupd completion code

Make swupd save a copy of the current MoM in a readable location, so
the completion code for bundle-add has access to it.

Stop generating the completion code, just use the file directly.

Remove the short option from the completion code, whilst they are
useful for an expert there is little point typing minus tab and then a
letter rather than minus and the letter.

Add crude filename completion for hashdump.

Signed-off-by: Icarus Sparry <icarus.w.sparry@intel.com>
This commit is contained in:
Icarus Sparry
2018-03-26 11:23:04 -07:00
committed by Matthew Johnson
parent e441c38462
commit bc5cae81dc
8 changed files with 121 additions and 171 deletions
-1
View File
@@ -42,7 +42,6 @@ stamp-h1
swupd
swupd-client*tar.gz
swupd_*
swupd.bash
tap-driver.sh
test-driver
test-suite.log
-3
View File
@@ -277,6 +277,3 @@ man: $(dist_man_MANS)
rst2man.py "$<" > "$@.tmp" && mv -f "$@.tmp" "$@"
test/functional/completion/basic/test.bats: swupd.bash
swupd.bash: $(top_srcdir)/scripts/swupd-completion.sh $(top_srcdir)/swupd
bash $<
-162
View File
@@ -1,162 +0,0 @@
#!/bin/bash
#
# Software Updater - autocompletion script
#
# Copyright © 2016 Intel Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 or later of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program creates a _swupd function to be used by completion built-in
# bash command to complete swup.
# It reads options and subcommands from swup --help and then it reads
# options for each subcommand recursively.
SCRIPTNAME="swupd.bash"
COMPLETIONFUNCTION="_swupd"
SWUPDCOMMAND="./swupd"
optionsfromhelp=""
subcommandsfromhelp=()
#Checks if swupd commad exists in current shell
if [ ! -e $SWUPDCOMMAND ] ; then
echo "Error: No swupd found"
exit 1
fi
currenttype=""
nexttype=""
#reads "swupd --help" line by line
while read word1 word2 rest
do
#For each line checks in what section is
if [[ "$word1 $word2" == "Help Options:" ]]; then
currenttype="options"
continue
fi
if [[ "$word1" == "Subcommands:" ]]; then
currenttype="subcommand"
continue
fi
if [[ "$word1" == "" ]]; then
currenttype=""
continue
fi
case $currenttype in
#In case of subcommand it inserts first word of line in an array
("subcommand")
subcommandsfromhelp+=("$word1") ;;
#For options it reads short and long options, remove the commma from short
("options")
optionsfromhelp="${optionsfromhelp} ${word1%,} ${word2}" ;;
#For the rest do nothing
("") : ;;
esac
done < <($SWUPDCOMMAND --help 2>&1)
# Construct sed script
#Convert all tabs to spaces
SEDSCRIPT=$'s/\t/ /g'
#get rid of anything that does not start with a -
SEDSCRIPT+=';/^ *-/!d'
#remove leading spaces
SEDSCRIPT+=';s/^ *//'
# remove commas, any extra spaces between options
SEDSCRIPT+=';s/, *-/ -/'
# Strip trailing junk, anything after = or the second word
SEDSCRIPT+=';s/=.*//'
SEDSCRIPT+=';s/^\([^ ]* [^ ]*\).*/\1/'
# See if there is only one option on line
SEDSCRIPT+=';/ -/!s/ .*//'
#Autocomplete script creation
{
cat <<EOM
#!/bin/bash
# WARNING: This is an autogenerated script
# Software Updater - autocompletion script
#
# Copyright © 2017 Intel Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 or later of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#declares the completion function
${COMPLETIONFUNCTION}()
{
# \$1 is the command being completed, \$2 is the current word being expanded
local opts IFS=\$' \t\n'
local -i i installed
COMPREPLY=()
for ((i=COMP_CWORD-1;i>=0;i--))
do case "\${COMP_WORDS[\$i]}" in
EOM
#This adds options for all subcommands
printf '\t ("$1")\n\t\topts="%s"\n' "$optionsfromhelp ${subcommandsfromhelp[*]}"
printf '\t break;;\n'
for i in "${subcommandsfromhelp[@]}"; do
printf '\t ("%s")\n' "$i"
C="$($SWUPDCOMMAND $i --help 2>&1 | sed -e "$SEDSCRIPT" | tr '\n' ' ')"
printf '\t\topts="%s"\n' "$C"
printf '\t\tbreak;;\n'
done
# The tail of the script
cat >> $SCRIPTNAME << 'EOM'
esac
done
# Add in additional completion options if we need to
if (( $i >= 0 ))
then
case "${COMP_WORDS[$i]}" in
("bundle-add")
if [ -r /var/lib/swupd/version ] &&
installed=$(</var/lib/swupd/version) &&
[ -r /var/lib/swupd/$installed/Manifest.MoM ]
then
opts+="$( sed '1,/^$/d; s/^.*\t/ /' /var/lib/swupd/$installed/Manifest.MoM | LC_ALL=C sort )"
fi ;;
("bundle-remove")
opts+=" $(unset CDPATH; test -d /usr/share/clear/bundles && \
cd /usr/share/clear/bundles && ls | grep -v '^os-core' )"
;;
esac
fi
COMPREPLY=($(compgen -W "${opts}" -- ${2}));
return 0
}
if [ ${BASH_VERSINFO[0]} -gt 4 ] || ( [ ${BASH_VERSINFO[0]} -eq 4 ] && [ ${BASH_VERSINFO[1]} -ge 4 ] )
then
complete -F _swupd -o nosort swupd
else
complete -F _swupd swupd
fi
EOM
} > $SCRIPTNAME
+2 -3
View File
@@ -928,13 +928,12 @@ int list_local_bundles()
return EXIT_FAILURE;
}
while ((ent = readdir(dir))) {
if ((strcmp(ent->d_name, ".") == 0) ||
(strcmp(ent->d_name, "..") == 0)) {
if (ent->d_name[0] == '.') {
continue;
}
/* Need to dup the strings as the directory
* may be bigger than the size of the I/O buffer */
char * name = strdup(ent->d_name);
char *name = strdup(ent->d_name);
if (!name) {
abort();
}
+12
View File
@@ -742,6 +742,18 @@ verify_mom:
}
free_string(&log_cmd);
}
/* Make a copy of the Manifest for the completion code */
if (latest) {
char *momcopy;
rm_bundle_file(".MoM");
string_or_die(&momcopy, "/bin/cp \"%s\" \"%s/%s/.MoM\" 2>/dev/null",
filename, path_prefix, BUNDLES_DIR);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
(void)system(momcopy); /* If it fails it fails */
#pragma GCC diagnostic pop
free(momcopy);
}
free_string(&filename);
free_string(&url);
return manifest;
+2 -1
View File
@@ -84,7 +84,8 @@ void read_subscriptions(struct list **subs)
dir = opendir(path);
if (dir) {
while ((ent = readdir(dir))) {
if ((strcmp(ent->d_name, ".") == 0) || (strcmp(ent->d_name, "..") == 0)) {
if (ent->d_name[0] == '.') {
/* ignore dot files, in particular .MoM */
continue;
}
if (ent->d_type == DT_REG) {
+104
View File
@@ -0,0 +1,104 @@
#!/bin/bash
# Software Updater - autocompletion script
#
# Copyright © 2018 Intel Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 or later of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#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
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 hashdump update verify check-update search info clean"
break;;
("autoupdate")
opts="--help --enable --disable "
break;;
("bundle-add")
opts="--help --url --contenturl --versionurl --port --path --format --force --nosigcheck --ignore-time --statedir --certpath --time --no-scripts --no-boot-update "
break;;
("bundle-remove")
opts="--help --path --url --contenturl --versionurl --port --format --force --nosigcheck --ignore-time --statedir --certpath "
break;;
("bundle-list")
opts="--help --all --url --contenturl --versionurl --path --format --nosigcheck --ignore-time --statedir --certpath --deps --has-dep "
break;;
("hashdump")
opts="--help --no-xattrs --path "
break;;
("update")
opts="--help --download --url --port --contenturl --versionurl --status --format --path --force --nosigcheck --ignore-time --statedir --certpath --time --no-scripts --no-boot-update --migrate --allow-mix-collisions "
break;;
("verify")
opts="--help --manifest --path --url --port --contenturl --versionurl --fix --picky --picky-tree --picky-whitelist --install --format --quick --force --nosigcheck --ignore-time --statedir --certpath --time --no-scripts --no-boot-update "
break;;
("check-update")
opts="--help --url --versionurl --port --format --force --nosigcheck --path --statedir "
break;;
("search")
opts="--help --library --binary --scope --top --csv --display-files --init --ignore-time --url --contenturl --versionurl --port --path --format --statedir --certpath "
break;;
("info")
opts=""
break;;
("clean")
opts="--all --dry-run --statedir --help "
break;;
esac
done
# Add in additional completion options if we need to
if (( $i >= 0 ))
then
case "${COMP_WORDS[$i]}" in
("bundle-add")
MoM=""
if [ -r /usr/share/clear/bundles/.MoM ]
then MoM=/usr/share/clear/bundles/.MoM
elif [ -r /var/lib/swupd/version ] &&
installed=$(</var/lib/swupd/version) &&
[ -r /var/lib/swupd/$installed/Manifest.MoM ]
then
MoM=/var/lib/swupd/$installed/Manifest.MoM
fi
if [ -n "$MoM" ]
then
opts+="$( sed '1,/^$/d; s/^.*\t/ /' "$MoM" | LC_ALL=C sort )"
fi ;;
("bundle-remove")
opts+=" $(unset CDPATH; test -d /usr/share/clear/bundles && \
cd /usr/share/clear/bundles && LC_ALL=C ls | grep -v '^os-core' )"
;;
("hashdump")
# Add in filenames. TODO add in directory completion
opts+=" $( compgen -f -- "$2" )"
;;
esac
fi
COMPREPLY=($(compgen -W "${opts}" -- ${2}));
return 0
}
if [ ${BASH_VERSINFO[0]} -gt 4 ] || ( [ ${BASH_VERSINFO[0]} -eq 4 ] && [ ${BASH_VERSINFO[1]} -ge 4 ] )
then
complete -F _swupd -o nosort swupd
else
complete -F _swupd swupd
fi
+1 -1
View File
@@ -15,6 +15,6 @@ load "../../swupdlib"
}
@test "autocomplete has expected hashdump opts" {
grep -q 'opts="-h --help -n --no-xattrs -p --path "' $SRCDIR/swupd.bash
grep -q 'opts="--help --no-xattrs --path "' $SRCDIR/swupd.bash
}
# vi: ft=sh ts=8 sw=2 sts=2 et tw=80