mirror of
https://github.com/clearlinux/swupd-client.git
synced 2026-09-08 06:31:50 +00:00
bash 4.4 added the 'nosort' option for completion. Use this so the flag options come before the bundle names. Signed-off-by: Icarus Sparry <icarus.w.sparry@intel.com>
163 lines
4.7 KiB
Bash
Executable File
163 lines
4.7 KiB
Bash
Executable File
#!/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)
|
|
|
|
# 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 | 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_VERSION[0]} -gt 4 ] || ( [ ${BASH_VERSION[0]} -eq 4 ] && [ $BASH_VERSION[1] -ge 4] )
|
|
then
|
|
complete -F _swupd -o nosort swupd
|
|
else
|
|
complete -F _swupd swupd
|
|
fi
|
|
|
|
EOM
|
|
} > $SCRIPTNAME
|
|
|