Fixed a way the vm list is built; several other imporvements

This commit is contained in:
2015-03-25 22:34:30 +01:00
parent c735d10602
commit 6b4282db0c

View File

@@ -1,166 +1,241 @@
# bash command-line completion for virtualbox # bash command-line completion for virtualbox
# development version, or specially used for my opts # development version, or specially used for my opts
# Author: Sebastian T. Hafner <sonix@own-hero.net> # Author: Sebastian T. Hafner <sonix@own-hero.net>
#
# [ ] adoptstate
# [ ] bandwidthctl
# [ ] clonehd
# [ ] clonevm
# [ ] closemedium
# [ ] controlvm
# [ ] convertfromraw
# [ ] createhd
# [ ] createvm
# [ ] debugvm
# [ ] dhcpserver
# [ ] discardstate
# [ ] export
# [ ] extpack
# [ ] getextradata
# [ ] guestcontrol
# [ ] guestproperty
# [ ] hostonlyif
# [ ] import
# [x] list
# [ ] metrics
# [ ] modifyhd
# [ ] modifyvm
# [ ] natnetwork
# [ ] registervm
# [ ] setextradata
# [ ] setproperty
# [ ] sharedfolder
# [ ] showhdinfo
# [ ] showvminfo
# [ ] snapshot
# [x] startvm
# [ ] storageattach
# [ ] storagectl
# [ ] unregistervm
# [ ] usbfilter
_VBoxManage() { _VBoxManage() {
local cur prev opts vms cmd hdds local cur prev opts vms vms cmd count item
# TODO: This function works quite well with option 'default'. Unfortunately
# there is an issue with filenames which contain space with that setting.
# OTOH I don't know how to force bash (or readline) to display full path in
# possible matches, so there would be a problem with completion of filenames
# if they are in different place on the fs. After an hour or so of trying to
# make it work I just gave up :/
_hdcomp() {
local cur=$1
local hdds
hdds=$(VBoxManage list hdds | \
grep "Location:" | \
sed 's/Location:\s\+//' | \
tr '\n' '|' | \
sed 's/|$//')
IFS='|' read -ra hdds <<< "$hdds"
for item in "${hdds[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item") && echo "added hd $item"
done
}
# Custom function to fill up the COMPREPLY. Works well with spaces in the
# names of the VMs using option 'filename' for the complete function. Will
# not work if you have a pipe in the VM names. Sorry.
_vmscomp() {
local cur=$1
local vms
vms=$(VBoxManage list vms | \
awk -F ' {' '{ print $1 }' | \
tr '\n' '|' | \
sed 's/|$//' | \
sed 's/"//g')
IFS='|' read -ra vms <<< "$vms"
for item in "${vms[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
}
COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks
COMPREPLY=() COMPREPLY=()
vms=`VBoxManage -q list vms | awk -F\" '{ print $2 }'`
hdds=`VBoxManage -q list hdds | grep Location | awk -F\/ '{ print $NF }'`
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
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
cmd="${COMP_WORDS[2]}" cmd="${COMP_WORDS[2]}"
fi fi
fi fi
opts=`VBoxManage -q help | grep VBoxManage\ [a-z] | awk '{ print $2 }' | uniq`
opts="$opts -q --quiet -v --version" # all possible commands for the VBoxManage
opts=$(VBoxManage -q help | \
egrep -o "^\s\s[a-z]+ " | \
grep -v VBoxManage | \
awk '{print $1}'| \
sort | \
uniq)
if [[ ${cur} == "-q" || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == "-q" || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0 return 0
fi fi
case "${prev}" in case "${prev}" in
# adoptstate)
showhdinfo|clonehd) showhdinfo|clonehd)
COMPREPLY=( $(compgen -W "$hdds" \ _hdcomp ${cur}
-- ${cur}) )
;; ;;
--format) --format)
COMPREPLY=( $(compgen -W "VDI VMDK VHD" \ COMPREPLY=( $(compgen -W "VDI VMDK VHD RAW" -- ${cur}) )
-- ${cur}) )
;; ;;
storageattach|startvm|modifyvm|controlvm|showvminfo|unregistervm|vmstatistics) adoptstate|storageattach|startvm|modifyvm|controlvm|showvminfo|\
COMPREPLY=( $(compgen -W "$vms" \ unregistervm|vmstatistics|snapshot)
-- ${cur}) ) _vmscomp ${cur}
;; ;;
--type) #startvm --type --type) #startvm --type
if [[ ${cmd} == "startvm" ]]; then if [[ ${cmd} == "startvm" ]]; then
COMPREPLY=( $(compgen -W "headless vrdp sdl gui" \ COMPREPLY=( $(compgen -W "headless vrdp sdl gui" -- ${cur}) )
-- ${cur}) ) fi
fi if [[ ${cmd} == "createhd" ]]; then
if [[ ${cmd} == "createhd" ]]; then COMPREPLY=( $(compgen -W "normal writethrough" -- ${cur}) )
COMPREPLY=( $(compgen -W "normal writethrough" \ fi
-- ${cur}) )
fi
;; ;;
--ostype) --ostype)
OS=`VBoxManage list ostypes | grep ID | awk '{ print $2 }'` OS=`VBoxManage list ostypes | grep ID | awk '{ print $2 }'`
COMPREPLY=( $(compgen -W "$OS" \ COMPREPLY=( $(compgen -W "$OS" \
-- ${cur}) ) -- ${cur}) )
;; ;;
--variant) --variant)
COMPREPLY=( $(compgen -W "Standard Fixed Split2G Stream ESX" \ COMPREPLY=( $(compgen -W "Standard Fixed Split2G Stream ESX" \
-- ${cur}) ) -- ${cur}) )
;; ;;
--vrdpauthtype) --vrdpauthtype)
COMPREPLY=( $(compgen -W "null external guest" \ COMPREPLY=( $(compgen -W "null external guest" -- ${cur}) )
-- ${cur}) )
;; ;;
--nictype[1-6]) --nictype[1-6])
COMPREPLY=( $(compgen -W "Am79C970A Am79C973 82540EM 82543GC 82545EM" \ COMPREPLY=( $(compgen -W "Am79C970A Am79C973 82540EM 82543GC
-- ${cur}) ) 82545EM" -- ${cur}) )
;; ;;
--nic[1-6]) --nic[1-6])
COMPREPLY=( $(compgen -W "bridged none null nat hostonly intnet" \ COMPREPLY=( $(compgen -W "bridged none null nat hostonly intnet" \
-- ${cur}) ) -- ${cur}) )
;; ;;
--boot[1-4]) --boot[1-4])
COMPREPLY=( $(compgen -W "none floppy dvd disk net" \ COMPREPLY=( $(compgen -W "none floppy dvd disk net" -- ${cur}) )
-- ${cur}) )
;; ;;
--sataport[1-6]) *vrdp)
COMPREPLY=( $(compgen -W "$hdds" \ COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
-- ${cur}) )
;; ;;
*vrdp) list)
COMPREPLY=( $(compgen -W "on off" \ COMPREPLY=( $(compgen -W "vms runningvms ostypes hostdvds
-- ${cur}) ) hostfloppies intnets bridgedifs hostonlyifs natnets dhcpservers
hostinfo hostcpuids hddbackends hdds dvds floppies usbhost
usbfilters systemproperties extpacks groups webcams" -- ${cur}) )
;; ;;
disk) # --storageattach
COMPREPLY=( $(compgen -W "$hdds" \ --storagectl)
-- ${cur}) ) CONTROLLER="SATA IDE"
COMPREPLY=( $(compgen -W "$CONTROLLER" -- ${cur}) )
;; ;;
list) --device)
COMPREPLY=( $(compgen -W "vms runningvms ostypes hostdvds hostfloppies COMPREPLY=( $(compgen -W "0" -- ${cur}) )
bridgedifs hostonlyifs dhcpservers hostinfo ;;
hddbackends hdds dvds floppies --port)
usbhost usbfilters systemproperties" \ COMPREPLY=( $(compgen -W "1 2 3 4" -- ${cur}) )
-- ${cur}) ) ;;
--type)
COMPREPLY=( $(compgen -W "dvddrive hdd fdd" -- ${cur}) )
;;
--medium)
for item in "none" "emptydrive" "additions"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
_hdcomp ${cur}
;;
--*) #any options ..
COMPREPLY=()
;; ;;
# --storageattach
--storagectl)
CONTROLLER="SATA IDE"
COMPREPLY=( $(compgen -W "$CONTROLLER" \
-- ${cur}) )
;;
--device)
COMPREPLY=( $(compgen -W "0" \
-- ${cur}) )
;;
--port)
COMPREPLY=( $(compgen -W "1 2 3 4" \
-- ${cur}) )
;;
--type)
COMPREPLY=( $(compgen -W "dvddrive hdd fdd" \
-- ${cur}) )
;;
--medium)
COMPREPLY=( $(compgen -W "none $hdds" \
-- ${cur}) )
;;
--*) #any options ..
COMPREPLY=()
;;
*) *)
case "${cmd}" in case "${cmd}" in
createhd) adoptstate)
COMPREPLY=( $(compgen -W "--filename --size --type COMPREPLY=("dupa")
--format --comment --remember --variant" \ ;;
-- ${cur}) ) createhd)
;; COMPREPLY=( $(compgen -W "--filename --size --type
createvm) --format --comment --remember --variant" -- ${cur}) )
COMPREPLY=( $(compgen -W "--name --ostype --register --basefolder ;;
--settingsfile --uid" \ createvm)
-- ${cur}) ) COMPREPLY=( $(compgen -W "--name --ostype --register
;; --basefolder --settingsfile --uid" -- ${cur}) )
controlvm) ;;
COMPREPLY=( $(compgen -W "pause resume poweroff reset savestate controlvm)
acpipowerbutton acpisleepbutton COMPREPLY=( $(compgen -W "pause resume poweroff reset
vrdp " \ savestate acpipowerbutton acpisleepbutton vrdp" -- ${cur}) )
-- ${cur}) ) ;;
;; startvm)
startvm) COMPREPLY=( $(compgen -W "--type" -- ${cur}) )
COMPREPLY=( $(compgen -W "--type" -- ${cur}) ) ;;
;; snapshot)
modifyvm) COMPREPLY=( $(compgen -W "take delete restore restorecurrent
COMPREPLY=( $(compgen -W "--name --memory --ostype --usb --vram edit list showvminfo" -- ${cur}) )
--acpi --ioapic --hwvirtex --accelerate3d --cpus ;;
--sataportcount --sataport1 --sataport2 --sataport3 --sataport4 modifyvm)
--nic1 --nictype1 --nic2 --nictype2 --nic3 --nictype3 COMPREPLY=( $(compgen -W "--name --memory --ostype --usb
--nic4 --nictype4 --nic5 --nictype5 --nic6 --nictype6 --vram --acpi --ioapic --hwvirtex --accelerate3d --cpus
--bridgeadapter1 --bridgeadapter2 --bridgeadapter3 --bridgeadapter4 --bridgeadapter5 --bridgeadapter6 --sataportcount --sataport1 --sataport2 --sataport3
--vrdp --vrdpport --vrdpauthtype --sataport4 --nic1 --nictype1 --nic2 --nictype2 --nic3
" \ --nictype3 --nic4 --nictype4 --nic5 --nictype5 --nic6
-- ${cur}) ) --nictype6 --bridgeadapter1 --bridgeadapter2
;; --bridgeadapter3 --bridgeadapter4 --bridgeadapter5
closemedium) --bridgeadapter6 --vrdp --vrdpport --vrdpauthtype" \
COMPREPLY=( $(compgen -W "disk dvd floppy" -- ${cur}) ) -- ${cur}) )
;; ;;
storageattach) closemedium)
COMPREPLY=( $(compgen -W "--storagectl --device --port --medium --type" -- ${cur}) ) COMPREPLY=( $(compgen -W "disk dvd floppy" -- ${cur}) )
;; ;;
*) storageattach)
COMPREPLY=() COMPREPLY=( $(compgen -W "--storagectl --device --port
;; --medium --type" -- ${cur}) )
esac ;;
*)
COMPREPLY=()
;;
esac
;; ;;
esac esac
} }
complete -o filenames -F _VBoxManage VBoxManage complete -o filenames -F _VBoxManage VBoxManage
#complete -o default -F _VBoxManage VBoxManage
# vim: set ft=sh tw=80 sw=4 et : # vim: set ft=sh tw=80 sw=4 et :