Add support for container composed names.

When a container name has whitespace in it
(e.g. created by `lxc-create -t download -n "arch linux"` ),
the completion for other commands should be able to work by adding a
backslash to escape it.

Although it may be interesting to support names between quotes, this
would probably means to have to add quotes to all names. Might not be
interesting just due to an edge case.

Signed-off-by: Edênis Freindorfer Azevedo <edenisfa@gmail.com>
This commit is contained in:
Edênis Freindorfer Azevedo 2021-08-30 10:33:35 -03:00
parent 8e4c68e67a
commit 6139460643
No known key found for this signature in database
GPG Key ID: F7F1907D677FB8C9

View File

@ -1,42 +1,55 @@
# lxc-* commands completion # lxc-* commands completion
_lxc_names() { _lxc_names() {
declare -a names
case ${words[0]} in case ${words[0]} in
lxc-attach | lxc-cgroup | lxc-checkpoint | lxc-console | lxc-device | lxc-freeze | lxc-stop ) lxc-attach | lxc-cgroup | lxc-checkpoint | lxc-console | lxc-device | lxc-freeze | lxc-stop )
COMPREPLY=( $( compgen -W "$( command lxc-ls --running )" -- "$cur" ) ) mapfile -t names < <(command lxc-ls --running -1)
;; ;;
lxc-destroy | lxc-execute | lxc-snapshot | lxc-start ) lxc-destroy | lxc-execute | lxc-snapshot | lxc-start )
COMPREPLY=( $( compgen -W "$( command lxc-ls --stopped )" -- "$cur" ) ) mapfile -t names < <(command lxc-ls --stopped -1)
;; ;;
lxc-copy | lxc-info | lxc-monitor | lxc-wait ) lxc-copy | lxc-info | lxc-monitor | lxc-wait )
COMPREPLY=( $( compgen -W "$( command lxc-ls --defined )" -- "$cur" ) ) mapfile -t names < <(command lxc-ls --defined -1)
;; ;;
lxc-autostart | lxc-create | lxc-checkconfig | lxc-config | lxc-ls | \ lxc-autostart | lxc-create | lxc-checkconfig | lxc-config | lxc-ls | \
lxc-top | lxc-unshare | lxc-update-config | lxc-usernsexec ) lxc-top | lxc-unshare | lxc-update-config | lxc-usernsexec )
;; ;;
lxc-unfreeze ) lxc-unfreeze )
COMPREPLY=( $( compgen -W "$( command lxc-ls --frozen )" -- "$cur" ) ) mapfile -t names < <(command lxc-ls --frozen -1)
;; ;;
*) *)
# If we are running as an alias or symlink with different name, # If we are running as an alias or symlink with different name,
# fallback to old behaviour. # fallback to old behaviour.
COMPREPLY=( $( compgen -W "$( command lxc-ls )" -- "$cur" ) ) mapfile -t names < <(command lxc-ls -1)
;; ;;
esac esac
COMPREPLY=()
for i in "${!names[@]}"; do
# For composed names with spaces escaped by '\'.
names[${i}]=$(command printf "%q" "${names[${i}]}")
if [[ -n $(compgen -W "${names[${i}]}" -- "$cur") ]]; then
COMPREPLY+=("${names[${i}]}")
fi
done
} }
_lxc_append_name() { _lxc_append_name() {
local vms=$(command lxc-ls) mapfile -t names < <(command lxc-ls -1)
local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$" local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$"
local parsed
# If `--name` or `-n` are present, do not complete with container names. # If `--name` or `-n` are present, do not complete with container names.
for param in ${words[@]}; do for param in "${words[@]}"; do
if [[ ${param} =~ ^--name(=(.*))?$ ]]; then # Parse names from command line when space is escaped by backslash.
parsed="${param//[\\]}"
if [[ ${parsed} =~ ^--name(=(.*))?$ ]]; then
return return
elif [[ ${param} =~ ${shortoptnamexp} ]]; then elif [[ ${parsed} =~ ${shortoptnamexp} ]]; then
return return
fi fi
for vm in ${vms[@]}; do for name in "${names[@]}"; do
[[ "${param}" = "${vm}" ]] && return [[ "${parsed}" = "${name}" ]] && return
done done
done done
_lxc_names _lxc_names