forked from tfmalt/bash-completion-virtualbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvboxmanage_completion.bash
277 lines (251 loc) · 8.09 KB
/
vboxmanage_completion.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bash
#
# Bash completion script for VBoxManage
#
# Copyright (c) 2012, Thomas Malt <[email protected]>
# Copyright (c) 2015, Benjamin Gandon <https://github.com/bgandon>
#
complete -F _vboxmanage 'vboxmanage'
complete -F _vboxmanage VBoxManage
# export VBOXMANAGE_NIC_TYPES
_vboxmanage() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# echo "cur: |$cur|"
# echo "prev: |$prev|"
case $prev in
# DEVELOPERS: here the commands are listed in alphabetical order
# so that it can stay consistent
-v|--version)
return 0
;;
controlvm)
opts=$(__vboxmanage_list-running-vms)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
getextradata|setextradata|showvminfo)
opts=$(__vboxmanage_list-all-vms)
if [[ $prev =~ [gs]etextradata ]]; then
opts="$opts global"
fi
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
modifyvm|startvm|unregistervm)
opts=$(__vboxmanage_list-stopped-vms)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
list)
opts=$(__vboxmanage_list)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
registervm)
# It's quite hard to manage sapces in filenames and tilde exampansion
# But the _filedir function in /etc/bash_completion does this for us.
# On Debian: /usr/share/bash-completion/bash_completion
# On OSX with Homebrew: /usr/local/etc/bash_completion
#
# See: <http://unix.stackexchange.com/a/77048>
_filedir vbox
return 0
;;
vboxmanage|VBoxManage)
opts="$(__vboxmanage_default)"
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
esac
local pprev=${COMP_WORDS[COMP_CWORD-2]}
# echo "previous: $pprev"
case $pprev in
getextradata|setextradata)
local context="$prev"
opts="$(__vboxmanage_list-keys "$context")"
if [ $pprev = getextradata ]; then
opts="$opts enumerate"
fi
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
list)
case $prev in
-l|--long)
opts=$(__vboxmanage_list -l --long)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
esac
;;
vboxmanage|VBoxManage)
case $prev in
-q|--nologo)
opts="$(__vboxmanage_default -q --nologo)"
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
esac
;;
esac
if echo " " $(__vboxmanage_list-all-vms) " " | grep -Fq " $prev "; then
local VM="$prev"
case $pprev in
startvm)
COMPREPLY=($(compgen -W "--type" -- $cur))
return 0
;;
controlvm)
opts=$(__vboxmanage_controlvm $VM)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0;
;;
showvminfo)
opts="--details --machinereadable --log"
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0;
;;
modifyvm)
opts=$(__vboxmanage_modifyvm)
COMPREPLY=($(compgen -W "$opts" -- $cur))
return 0
;;
esac
# else
# echo "'$prev' not in '$(__vboxmanage_list-all-vms)'"
fi
local ppprev=${COMP_WORDS[COMP_CWORD-2]}
case $ppprev in
controlvm)
case $prev in
nictrace*|setlinkstate*|videocap|vrde)
COMPREPLY=($(compgen -W "on off" -- $cur))
return 0
;;
esac
;;
modifyvm)
case $prev in
--nic[1-8])
opts=$(__vboxmanage_list-nic-types)
COMPREPLY=($(compgen -W "$opts" -- $cur))
;;
esac
;;
showvminfo)
COMPREPLY=($(compgen -W "--details --log --machinereadable" -- $cur))
return 0
;;
startvm)
case $prev in
--type)
COMPREPLY=($(compgen -W "gui headless separate" -- $cur))
return 0
;;
esac
;;
unregistervm)
COMPREPLY=($(compgen -W "--delete" -- $cur))
return 0
;;
esac
# echo "Got to end withoug completion"
}
function __vboxmanage_list-keys {
local ctx="$1"
VBoxManage getextradata "$ctx" enumerate | sed -ne 's/^Key: \([^,]*\), Value:.*$/\1/p'
}
__vboxmanage_list-nic-types() {
VBoxManage \
| grep -F ' nic<' \
| sed -e 's/.*nic<1-N> \([a-z\|]*\).*/\1/' \
| tr '|' ' '
}
function __vboxmanage_extract-vm-names {
cut -d' ' -f1 | tr -d '"'
}
function __vboxmanage_list-all-vms {
VBoxManage list vms | __vboxmanage_extract-vm-names
}
function __vboxmanage_list-running-vms {
VBoxManage list runningvms | __vboxmanage_extract-vm-names
}
function __vboxmanage_exclude {
local list="$1"
local filtered word excluded excl_word
for word in $list; do
excluded=false
for excl_word in "$@"; do
if [ "$word" = "$excl_word" ]; then
excluded=true
break
fi
done
if [ $excluded = false ]; then
filtered="$filtered $word"
fi
done
echo $filtered
}
function __vboxmanage_list-stopped-vms {
__vboxmanage_exclude "$(__vboxmanage_list-all-vms)" $(__vboxmanage_list-running-vms)
}
__vboxmanage_list() {
local opts=$(VBoxManage list | tr -s '\n[]|' ' ' | cut -d' ' -f4-)
__vboxmanage_exclude "$opts" "$@"
}
__vboxmanage_controlvm() {
local vm="$1"
local opts=$( \
VBoxManage controlvm \
| grep '^ [a-z]' \
| sed -e 's/^ *\([a-z<1N>|-]\{1,\}\).*/\1/; s/\|$//' \
| tr -d ' ' \
| tr '|' '\n' \
)
local nic_or_nat_opts_pattern='^(setlinkstate|(nic|nat)[a-z]*)<1-N>'
# NOTE: here we quote "$opts" in order to preserve newlines,
# which enables us to properly use 'grep' and 'grep -v'
local nic_opts="setlinkstate $(echo "$opts" | grep -E "$nic_or_nat_opts_pattern" | sed -e 's/<1-N>//')"
opts=$(echo "$opts" | grep -Ev "$nic_or_nat_opts_pattern")
# setlinkstate<1-N> nic<1-N> nictrace<1-N> nictracefile<1-N> nicproperty<1-N> nicpromisc<1-N> natpf<1-N> natpf<1-N>
local num nic_opt nic_cmds
for num in $(__vboxmanage_list-active-nic-nums "$vm"); do
for nic_opt in $nic_opts; do
nic_cmds="$nic_cmds $nic_opt$num"
done
done
echo $opts $nic_cmds | tr ' ' '\n' | sort | uniq
}
__vboxmanage_modifyvm() {
local opts=$( \
VBoxManage modifyvm \
| grep -F '[--' \
| grep -Fv '[--nic<' \
| sed 's/ *\[--\([a-z-]*\).*/--\1/' \
| sort \
)
# Exceptions
for i in {1..8}; do
opts="$opts --nic${i}"
done
echo $opts
}
function __vboxmanage_list-active-nic-nums {
local vm="$1"
VBoxManage showvminfo "$vm" --machinereadable \
| awk '/^nic[1-8]=/ && ! /="none"$/' \
| cut -d= -f1 \
| sed -e 's/^nic//'
}
__vboxmanage_default() {
local help_text=$(VBoxManage)
# Note: the default sed in OS X only supports \{1,\} and not \+
# that's why we fallback to the latter here
local opts=$(echo "$help_text" | sed -n -e 's/^ \{2\}\[\([a-z|-]\{1,\}\).*$/\1/p' | tr '|' ' ')
local cmds=$(echo "$help_text" | sed -n -e 's/^ \{2\}\([a-z-]\{1,\}\).*$/\1/p' | uniq)
__vboxmanage_exclude "$opts $cmds" "$@"
}