2014-03-10 15:18:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
# (The MIT License)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2014 Kura
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the 'Software'), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2014-05-12 11:20:32 +00:00
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
__pwdln() {
|
|
|
|
pwdmod="${PWD}/"
|
|
|
|
itr=0
|
|
|
|
until [[ -z "$pwdmod" ]];do
|
|
|
|
itr=$(($itr+1))
|
|
|
|
pwdmod="${pwdmod#*/}"
|
|
|
|
done
|
|
|
|
echo -n $(($itr-1))
|
|
|
|
}
|
|
|
|
|
|
|
|
__vagrantinvestigate() {
|
|
|
|
if [ -f "${PWD}/.vagrant" -o -d "${PWD}/.vagrant" ];then
|
|
|
|
echo "${PWD}/.vagrant"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
pwdmod2="${PWD}"
|
|
|
|
for (( i=2; i<=$(__pwdln); i++ ));do
|
|
|
|
pwdmod2="${pwdmod2%/*}"
|
|
|
|
if [ -f "${pwdmod2}/.vagrant" -o -d "${pwdmod2}/.vagrant" ];then
|
|
|
|
echo "${pwdmod2}/.vagrant"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
2013-05-24 09:45:44 +00:00
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
_vagrant() {
|
2013-05-24 09:45:44 +00:00
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
2018-10-12 16:49:49 +00:00
|
|
|
commands="box cloud connect destroy docker-exec docker-logs docker-run global-status halt help init list-commands login package plugin provision push rdp reload resume rsync rsync-auto share snapshot ssh ssh-config status suspend up version"
|
2013-05-24 09:45:44 +00:00
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
if [ $COMP_CWORD == 1 ]
|
|
|
|
then
|
|
|
|
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
|
2013-05-24 09:45:44 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
if [ $COMP_CWORD == 2 ]
|
|
|
|
then
|
|
|
|
case "$prev" in
|
|
|
|
"init")
|
2018-07-04 11:55:05 +00:00
|
|
|
local box_list=$(find "${VAGRANT_HOME:-${HOME}/.vagrant.d}/boxes" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sed -e 's/-VAGRANTSLASH-/\//')
|
2014-05-02 16:50:51 +00:00
|
|
|
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2014-05-12 11:20:32 +00:00
|
|
|
"up")
|
2015-03-25 14:51:23 +00:00
|
|
|
vagrant_state_file=$(__vagrantinvestigate) || return 1
|
2017-03-06 22:47:26 +00:00
|
|
|
if [[ -d "${vagrant_state_file}" ]]
|
2015-03-25 14:51:23 +00:00
|
|
|
then
|
2017-03-06 22:47:26 +00:00
|
|
|
local vm_list=$(find "${vagrant_state_file}/machines" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
|
2015-03-25 14:51:23 +00:00
|
|
|
fi
|
2014-05-12 11:20:32 +00:00
|
|
|
local up_commands="--no-provision"
|
2015-03-25 14:51:23 +00:00
|
|
|
COMPREPLY=($(compgen -W "${up_commands} ${vm_list}" -- ${cur}))
|
2014-05-12 11:20:32 +00:00
|
|
|
return 0
|
|
|
|
;;
|
2018-09-03 08:00:47 +00:00
|
|
|
"destroy"|"ssh"|"provision"|"reload"|"halt"|"suspend"|"resume"|"ssh-config")
|
2014-05-02 16:50:51 +00:00
|
|
|
vagrant_state_file=$(__vagrantinvestigate) || return 1
|
2017-03-06 22:47:26 +00:00
|
|
|
if [[ -f "${vagrant_state_file}" ]]
|
2014-05-12 11:20:32 +00:00
|
|
|
then
|
2017-03-06 22:47:26 +00:00
|
|
|
running_vm_list=$(grep 'active' "${vagrant_state_file}" | sed -e 's/"active"://' | tr ',' '\n' | cut -d '"' -f 2 | tr '\n' ' ')
|
|
|
|
else
|
|
|
|
running_vm_list=$(find "${vagrant_state_file}" -type f -name "id" | awk -F"/" '{print $(NF-2)}')
|
|
|
|
fi
|
2014-05-02 16:50:51 +00:00
|
|
|
COMPREPLY=($(compgen -W "${running_vm_list}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
"box")
|
2017-07-20 21:08:58 +00:00
|
|
|
box_commands="add help list outdated prune remove repackage update"
|
2014-05-02 16:50:51 +00:00
|
|
|
COMPREPLY=($(compgen -W "${box_commands}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2018-10-12 16:49:49 +00:00
|
|
|
"cloud")
|
|
|
|
cloud_commands="auth box search provider publish version"
|
|
|
|
COMPREPLY=($(compgen -W "${cloud_commands}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2014-05-02 16:50:51 +00:00
|
|
|
"plugin")
|
|
|
|
plugin_commands="install license list uninstall update"
|
|
|
|
COMPREPLY=($(compgen -W "${plugin_commands}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
"help")
|
|
|
|
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
|
2016-04-10 03:44:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
"snapshot")
|
|
|
|
snapshot_commands="delete list pop push restore save"
|
|
|
|
COMPREPLY=($(compgen -W "${snapshot_commands}" -- ${cur}))
|
2014-05-02 16:50:51 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
2013-05-24 09:45:44 +00:00
|
|
|
fi
|
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
if [ $COMP_CWORD == 3 ]
|
|
|
|
then
|
|
|
|
action="${COMP_WORDS[COMP_CWORD-2]}"
|
2015-03-25 14:51:23 +00:00
|
|
|
case "$action" in
|
|
|
|
"up")
|
|
|
|
if [ "$prev" == "--no-provision" ]
|
|
|
|
then
|
2017-03-06 22:47:26 +00:00
|
|
|
if [[ -d "${vagrant_state_file}" ]]
|
2015-03-25 14:51:23 +00:00
|
|
|
then
|
2017-03-06 22:47:26 +00:00
|
|
|
local vm_list=$(find "${vagrant_state_file}/machines" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
|
2015-03-25 14:51:23 +00:00
|
|
|
fi
|
|
|
|
COMPREPLY=($(compgen -W "${vm_list}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
"box")
|
|
|
|
case "$prev" in
|
2014-05-02 16:50:51 +00:00
|
|
|
"remove"|"repackage")
|
2018-07-04 11:55:05 +00:00
|
|
|
local box_list=$(find "${VAGRANT_HOME:-${HOME}/.vagrant.d}/boxes" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sed -e 's/-VAGRANTSLASH-/\//')
|
2014-05-02 16:50:51 +00:00
|
|
|
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
2015-03-25 14:51:23 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
2017-10-13 10:59:01 +00:00
|
|
|
"snapshot")
|
|
|
|
case "$prev" in
|
|
|
|
"restore"|"delete")
|
|
|
|
local snapshot_list=$(vagrant snapshot list)
|
|
|
|
COMPREPLY=($(compgen -W "${snapshot_list}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
2015-03-25 14:51:23 +00:00
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
2013-05-24 09:45:44 +00:00
|
|
|
fi
|
|
|
|
|
2014-05-02 16:50:51 +00:00
|
|
|
}
|
2013-05-24 09:45:44 +00:00
|
|
|
complete -F _vagrant vagrant
|