Skip to content

Commit

Permalink
Add bash completions
Browse files Browse the repository at this point in the history
Signed-off-by: TobiPeterG <[email protected]>
  • Loading branch information
TobiPeterG committed Jan 16, 2025
1 parent c22d7f2 commit 7c61b5c
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
114 changes: 114 additions & 0 deletions completions/bash_sdbootutil
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# shellcheck disable=SC2148
shopt -s nullglob
# shellcheck disable=SC2148
err() {
:
}

_none() {
declare -ga result
result=()
}

_find_kernels()
{
local fn kv
declare -ga result
result=()

for fn in /usr/lib/modules/*/"$image"; do
kv="${fn%/*}"
kv="${kv##*/}"
result+=("$kv")
done
}

_find_entry_ids()
{
declare -ga result
result=()

mapfile -t result < <(bootctl list --json=short | jq -r '.[].id')
}

_arch_name() {
declare -ga result
result=("${!arch_image_map[@]}")
}

_image_name() {
declare -ga result
result=()
result=("${!arch_image_map[@]}")
}

_method() {
declare -ga result
result=()
for i in "tpm2" "tpm2+pin" "fido2" "password" "recovery-key"; do
result+=("$i")
done
}

_sdbootutil_completion() {
eval "$(sdbootutil _print_bash_completion_data)"
local cur prev words cword image
COMPREPLY=()
_get_comp_words_by_ref -n : cur prev words cword

local command_found=0
eval_bootctl
set_image_name
define_commands

define_options
# shellcheck disable=SC2154
IFS=, read -r -a opts <<< "$opts_long"
local opts_with_dashes=()
for opt in "${opts[@]}"; do
opts_with_dashes+=("--${opt//:/}")
done

local i=0

for word in "${words[@]:1:$cword-1}"; do
if [ -n "${commands["$word"]+yes}" ]; then
command_found=1
fi
if [ " --arch " == " $word " ]; then
# shellcheck disable=SC2034
firmware_arch="${words[i+2]}"
set_image_name
fi
if [ " --image " == " $word " ]; then
image="${words[i+2]}"
fi
((i++))
done

# shellcheck disable=SC2154
local opt_arg_fun="${options_with_arg[${prev#--}]}"
if [ "${commands["$prev"]}" == "kernel" ]; then
_find_kernels
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${result[*]}" -- "$cur") )
elif [ "${commands["$prev"]}" == "id" ]; then
_find_entry_ids
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${result[*]}" -- "$cur") )
elif [ "$opt_arg_fun" == "_path" ]; then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -d -- "$cur"))
elif [ "$opt_arg_fun" ]; then
eval "$opt_arg_fun"
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${result[*]}" -- "$cur") )
elif [[ $cur == -* ]] || [ $command_found -eq 1 ]; then
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts_with_dashes[*]}" -- "$cur") )
elif [ $command_found -eq 0 ]; then
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${!commands[*]}" -- "$cur") )
fi
}
complete -F _sdbootutil_completion sdbootutil
8 changes: 8 additions & 0 deletions sdbootutil
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,14 @@ bootloader_name()
####### main #######
if [ "$1" = "_print_bash_completion_data" ]; then
declare -f set_image_name
declare -f eval_bootctl
declare -f define_commands
declare -f define_options
exit 0
fi
define_options
getopt_tmp=$(getopt -o hc:v --long "$opts_long" -n "${0##*/}" -- "$@")
eval set -- "$getopt_tmp"
Expand Down
19 changes: 19 additions & 0 deletions sdbootutil.spec
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ JEOS module for full disk encryption enrollment. The module
present the different options and delegate into sdbootutil-enroll
service the effective enrollment.

%package bash-completion
Summary: Bash completions for sdbootutil
Requires: sdbootutil >= %{version}-%{release}
Requires: bash
Requires: bash-completion

%description bash-completion
Bash completions script for sdbootutil.
Allows the user to press TAB to see available commands,
options and parameters.

%prep
%setup -q

Expand Down Expand Up @@ -124,6 +135,9 @@ install -D -m 755 10-%{name}.tukit %{buildroot}%{_prefix}/lib/tukit/plugins/10-%
# kernel-install
install -D -m 755 50-%{name}.install %{buildroot}%{_prefix}/lib/kernel/install.d/50-%{name}.install

#bash completions
install -D -m 755 completions/bash_sdbootutil %{buildroot}%{_datadir}/bash-completion/completions/sdbootutil

# tmpfiles
install -D -m 755 kernel-install-%{name}.conf \
%{buildroot}%{_prefix}/lib/tmpfiles.d/kernel-install-%{name}.conf
Expand Down Expand Up @@ -195,4 +209,9 @@ sdbootutil update
%dir %{_unitdir}/jeos-firstboot.service.d
%{_unitdir}/jeos-firstboot.service.d/jeos-firstboot-enroll-override.conf

%files bash-completion
%dir %{_datadir}/bash-completion
%dir %{_datadir}/bash-completion/completions
%{_datadir}/bash-completion/completions/sdbootutil

%changelog

0 comments on commit 7c61b5c

Please sign in to comment.