Skip to content

Commit

Permalink
[bash] two part bash completion with restorable instances and then th…
Browse files Browse the repository at this point in the history
…eir respective snapshots
  • Loading branch information
sharder996 authored and ricab committed Aug 29, 2023
1 parent 8405785 commit 4ea4992
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions completions/bash/multipass
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,62 @@ _multipass_complete()
opts=$( \eval $cmd | \grep -Ev '(\+--|Name)' | \cut -d',' -f 1 )
}

_multipass_restorable_instances_with_period()
{
local instances=$( multipass info --all --no-runtime-information --format=csv \
| \awk -F'("[^"]*")?,|\r?\n' '$2 == "Stopped" && $16 > 0' \
| \grep -Ev '(\+--|Name)' \
| \cut -d',' -f 1 )
local found

_get_comp_words_by_ref -n := -w WORDS -i CWORD cur prev
for instance in $instances; do
found=0
for ((i=2; i<CWORD; i++)); do
if [[ "${WORDS[i]}" == ${instance} ]]; then
found=1
break
fi
done
if [ ${found} == 0 ]; then
opts="${opts} ${instance}."
fi
done
}

_unspecified_restore_vars()
{
opts=""

local destructive_found=0
local help_found=0
local instance_found=0
local verbose_found=0

for ((i=2; i<COMP_CWORD; ++i)); do
if [[ "${COMP_WORDS[$i]}" == '--destructive' ]]; then destructive_found=1; fi
if [[ "${COMP_WORDS[$i]}" == '--help' ]]; then help_found=1; fi
if [[ "${COMP_WORDS[$i]}" == '--verbose' ]]; then verbose_found=1; fi
if [[ "${COMP_WORDS[$i]}" =~ '.' ]]; then instance_found=1; fi
done

if [[ ${destructive_found} == 0 ]]; then opts="${opts} --destructive"; fi
if [[ ${help_found} == 0 ]]; then opts="${opts} --help"; fi
if [[ ${verbose_found} == 0 ]]; then opts="${opts} --verbose"; fi
if [[ ${instance_found} == 0 ]] && [[ "${cur}" =~ '.' ]]; then
local instance="${cur%%.*}"
local cmd="multipass info --snapshot-overview "${instance}" --format=csv 2>/dev/null"
local snapshots=$( \eval $cmd | \grep -Ev '(\+--|Instance)' | \cut -d',' -f 2 )

for snapshot in $snapshots; do
opts="${opts} ${instance}.${snapshot}"
done
elif [[ ${instance_found} == 0 ]]; then
_multipass_restorable_instances_with_period
compopt -o nospace
fi
}

_multipass_instances_with_colon()
{
local opts_bak="${opts}"
Expand Down Expand Up @@ -263,9 +319,6 @@ _multipass_complete()
"snapshot")
opts="${opts} --name --comment"
;;
"restore")
opts="${opts} --destructive"
;;
esac

if [[ ${prev} == -* ]]; then
Expand Down Expand Up @@ -343,9 +396,12 @@ _multipass_complete()
"recover")
_multipass_instances "Deleted"
;;
"snapshot"|"restore")
"snapshot")
_multipass_instances "Stopped"
;;
"restore")
_unspecified_restore_vars
;;
"mount")
local source_set=0
local prev
Expand Down

0 comments on commit 4ea4992

Please sign in to comment.