]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Fix `lxc-snapshot` completion.
authorEdênis Freindorfer Azevedo <edenisfa@gmail.com>
Mon, 30 Aug 2021 18:34:06 +0000 (15:34 -0300)
committerEdênis Freindorfer Azevedo <edenisfa@gmail.com>
Wed, 8 Sep 2021 01:23:57 +0000 (22:23 -0300)
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 <edenisfa@gmail.com>
config/bash/lxc.in

index 292e2694205a068c40688151029b22b25d335f91..94feb0f3cec1af9e4e87014d9ea87ebd6d03ff74 100644 (file)
@@ -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 )