From: EdĂȘnis Freindorfer Azevedo Date: Mon, 30 Aug 2021 18:34:06 +0000 (-0300) Subject: Fix `lxc-snapshot` completion. X-Git-Tag: lxc-4.0.11~88 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=289b75c8b34aec95e34778e62edd8b5d0bd8329b;p=thirdparty%2Flxc.git Fix `lxc-snapshot` completion. For options `-r,--restore` and `-d,--destroy`, we need the container name to create the list of completion values. Therefore, it is needed to scan the current command line to check if there is a container name available. Signed-off-by: EdĂȘnis Freindorfer Azevedo --- diff --git a/config/bash/lxc.in b/config/bash/lxc.in index 292e26942..94feb0f3c 100644 --- a/config/bash/lxc.in +++ b/config/bash/lxc.in @@ -42,7 +42,7 @@ _lxc_append_name() { # If `--name` or `-n` are present, do not complete with container names. for param in "${words[@]}"; do # Parse names from command line when space is escaped by backslash. - parsed="${param//[\\]}" + parsed="${param//[\\\"\']}" if [[ ${parsed} =~ ^--name(=(.*))?$ ]]; then return elif [[ ${parsed} =~ ${shortoptnamexp} ]]; then @@ -55,6 +55,44 @@ _lxc_append_name() { _lxc_names } +_lxc_get_snapshots() { + mapfile -t names < <(command lxc-ls -1) + local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$" + local container + local param + for i in "${!words[@]}"; do + param="${words[${i}]}" + parsed="${param//[\\\"\']}" + if [[ ${param} =~ ^--name(=(.*))?$ ]]; then + if [[ -n "${BASH_REMATCH[2]}" ]]; then + container="${BASH_REMATCH[2]}" + else + container="${words[${i}+1]}" + fi + break + elif [[ ${param} =~ ${shortoptnamexp} ]]; then + container="${words[${i}+1]}" + break + fi + for name in "${names[@]}"; do + if [[ "${parsed}" == "${name}" ]]; then + container="${name}" + break + fi + done + [[ -n "${container}" ]] && break + done + container="${container//[\\\"\']}" + mapfile -t snaps < <(command lxc-snapshot --name="${container}" --list) + local -r nosnapxp="^No snapshots$" + if [[ ! "${snaps[*]}" =~ ${nosnapxp} ]]; then + for i in "${!snaps[@]}"; do + read -r -e -a line <<< "${snaps[${i}]}" + printf "%s " "${line[0]}" + done + fi +} + _lxc_common_opt() { # End of options. if [[ "${words[*]}" =~ ' -- ' ]]; then @@ -724,14 +762,14 @@ _lxc_snapshot() { return ;; --destroy | -d ) - COMPREPLY=( $( compgen -W 'ALL $( lxc-snapshot --list )' -- "${cur}" ) ) + COMPREPLY=( $( compgen -W 'ALL $( _lxc_get_snapshots )' -- "${cur}" ) ) return ;; --list | -L | --showcomments | -C ) # Only flags. ;; --restore | -r ) - COMPREPLY=( $( compgen -W '$( lxc-snapshot --list )' -- "${cur}" ) ) + COMPREPLY=( $( compgen -W '$( _lxc_get_snapshots )' -- "${cur}" ) ) return ;; --newname | -N )