Skip to content

Instantly share code, notes, and snippets.

@s-hemer
Created August 24, 2023 14:19
Show Gist options
  • Save s-hemer/760f13ced6f4f6d99b0cddde5757470f to your computer and use it in GitHub Desktop.
Save s-hemer/760f13ced6f4f6d99b0cddde5757470f to your computer and use it in GitHub Desktop.
Auto-completionscript for bmaptool
#/usr/bin/env bash
function exists_in_list()
{
LIST=$1
DELIMITER=$2
VALUE=$3
[[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]]
}
_bmaptool_completions()
{
local current=""
local previous=""
local command=""
if [[ ${COMP_CWORD} > 0 ]] ; then
current="${COMP_WORDS[COMP_CWORD]}"
if [[ ${COMP_CWORD} > 1 ]] ; then
command="${COMP_WORDS[1]}"
previous="${COMP_WORDS[COMP_CWORD - 1]}"
fi
fi
local help_options="-h --help"
local bmap_options="--bmap --nobmap"
local sig_options="--bmap-sig --no-sig-verify --no-verify"
local output_options="-o --output"
if [[ ${COMP_CWORD} == 1 ]] ; then
COMPREPLY=( $(compgen -W "${help_options} create copy" -- ${current}) )
return 0
fi
if [[ ${command} == copy ]] ; then
if [[ ${previous} == ${command} ]] ; then
#only add help if we are first after command
COMPREPLY+=( $(compgen -W "${help_options}" -- ${current}) )
fi
if exists_in_list "${help_options}" " " ${previous} ; then
# if we have choosen help option, nothing should follow
return 0
fi
if [[ ${previous} == "--bmap" ]] ; then
# --bmap BMAP the block map file for the image
COMPREPLY=( $(compgen -f -- ${current}) )
elif [[ ${previous} == "--bmap-sig" ]] ; then
# --bmap-sig BMAP_SIG the detached GPG signature for the bmap file
COMPREPLY=( $(compgen -f -- ${current}) )
elif [[ ${previous} == "--psplash-pipe" ]] ; then
# --psplash-pipe PSPLASH_PIPE
COMPREPLY=( $(compgen -f -- ${current}) )
else
COMPREPLY+=( $(compgen -W "${bmap_options} ${sig_options}" -- ${current}) )
# image the image file to copy. Supported formats: uncompressed, bz2, gz, xz, lzo, lz4, zst,..
# dest the destination file or device node to copy the image to
COMPREPLY+=( $(compgen -f -- ${current}) )
fi
return 0
fi
if [[ ${command} == create ]] ; then
if [[ ${previous} == ${command} ]] ; then
COMPREPLY+=( $(compgen -W "${help_options}" -- ${current}) )
fi
if exists_in_list "${help_options}" " " ${previous} ; then
return 0
fi
if exists_in_list "${output_options}" " " ${previous} ; then
#options:
# -o OUTPUT, --output OUTPUT
# the output file name (otherwise stdout is used)
#if output is previous, a file must follow
COMPREPLY=( $(compgen -f -- ${current}) )
return 0
else
COMPREPLY+=( $(compgen -W "${output_options} --no-checksum" -- ${current}) )
# image the image to generate bmap for
COMPREPLY+=( $(compgen -f -- ${current}) )
return 0
fi
fi
}
complete -o filenames -F _bmaptool_completions bmaptool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment