diff --git a/.github/workflows/system-tests.yml b/.github/workflows/system-tests.yml index 4525f902..4353d62a 100644 --- a/.github/workflows/system-tests.yml +++ b/.github/workflows/system-tests.yml @@ -19,7 +19,7 @@ jobs: - name: Build and install application run: | meson setup build --buildtype=release - meson install -C build + sudo meson install -C build - name: Setup loopdev and symlink run: | dd if=/dev/zero of=loop-file bs=1M count=100 diff --git a/AUTHORS.rst b/AUTHORS.rst index d0bc5a3f..eaaf8ea5 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -6,4 +6,5 @@ Martin Schwan Contributors ============ -Leonard Anderweit +* Leonard Anderweit +* Steffen Hemer diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5c97a8f6..1a801425 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,13 @@ - Add a FAQ section to the documentation explaining the ``Filesystem has unsupported feature(s)`` error of ``resize2fs``, caused by the ext4 ``orphan_file`` feature and old e2fsprogs versions. +- Add a Bash completion script, which completes commands, options and their + arguments. It is installed together with partup. + +.. rubric:: Contributors + +`Steffen Hemer `__, +`Martin Schwan `__ .. _release-4.0.0: diff --git a/completions/bash/partup b/completions/bash/partup new file mode 100644 index 00000000..5d87699f --- /dev/null +++ b/completions/bash/partup @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-or-later +# +# Bash completion for partup + +_partup_commands="install package show" +_partup_help_options="-h --help -V --version" +_partup_app_options="-q --quiet -d --debug -D --debug-domains" +_partup_install_options="-s --skip-checksums" +_partup_package_options="-f --force -C --directory" +_partup_show_options="-s --size" + +_partup_in_list() +{ + local list=$1 + local value=$2 + + [[ " ${list} " == *" ${value} "* ]] +} + +_partup_complete_files() +{ + local pattern=$1 + local current=$2 + + if [[ -n ${pattern} ]]; then + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -f -X "!${pattern}" -- "${current}") + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -d -- "${current}") + else + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -f -- "${current}") + fi +} + +_partup_completions() +{ + local current="${COMP_WORDS[COMP_CWORD]}" + local previous="" + local command="" + local -a positionals=() + local i word skip=0 + + COMPREPLY=() + + if (( COMP_CWORD > 0 )); then + previous="${COMP_WORDS[COMP_CWORD - 1]}" + fi + + # Find the command and the positional arguments in front of the cursor. + # Arguments of options that take a value are skipped. + for (( i = 1; i < COMP_CWORD; i++ )); do + word="${COMP_WORDS[i]}" + + if (( skip )); then + skip=0 + continue + fi + + case "${word}" in + -D|--debug-domains|-C|--directory) + skip=1 + continue + ;; + -*) + continue + ;; + esac + + if [[ -z ${command} ]] && _partup_in_list "${_partup_commands}" "${word}"; then + command="${word}" + else + positionals+=( "${word}" ) + fi + done + + # Complete the argument of an option that takes a value. + case "${previous}" in + -D|--debug-domains) + return 0 + ;; + -C|--directory) + if [[ ${command} == package ]]; then + mapfile -t COMPREPLY < <(compgen -d -- "${current}") + return 0 + fi + ;; + esac + + case "${current}" in + --debug-domains=*) + return 0 + ;; + --directory=*) + if [[ ${command} == package ]]; then + mapfile -t COMPREPLY < <(compgen -d -P "--directory=" -- "${current#--directory=}") + return 0 + fi + ;; + esac + + if [[ -z ${command} ]]; then + mapfile -t COMPREPLY < <(compgen -W "${_partup_help_options} ${_partup_commands} ${_partup_app_options}" -- "${current}") + return 0 + fi + + local options="${_partup_app_options}" + + # Only offer help directly after the command + if (( ${#positionals[@]} == 0 )) && [[ ${previous} == "${command}" ]]; then + options+=" ${_partup_help_options}" + fi + + case "${command}" in + install) + options+=" ${_partup_install_options}" + ;; + package) + options+=" ${_partup_package_options}" + ;; + show) + options+=" ${_partup_show_options}" + ;; + esac + + if [[ ${current} == -* ]]; then + mapfile -t COMPREPLY < <(compgen -W "${options}" -- "${current}") + return 0 + fi + + case "${command}" in + install) + # install PACKAGE DEVICE + case ${#positionals[@]} in + 0) + _partup_complete_files "*.partup" "${current}" + ;; + 1) + mapfile -t COMPREPLY < <(compgen -f -- "${current:-/dev/}") + ;; + esac + ;; + package) + # package PACKAGE FILES... + if (( ${#positionals[@]} == 0 )); then + _partup_complete_files "*.partup" "${current}" + else + _partup_complete_files "" "${current}" + fi + ;; + show) + # show PACKAGE + if (( ${#positionals[@]} == 0 )); then + _partup_complete_files "*.partup" "${current}" + fi + ;; + esac + + if [[ -z ${current} ]] && (( ${#COMPREPLY[@]} == 0 )); then + mapfile -t COMPREPLY < <(compgen -W "${options}" -- "${current}") + fi + + return 0 +} + +complete -o filenames -F _partup_completions partup diff --git a/debian/dirs b/debian/dirs deleted file mode 100644 index e7724817..00000000 --- a/debian/dirs +++ /dev/null @@ -1 +0,0 @@ -usr/bin diff --git a/debian/partup.bash-completion b/debian/partup.bash-completion deleted file mode 100644 index c6b068de..00000000 --- a/debian/partup.bash-completion +++ /dev/null @@ -1 +0,0 @@ -partup-completion.bash diff --git a/debian/rules b/debian/rules index 79d7d8c3..71a79be6 100755 --- a/debian/rules +++ b/debian/rules @@ -3,7 +3,7 @@ export DH_VERBOSE=1 %: - dh $@ --with bash-completion + dh $@ override_dh_auto_configure: rm -rf build diff --git a/doc/usage.rst b/doc/usage.rst index 7d24f51a..0b60f4ee 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -61,6 +61,19 @@ version .. deprecated:: 5.0.0 Use the global ``-V, --version`` option instead. +Shell Completion +---------------- + +partup ships a completion script for Bash. It completes commands, options and +their arguments, e.g. directories for ``--directory``, ``*.partup`` files for +the *PACKAGE* argument and device paths for the *DEVICE* argument. + +The script is installed together with partup to the ``bash-completion`` +directory of the installation prefix. To use it without installing partup, +source the script ``completions/bash/partup`` from the source tree:: + + source completions/bash/partup + Supported Output Devices ------------------------ diff --git a/meson.build b/meson.build index d873bd5f..6bee6d1e 100644 --- a/meson.build +++ b/meson.build @@ -56,6 +56,21 @@ exec = executable( install : true ) +bash_completion_dep = dependency('bash-completion', required : false) +if bash_completion_dep.found() + bash_completion_dir = bash_completion_dep.get_variable( + 'completionsdir', + pkgconfig_define : ['datadir', get_option('prefix') / get_option('datadir')] + ) +else + bash_completion_dir = get_option('prefix') / get_option('datadir') / 'bash-completion' / 'completions' +endif + +install_data( + 'completions/bash/partup', + install_dir : bash_completion_dir +) + partup_dep = declare_dependency( dependencies : deps, include_directories : ['src'], diff --git a/partup-completion.bash b/partup-completion.bash deleted file mode 100644 index 80dd36dd..00000000 --- a/partup-completion.bash +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env bash - -partup_commands="install package show" -help_options="-h --help -V --version" -app_options="-q --quiet -d --debug -D --debug-domains" -install_options="-s --skip-checksums" -package_options="-f --force -C --directory" -show_options="-s --size" - -function exists_in_list() -{ - LIST=$1 - DELIMITER=$2 - VALUE=$3 - [[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]] -} - -function add_command_help() -{ - 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 - COMPREPLY=() - return 1 - fi - return 0 -} - - -_partup_completions() -{ - local current="" - local previous="" - local command="" - - if (( ${COMP_CWORD} > 0 )) ; then - current="${COMP_WORDS[COMP_CWORD]}" - - if (( ${COMP_CWORD} > 1 )) ; then - # iterate over all COMP_WORDS - for s in "${COMP_WORDS[@]:1}"; do - if exists_in_list "${partup_commands}" " " "${s}" ; then - command="${s}" - fi - done - previous="${COMP_WORDS[COMP_CWORD - 1]}" - fi - fi - - if [[ -z ${command} ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - COMPREPLY+=( $(compgen -W "${partup_commands} ${app_options}" -- ${current}) ) - return 0 - fi - - # we may have things like "partup --debug install ..." - - if [[ ${command} == install ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${install_options} ${app_options}" -- ${current}) ) - - # PACKAGE - COMPREPLY+=( $(compgen -f -o plusdirs -X '!*.partup' -- ${current}) ) - - # DEVICE -> if previous ends with partup, complete only from /dev - if [[ ${previous} == *.partup ]] ; then - if [[ -z ${current} ]] ; then - COMPREPLY=( '/dev' ) - return 0 - else - COMPREPLY+=( $(compgen -f -o plusdirs -- ${current} ) ) - fi - fi - - # don't allow options after dev - if [[ ${previous} == /dev/* ]] ; then - COMPREPLY=() - fi - - return 0 - fi - - if [[ ${command} == package ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${package_options} ${app_options}" -- ${current}) ) - - # -C, --directory=DIR - # PACKAGE FILES… - - if exists_in_list "${package_options}" " " ${previous} ; then - #if output is previous, a file must follow - COMPREPLY=( $(compgen -f -- ${current}) ) - fi - - return 0 - fi - - if [[ ${command} == show ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${show_options} ${app_options}" -- ${current}) ) - - # PACKAGE - if we do not already have one, complete available partup files - if [[ ${previous} != *.partup ]] ; then - COMPREPLY+=( $(compgen -f -o plusdirs -X '!*.partup' -- ${current}) ) - fi - - return 0 - fi - - return 1 -} - -complete -o filenames -F _partup_completions partup diff --git a/tools/debian-package/Containerfile b/tools/debian-package/Containerfile index 8a819c1b..6ace2010 100644 --- a/tools/debian-package/Containerfile +++ b/tools/debian-package/Containerfile @@ -3,6 +3,7 @@ FROM ubuntu:24.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && \ apt-get install --yes \ + bash-completion \ debhelper \ git \ meson \