Skip to content

Commit fb522fc

Browse files
bors[bot]luis4a0
andauthored
Merge #2119
2119: Bash completions for the aliases features r=townsend2010 a=luis4a0 This PR adds completion for the Multipass commands `alias`, `unalias` and `aliases`. Co-authored-by: Luis Peñaranda <[email protected]>
2 parents e035296 + 582a362 commit fb522fc

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

completions/bash/multipass

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,54 @@ _multipass_complete()
4848
opts=$( \eval $cmd | \grep -Ev '(\+--|Name)' | \cut -d',' -f 1 )
4949
}
5050

51+
_multipass_instances_with_colon()
52+
{
53+
local opts_bak="${opts}"
54+
opts=""
55+
56+
_multipass_instances
57+
local instances="${opts}"
58+
opts="$opts_bak"
59+
60+
for instance in ${instances}; do
61+
opts="${opts} ${instance}:"
62+
done
63+
}
64+
65+
# When we are completing the `alias` command, this function makes sure that each argument was specified at most
66+
# once. It fills $opts with the variables which were not yet specified.
67+
# Precondition: the command must be "alias".
68+
# Parameters: none.
69+
_unspecified_alias_vars()
70+
{
71+
opts=""
72+
73+
local help_found=0
74+
local definition_found=0
75+
local verbose_found=0
76+
77+
for ((i=2; i<COMP_CWORD; ++i)); do
78+
if [[ "${COMP_WORDS[$i]}" == '--help' ]]; then help_found=1; fi
79+
if [[ "${COMP_WORDS[$i]}" == '--verbose' ]]; then verbose_found=1; fi
80+
if [[ "${COMP_WORDS[$i]}" =~ ':' ]]; then definition_found=1; fi
81+
done
82+
83+
if [[ ${help_found} == 0 ]]; then opts="${opts} --help"; fi
84+
if [[ ${verbose_found} == 0 ]]; then opts="${opts} --verbose"; fi
85+
86+
if [[ ${definition_found} == 0 ]] && [[ "${cur}" != ':' ]]; then
87+
_multipass_instances_with_colon
88+
fi
89+
}
90+
91+
# Set $multipass_aliases to the list of available aliases.
92+
_multipass_aliases()
93+
{
94+
local cmd="multipass aliases --format=csv 2>/dev/null"
95+
96+
multipass_aliases=$( \eval $cmd | \grep -Ev '(\+--|Alias)' | \cut -d',' -f 1 )
97+
}
98+
5199
# Removes the comma or equals sign at the end of $cur.
52100
_remove_trailing_char()
53101
{
@@ -126,17 +174,22 @@ _multipass_complete()
126174
COMP_WORDBREAKS="${COMP_WORDBREAKS}="
127175
fi
128176

129-
local cur cmd opts prev prev2 prev_opts
177+
local cur cmd opts prev prev2 prev_opts multipass_aliases
130178
COMPREPLY=()
131179
cur="${COMP_WORDS[COMP_CWORD]}"
132180
prev="${COMP_WORDS[COMP_CWORD-1]}"
133181
prev2="${COMP_WORDS[COMP_CWORD-2]}"
134182
cmd="${COMP_WORDS[1]}"
135183
prev_opts=false
136184
multipass_cmds="transfer delete exec find help info launch list mount networks \
137-
purge recover shell start stop suspend restart umount version get set"
185+
purge recover shell start stop suspend restart umount version get set \
186+
alias aliases unalias"
187+
188+
if [[ "${multipass_cmds}" =~ " ${cmd} " || "${multipass_cmds}" =~ ^${cmd} || "${multipass_cmds}" =~ \ ${cmd}$ ]];
189+
then
190+
opts="--help --verbose"
191+
fi
138192

139-
opts="--help --verbose"
140193
case "${cmd}" in
141194
"info")
142195
opts="${opts} --all --format"
@@ -165,6 +218,13 @@ _multipass_complete()
165218
"find")
166219
opts="${opts} --show-unsupported --format"
167220
;;
221+
"aliases")
222+
opts="${opts} --format"
223+
;;
224+
"unalias")
225+
_multipass_aliases
226+
opts="${opts} ${multipass_aliases}"
227+
;;
168228
esac
169229

170230
if [[ ${prev} == -* ]]; then
@@ -266,14 +326,20 @@ _multipass_complete()
266326
return
267327
fi
268328
;;
329+
"alias")
330+
_unspecified_alias_vars
331+
# TODO: Do use spaces after the completion of an option starting with "--".
332+
compopt -o nospace
333+
;;
269334
"help")
270335
opts=$multipass_cmds
271336
;;
272337
esac
273338
fi
274339

275340
if [[ ${COMP_CWORD} -eq 1 ]]; then
276-
opts="${opts} ${multipass_cmds}"
341+
_multipass_aliases
342+
opts="${opts} ${multipass_cmds} ${multipass_aliases}"
277343
fi
278344

279345
if [[ -n "${opts}" ]]; then

0 commit comments

Comments
 (0)