bash completion: various machinectl/portablectl fixes (#40719)

This commit is contained in:
Luca Boccassi
2026-02-18 11:08:29 +00:00
committed by GitHub
2 changed files with 86 additions and 12 deletions
+85 -11
View File
@@ -25,33 +25,43 @@ __contains_word() {
}
__get_machines() {
local mode=$1; shift 1
local a b
{ machinectl list-images --full --no-legend --no-pager 2>/dev/null; machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
{ machinectl $mode list-images --full --no-legend --no-pager 2>/dev/null; machinectl $mode list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
{ while read -r a b; do echo " $a"; done; } |
sort -u
}
_machinectl() {
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
local i verb comps
local i verb comps mode
local -A OPTS=(
[STANDALONE]='--all -a -l --full --help -h --no-ask-password --no-legend --no-pager --version --value
--mkdir --read-only --force -q --quiet'
[ARG]='--host -H --kill-whom -M --machine --property -p --signal -s --uid -E --setenv -n --lines
-o --output --verify --format --max-addresses'
--mkdir --read-only --force -q --quiet --system --user --now -V'
[ARG]='--host -H --kill-whom -M --machine --property -p -P --signal -s --uid -E --setenv -n --lines
-o --output --verify --format --max-addresses --runner'
)
local -A VERBS=(
[STANDALONE]='list list-images clean pull-tar pull-raw list-transfers cancel-transfer import-fs'
[MACHINES]='status show start stop login shell enable disable poweroff reboot terminate kill bind
copy-to copy-from image-status show-image clone rename read-only remove set-limit
export-tar export-raw'
[MACHINES]='status show start stop login shell enable disable poweroff reboot terminate kill
image-status show-image remove export-tar export-raw'
[MACHINES_OR_FILES]='edit cat'
[MACHINE_ONLY]='clone rename set-limit'
[READONLY]='read-only'
[FILE]='import-tar import-raw'
[MACHINES_AND_FILES]='copy-to copy-from bind'
)
_init_completion || return
if __contains_word "--user" ${COMP_WORDS[*]}; then
mode=--user
else
mode=--system
fi
for ((i=0; i <= COMP_CWORD; i++)); do
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
@@ -73,9 +83,9 @@ _machinectl() {
comps=$(compgen -A hostname)
;;
--machine|-M)
comps=$( __get_machines )
comps=$( __get_machines $mode )
;;
--property|-p)
--property|-p|-P)
comps=''
;;
--output|-o)
@@ -87,6 +97,9 @@ _machinectl() {
--format)
comps='uncompressed xz gzip bzip2 zstd'
;;
--runner)
comps='nspawn vmspawn'
;;
esac
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0
@@ -104,7 +117,68 @@ _machinectl() {
comps=''
elif __contains_word "$verb" ${VERBS[MACHINES]}; then
comps=$( __get_machines )
comps=$( __get_machines $mode )
elif __contains_word "$verb" ${VERBS[MACHINES_OR_FILES]}; then
comps=$( __get_machines $mode )
compopt -o filenames
comps+=" $(compgen -f -- "$cur")"
elif __contains_word "$verb" ${VERBS[MACHINE_ONLY]}; then
local k args=0
for ((k=i+1; k < COMP_CWORD; k++)); do
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then
((k++))
elif [[ ${COMP_WORDS[k]} = -* ]]; then
continue
else
((args++))
fi
done
if (( args == 0 )); then
comps=$( __get_machines $mode )
else
comps=''
fi
elif __contains_word "$verb" ${VERBS[READONLY]}; then
local k args=0
for ((k=i+1; k < COMP_CWORD; k++)); do
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then
((k++))
elif [[ ${COMP_WORDS[k]} = -* ]]; then
continue
else
((args++))
fi
done
if (( args == 0 )); then
comps=$( __get_machines $mode )
elif (( args == 1 )); then
comps='true false'
else
comps=''
fi
elif __contains_word "$verb" ${VERBS[MACHINES_AND_FILES]}; then
local k args=0
for ((k=i+1; k < COMP_CWORD; k++)); do
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then
((k++))
elif [[ ${COMP_WORDS[k]} = -* ]]; then
continue
else
((args++))
fi
done
if (( args == 0 )); then
comps=$( __get_machines $mode )
elif (( args <= 2 )); then
comps=$(compgen -f -- "$cur")
compopt -o filenames
else
comps=''
fi
elif __contains_word "$verb" ${VERBS[FILE]}; then
if (( COMP_CWORD == i + 1 )); then # first argument after verb
+1 -1
View File
@@ -37,7 +37,7 @@ _portablectl() {
local -A OPTS=(
[STANDALONE]='-q --quiet --runtime --no-reload --cat --no-pager --no-legend
--no-ask-password --enable --now -h --help --version
--clean --no-block --force'
--clean --no-block --force --user --system'
[ARG]='-p --profile --copy -H --host -M --machine --extension'
)