Skip to content

Instantly share code, notes, and snippets.

@ccooke
Created September 16, 2014 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccooke/b12ab89e02e34bb7d0c3 to your computer and use it in GitHub Desktop.
Save ccooke/b12ab89e02e34bb7d0c3 to your computer and use it in GitHub Desktop.
_virsh_vm_name()
{
virsh list --all --name "${@}"
}
_virsh_vol_name()
{
virsh vol-list default | sed '1,2d' | awk '{print $1}'
}
_virsh()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
#if [[ ${cur} == -* ]] ; then
# COMPREPLY=( $(compgen -W "--conf -c --name --id -m --version -s --status -w --watch -o --out-file -i --in-file" -- ${cur}) )
# return 0
#fi
case "${prev}" in
list)
COMPREPLY=( $(compgen -W "--all --inactive --state-running --state-paused --state-shutoff --state-other --autostart --no-autostart --with-managed-save --without-managed-save --with-snapshot --without-snapshot" -- ${cur} ) )
return 0
;;
autostart)
COMPREPLY=( $(compgen -W "$(_virsh_vm_name --no-autostart)" -- ${cur}) )
return 0
;;
start)
COMPREPLY=( $(compgen -W "$(_virsh_vm_name --state-shutoff)" -- ${cur} ) )
return 0
;;
shutdown|console|domid|domname|send-key|managedsave|migrate|reboot|reset|save|suspend|vncdisplay)
COMPREPLY=( $(compgen -W "$(_virsh_vm_name --state-running)" -- ${cur} ) )
return 0
;;
edit|destroy|desc|dumpxml|managedsave-remove|restore|resume|setmem|setvcpus|undefine)
COMPREPLY=( $(compgen -W "$(_virsh_vm_name)" -- ${cur} ) )
return 0
;;
vol-delete|vol-info)
COMPREPLY=( $(compgen -W "$(_virsh_vol_name --pool default)" -- ${cur} ) )
return 0
;;
*)
COMPREPLY=( $(compgen -W "autostart start shutdown console domid domname send-key migrate reboot reset save suspend vncdisplay managedsave edit destroy desc dumpxml managedsave-remove restore resume list sysinfo vol-list vol-info" -- ${cur}) )
return 0
;;
esac
}
complete -F _virsh virsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment