diff --git a/src/_vagrant b/src/_vagrant index e81688d..9903903 100644 --- a/src/_vagrant +++ b/src/_vagrant @@ -61,24 +61,71 @@ __task_list () local expl local -a tasks - tasks=(box destroy global-status halt init package plugin provision reload resume ssh ssh_config status suspend up version) + tasks=(box destroy global-status halt init package plugin provision \ + reload resume ssh ssh_config status suspend up version) _wanted tasks expl 'help' compadd $tasks } __box_list () { - _wanted application expl 'command' compadd $(command ls -1 ${VAGRANT_HOME:-$HOME/.vagrant.d}/boxes 2>/dev/null| sed -e 's/ /\\ /g') + _wanted application expl 'command' compadd $(command vagrant box list | \ + awk '{print $1}' | \ + sed -e 's/ /\\ /g') } __plugin_list () { - _wanted application expl 'command' compadd $(command vagrant plugin list 2>/dev/null| cut -d' ' -f1) + _wanted application expl 'command' compadd $(command vagrant plugin list \ + 2>/dev/null| cut -d' ' -f1) +} + +_vagrant_caching_policy() +{ + local reg_time comp_time check_file + + case "${1##*/}" in + VAGRANT_VMS) + if [[ -z $VAGRANT_CWD ]]; then + check_file=./Vagrantfile + else + check_file=$VAGRANT_CWD/Vagrantfile + fi + ;; + *) + echo "unknown type: $1" + return -1 + ;; + esac + + case $(uname -s) in + Darwin) STATCMD="stat -f '%c'" ;; + *) STATCMD="stat -c '%Z'" ;; + esac + reg_time=${$(${(z)STATCMD} $check_file):Q} + comp_time=${$(${(z)STATCMD} $1):Q} + + return $(( reg_time < comp_time )) } __vm_list () { - _wanted application expl 'command' compadd $(command grep Vagrantfile -oe '^[^#]*\.vm\.define * ['\''":]\?\([a-zA-Z0-9\-_]\+\)['\''"]\?' 2>/dev/null | awk '{print $NF}' | sed 's/'\''//g'|sed 's/\"//g'|sed 's/^://' ) + local cache_policy + zstyle -s ":completion:${curcontext}:" cache-policy cache_policy + zstyle ":completion:${curcontext}:" cache-policy \ + ${cache_policy:-_vagrant_caching_policy} + + # Cache the list of VMs available + if ( [[ ${+_vagrant_vms} -eq 0 ]] || _cache_invalid VAGRANT_VMS ) && + ! _retrieve_cache VAGRANT_VMS; + then + _vagrant_vms=( $(_call_program path-all "vagrant status | \ + awk '{print \$1}' | \ + egrep -v '^$|^(Current|This|above|VM,)$'" ) ) + _store_cache VAGRANT_VMS _vagrant_vms + fi + + _wanted application expl 'command' compadd $_vagrant_vms } __vagrant-box () @@ -163,8 +210,8 @@ case $state in (plugin) __vagrant-plugin ;; - (up|provision|package|destroy|reload|ssh|halt|resume|status) - _arguments ':feature:__vm_list' + (up|provision|package|destroy|reload|ssh|halt|resume|status|suspend) + _arguments ':feature:__vm_list' esac ;; esac