Rewriting implementation

This commit is contained in:
2015-04-07 17:32:03 +02:00
parent ca68fe953b
commit e9401c4cfe
2 changed files with 206 additions and 218 deletions

10
README
View File

@@ -1,3 +1,11 @@
VBoxManage bash completion script. VBoxManage bash completion script.
This is improved version of the completion by Sebastian T. Hafner This version of the completion was initially based on Sebastian T. Hafner
script. However, in some point of time I've decided to revrite it almost from
scratch.
Current version of script was written and tested against VBoxManage in version
4.3.18, and supports dcommands:
- adoptstate
- bandwidthctl

View File

@@ -1,18 +1,19 @@
# bash command-line completion for virtualbox # bash command-line completion for virtualbox
# development version, or specially used for my opts
# Author: Sebastian T. Hafner <sonix@own-hero.net>
# #
# This version of bash completion was born due to the need of fast and easy # This version of bash completion was born due to the need of fast and easy
# access to the maze of commands and parameters VBoxManage provides. Based on # access to the maze of commands and parameters VBoxManage provides. Based on
# Sebastian script I've managed to improve it and adapt to newest stable version # Sebastian[1] script I've managed to improve it and adapt to newest stable
# available in Gentoo Portage. # version available in Gentoo Portage.
#
# [1] Sebastian T. Hafner <sonix@own-hero.net>
#
# #
# [x] adoptstate # [x] adoptstate
# [ ] bandwidthctl # [x] bandwidthctl
# [x] clonehd # [ ] clonehd
# [x] clonevm # [ ] clonevm
# [ ] closemedium # [ ] closemedium
# [x] controlvm # [ ] controlvm
# [ ] convertfromraw # [ ] convertfromraw
# [ ] createhd # [ ] createhd
# [ ] createvm # [ ] createvm
@@ -26,32 +27,32 @@
# [ ] guestproperty # [ ] guestproperty
# [ ] hostonlyif # [ ] hostonlyif
# [ ] import # [ ] import
# [x] list # [ ] list
# [ ] metrics # [ ] metrics
# [ ] modifyhd # [ ] modifyhd
# [.] modifyvm - a LOT options missing # [ ] modifyvm - a LOT options missing
# [ ] natnetwork # [ ] natnetwork
# [ ] registervm # [ ] registervm
# [ ] setextradata # [ ] setextradata
# [ ] setproperty # [ ] setproperty
# [ ] sharedfolder # [ ] sharedfolder
# [x] showhdinfo # [ ] showhdinfo
# [x] showvminfo # [ ] showvminfo
# [x] snapshot # [ ] snapshot
# [x] startvm # [ ] startvm
# [o] storageattach - no all options yet # [ ] storageattach - no all options yet
# [ ] storagectl # [ ] storagectl
# [x] unregistervm # [ ] unregistervm
# [ ] usbfilter # [ ] usbfilter
_VBoxManage() { _VBoxManage() {
local cur prev opts vms vms cmd count item local cur prev opts vms vms cmd subcommand count item tmp name index
# Generate registered hard disk files. # Generate registered hard disk files.
# NOTE: This function may introduce some quirks, if there is a space or # NOTE: This function may introduce some quirks, if there is a space or
# other characters which usually are treated as IFS - like space. Pipe # other characters which usually are treated as IFS - like space. Pipe
# character used in disk filename will ruin this completions. # character used in disk filename will ruin this completions.
_hdcomp() { _hdd_comp() {
local cur=$1 local cur=$1
local hdds local hdds
@@ -72,7 +73,7 @@ _VBoxManage() {
# Complete registered VM names. # Complete registered VM names.
# Issues are the same as in above function. # Issues are the same as in above function.
_vmscomp() { _vms_comp() {
local cur=$1 local cur=$1
local vms local vms
@@ -89,11 +90,89 @@ _VBoxManage() {
done done
} }
_list_comp() {
local cur=$1
local list
list=$(VBoxManage list | \
grep '|' | \
sed 's/\[.*\]//g'| \
sed 's/VBoxManage list//' | \
tr "\\n" " " | \
sed 's/$/\n/' | \
sed 's/\s\+//g' | \
sed 's/|/ /g')
COMPREPLY=( $(compgen -W "$list" -- ${cur}) )
}
# Check the COMP_WORDS looking for name of the vm. If name contain space or
# is enclosed in quotes, glue name together in variable name. Variable index
# will hold the last index of COMP_WORDS array which contain the end of the
# name.
_find_item_name() {
local idx=$1
name=""
while true
do
name="${NAME}${COMP_WORDS[$idx]}"
[[ ${COMP_WORDS[$idx]} = *'"' ]] && break
if [[ ${COMP_WORDS[$idx]} = '"'* || ${COMP_WORDS[$idx]} = *'\ ' ]]
then
idx=$((++idx))
continue
fi
break
done
index=$idx
}
_bandwidthctl_comp() {
local rules cur=$1
_find_item_name 2
rules=$(VBoxManage bandwidthctl "${name//\\/}" \
list --machinereadable | \
awk -F ',' '{print $1}' | \
awk -F '=' '{print $2}' | \
tr '\n' '|' | \
sed 's/|$//' | \
sed 's/\s/\\ /g')
IFS='|' read -ra rules <<< "$rules"
for item in "${rules[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
}
_snapshot_comp() {
local snap cur=$1
_find_item_name 2
snap=$(VBoxManage snapshot "${name//\\/}" \
list | \
awk -F ': ' '{print $2}' | \
sed 's/ (.*//' | \
tr '\n' '|' | \
sed 's/|$//' | \
sed 's/\s/\\ /g')
IFS='|' read -ra snap <<< "$snap"
for item in "${snap[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
}
COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
lastbutone="${COMP_WORDS[COMP_CWORD-2]}"
if [[ COMP_CWORD -ge 2 ]]; then if [[ COMP_CWORD -ge 2 ]]; then
cmd="${COMP_WORDS[1]}" cmd="${COMP_WORDS[1]}"
if [[ $cmd == "-q" ]]; then if [[ $cmd == "-q" ]]; then
@@ -114,225 +193,126 @@ _VBoxManage() {
return 0 return 0
fi fi
case "${prev}" in case "${cmd}" in
showhdinfo) adoptstate)
_hdcomp ${cur} _vms_comp ${cur}
;; ;;
clonevm) bandwidthctl)
_vmscomp ${cur} if [[ ${prev} == ${cmd} ]]; then
_vms_comp ${cur}
else
_find_item_name 2
subcommand=${COMP_WORDS[$((index+1))]}
if [[ " add set remove list " == *" $subcommand "* ]]; then
case "${subcommand}" in
add)
tmp=""
for i in --type --limit; do
[[ " ${COMP_WORDS[@]} " == *" $i "* ]] && continue
tmp="$tmp $i"
done
COMPREPLY=( $(compgen -W "$tmp" -- ${cur}) )
;;
set)
if [[ ${prev} == "set" ]]; then
_bandwidthctl_comp ${cur}
else
[[ " ${COMP_WORDS[@]} " != *" --limit "* ]] && \
COMPREPLY=( $(compgen -W "--limit" -- \
${cur}) )
fi
;;
remove)
if [[ ${prev} == "remove" ]]; then
_bandwidthctl_comp ${cur}
fi
;;
list)
if [[ ${prev} == "list" ]]; then
COMPREPLY=( $(compgen -W "--machinereadable" \
-- ${cur}) )
fi
;;
esac
case "${prev}" in
--type)
COMPREPLY=( $(compgen -W "disk network" -- ${cur}) )
;;
esac
else
[[ ${#COMPREPLY[@]} -eq 0 ]] && \
COMPREPLY=( $(compgen -W "add set remove list" \
-- ${cur}) )
fi
fi
;; ;;
clonehd) clonehd)
case "${cur}" in
-*)
COMPREPLY=( $(compgen -W "--format --variant
--existing" -- ${cur}) )
;;
*)
_hdcomp ${cur}
;;
esac
;; ;;
--format) clonevm)
COMPREPLY=( $(compgen -W "VDI VMDK VHD RAW" -- ${cur}) )
;; ;;
adoptstate|storageattach|startvm|modifyvm|controlvm|showvminfo|\ closemedium)
unregistervm|snapshot)
_vmscomp ${cur}
;; ;;
--type) #startvm --type controlvm)
[[ ${cmd} == "startvm" ]] && \
COMPREPLY=( $(compgen -W "headless vrdp sdl gui" -- ${cur}) )
[[ ${cmd} == "createhd" ]] && \
COMPREPLY=( $(compgen -W "normal writethrough" -- ${cur}) )
;; ;;
--ostype) convertfromraw)
OS=$(VBoxManage list ostypes | egrep "^ID" | sed "s/ID:\s\+//"|sort)
COMPREPLY=( $(compgen -W "$OS" -- ${cur}) )
;; ;;
--variant) createhd)
COMPREPLY=( $(compgen -W "Standard Fixed Split2G Stream ESX" \
-- ${cur}) )
;; ;;
--vrdpauthtype) createvm)
COMPREPLY=( $(compgen -W "null external guest" -- ${cur}) )
;; ;;
--nictype[1-6]) debugvm)
COMPREPLY=( $(compgen -W "Am79C970A Am79C973 82540EM 82543GC
82545EM" -- ${cur}) )
;; ;;
--nic[1-6]) dhcpserver)
COMPREPLY=( $(compgen -W "bridged none null nat hostonly intnet" \
-- ${cur}) )
;; ;;
--boot[1-4]) discardstate)
COMPREPLY=( $(compgen -W "none floppy dvd disk net" -- ${cur}) ) ;;
"export")
;;
extpack)
;;
getextradata)
;;
guestcontrol)
;;
guestproperty)
;;
hostonlyif)
;;
import)
;; ;;
list) list)
t=$(VBoxManage list | \
grep '|' | \
sed 's/\[.*\]//g'| \
sed 's/VBoxManage list//' | \
tr "\\n" " " | \
sed 's/$/\n/' | \
sed 's/\s\+//g' | \
sed 's/|/ /g')
COMPREPLY=( $(compgen -W "$t" -- ${cur}) )
;; ;;
--device) metrics)
COMPREPLY=( $(compgen -W "0" -- ${cur}) )
;; ;;
--port) modifyhd)
COMPREPLY=( $(compgen -W "1 2 3 4" -- ${cur}) )
;; ;;
--type) modifyvm)
COMPREPLY=( $(compgen -W "dvddrive hdd fdd" -- ${cur}) )
;; ;;
--medium) natnetwork)
for item in "none" "emptydrive" "additions"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
_hdcomp ${cur}
;; ;;
--snapshot) registervm)
;; ;;
--*) #any options .. setextradata)
COMPREPLY=()
;; ;;
*) setproperty)
case "${cmd}" in ;;
clonevm) sharedfolder)
case "${prev}" in ;;
--mode) showhdinfo)
COMPREPLY=( $(compgen -W "machine machineandchildren all" -- ${cur}) ) ;;
;; showvminfo)
--options) ;;
COMPREPLY=( $(compgen -W "link keepallmacs snapshot)
keepnatmacs keepdisknames" -- ${cur}) ) ;;
;; startvm)
esac ;;
COMPREPLY=( $(compgen -W "--snapshot --mode --options storageattach)
--name --groups --basefolder --uuid --register" -- ${cur}) ) ;;
;; storagectl)
adoptstate) ;;
COMPREPLY=() unregistervm)
;; ;;
createhd) usbfilter)
COMPREPLY=( $(compgen -W "--filename --size --type
--format --comment --remember --variant" -- ${cur}) )
;;
createvm)
COMPREPLY=( $(compgen -W "--name --ostype --register
--basefolder --settingsfile --uid" -- ${cur}) )
;;
controlvm)
case "${prev}" in
nictracefile[0-9]*)
COMPREPLY=( $(compgen -f -- ${cur}) )
;;
nictrace[0-9]*)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
nicpromisc[0-9]*)
COMPREPLY=( $(compgen -W "deny allow-vms
allow-all" -- ${cur}) )
;;
nic[0-9]*)
COMPREPLY=( $(compgen -W "null nat bridged intnet
hostonly generic natnetwork" -- ${cur}) )
;;
natpf[0-9]*)
COMPREPLY=( $(compgen -W "delete tcp
udp" -- ${cur}) )
;;
clipboard)
COMPREPLY=( $(compgen -W "disabled hosttoguest
guesttohost bidirectional" -- ${cur}) )
;;
draganddrop)
COMPREPLY=( $(compgen -W "disabled
hosttoguest" -- ${cur}) )
;;
vrde)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
vcpenabled)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
vcpscreens)
COMPREPLY=( $(compgen -W "all none" -- ${cur}) )
;;
setcredentials)
COMPREPLY=( $(compgen -W "--passwordfile
--allowlocallogon" -- ${cur}) )
;;
teleport)
COMPREPLY=( $(compgen -W "--host --port --maxdowntime
--passwordfile --password" -- ${cur}) )
;;
webcam)
COMPREPLY=( $(compgen -W "attach detach
list" -- ${cur}) )
;;
acpipowerbutton|acpisleepbutton|cpuexecutioncap|\
guestmemoryballoon|keyboardputscancode|\
nicproperty[0-9]*|pause|plugcpu|poweroff|reset|\
resume|savestate|screenshotpng|setlinkstate1|\
setvideomodehint|unplugcpu|usbattach|usbdetach|\
vrdeport|vrdeproperty|vrdevideochannelquality)
COMPREPLY=()
;;
*)
COMPREPLY=( $(compgen -W "acpipowerbutton
acpisleepbutton clipboard cpuexecutioncap
draganddrop guestmemoryballoon keyboardputscancode
natpf1 nic1 nicpromisc1 nicproperty1 nictrace1
nictracefile1 pause plugcpu poweroff reset resume
savestate screenshotpng setcredentials setlinkstate1
setvideomodehint teleport unplugcpu usbattach
usbdetach vcpenabled vcpscreens vrde vrdeport
vrdeproperty vrdevideochannelquality
webcam" -- ${cur}) )
;;
esac
;;
unregistervm)
COMPREPLY=( $(compgen -W "--delete" -- ${cur}) )
;;
startvm)
COMPREPLY=( $(compgen -W "--type" -- ${cur}) )
;;
snapshot)
COMPREPLY=( $(compgen -W "take delete restore restorecurrent
edit list showvminfo --description --current --name
--details --machinereadable" -- ${cur}) )
;;
modifyvm)
COMPREPLY=( $(compgen -W "--name --memory --ostype --usb
--vram --acpi --ioapic --hwvirtex --accelerate3d --cpus
--sataportcount --sataport1 --sataport2 --sataport3
--sataport4 --nic1 --nictype1 --nic2 --nictype2 --nic3
--nictype3 --nic4 --nictype4 --nic5 --nictype5 --nic6
--nictype6 --bridgeadapter1 --bridgeadapter2
--bridgeadapter3 --bridgeadapter4 --bridgeadapter5
--bridgeadapter6 --vrdp --vrdpport --vrdpauthtype" \
-- ${cur}) )
;;
closemedium)
COMPREPLY=( $(compgen -W "disk dvd floppy" -- ${cur}) )
;;
storageattach)
COMPREPLY=( $(compgen -W "--storagectl --device --port
--medium --type" -- ${cur}) )
;;
showvminfo)
COMPREPLY=( $(compgen -W "--details --machinereadable
--log" -- ${cur}) )
;;
*)
COMPREPLY=()
;;
esac
;; ;;
esac esac
} }