Skip to content

Commit

Permalink
vagrant autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 9, 2013
1 parent 1f09f57 commit 16c027b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 37 deletions.
1 change: 1 addition & 0 deletions Vendorfile
Expand Up @@ -4,6 +4,7 @@ file "bash/cap-thor-bash-autocomplete.sh", "https://raw.github.com/sgruhier/rake
end

file "bash/rake-autocomplete.rb", "https://raw.github.com/gist/3395598/rake_autocomplete.rb"
file "bash/vagrant-autocomplete.sh", "https://raw.github.com/kura/vagrant-bash-completion/master/vagrant"
file "bin/sshb", "https://raw.github.com/kpumuk/dotfiles/master/bash/bin/sshb" do |file|
rewrite(file) do |content|
content.
Expand Down
34 changes: 0 additions & 34 deletions bash/tddium_autocomplete.rb

This file was deleted.

97 changes: 97 additions & 0 deletions bash/vagrant-autocomplete.rb
@@ -0,0 +1,97 @@
#!/bin/bash
__pwdln() {

# Doing PE from the beginning of the string is needed
# so we get a string of 0 len to break the until loop.

pwdmod="${PWD}/"
itr=0
until [[ -z "$pwdmod" ]];do
itr=$(($itr+1))
pwdmod="${pwdmod#*/}"
done
echo -n $(($itr-1))

}

__vagrantinvestigate() {

if [[ -f "${PWD}/.vagrant" ]];then
echo "${PWD}/.vagrant"
return 0
else
pwdmod2="${PWD}" # Since we didn't find a $PWD/.vagrant, we're going to pop
for (( i=2; i<=$(__pwdln); i++ ));do # a directory off the end of the $pwdmod2 stack until we
pwdmod2="${pwdmod2%/*}" # come across a ./.vagrant. /home/igneous/proj/1 will start at
if [[ -f "${pwdmod2}/.vagrant" ]];then # /home/igneous/proj because of our loop starting at 2.
echo "${pwdmod2}/.vagrant"
return 0
fi
done
fi

return 1

}

_vagrant()
{
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="box destroy halt help init package provision reload resume ssh ssh_config status suspend up version"

if [ $COMP_CWORD == 1 ]
then
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
return 0
fi

if [ $COMP_CWORD == 2 ]
then
case "$prev" in
"init")
local box_list=$(find $HOME/.vagrant.d/boxes -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
return 0
;;
"ssh")
vagrant_state_file=$(__vagrantinvestigate) || return 1
#Got lazy here.. I'd like to eventually replace this with a pure bash solution.
running_vm_list=$(grep 'active' $vagrant_state_file | sed -e 's/"active"://' | tr ',' '\n' | cut -d '"' -f 2 | tr '\n' ' ')
COMPREPLY=($(compgen -W "${running_vm_list}" -- ${cur}))
return 0
;;
"box")
box_commands="add help list remove repackage"
COMPREPLY=($(compgen -W "${box_commands}" -- ${cur}))
return 0
;;
"help")
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
return 0
;;
*)
;;
esac
fi

if [ $COMP_CWORD == 3 ]
then
action="${COMP_WORDS[COMP_CWORD-2]}"
if [ $action == 'box' ]
then
case "$prev" in
"remove"|"repackage")
local box_list=$(find $HOME/.vagrant.d/boxes -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
return 0
;;
*)
;;
esac
fi
fi

}
complete -F _vagrant vagrant

97 changes: 97 additions & 0 deletions bash/vagrant-autocomplete.sh
@@ -0,0 +1,97 @@
#!/bin/bash
__pwdln() {

# Doing PE from the beginning of the string is needed
# so we get a string of 0 len to break the until loop.

pwdmod="${PWD}/"
itr=0
until [[ -z "$pwdmod" ]];do
itr=$(($itr+1))
pwdmod="${pwdmod#*/}"
done
echo -n $(($itr-1))

}

__vagrantinvestigate() {

if [[ -f "${PWD}/.vagrant" ]];then
echo "${PWD}/.vagrant"
return 0
else
pwdmod2="${PWD}" # Since we didn't find a $PWD/.vagrant, we're going to pop
for (( i=2; i<=$(__pwdln); i++ ));do # a directory off the end of the $pwdmod2 stack until we
pwdmod2="${pwdmod2%/*}" # come across a ./.vagrant. /home/igneous/proj/1 will start at
if [[ -f "${pwdmod2}/.vagrant" ]];then # /home/igneous/proj because of our loop starting at 2.
echo "${pwdmod2}/.vagrant"
return 0
fi
done
fi

return 1

}

_vagrant()
{
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="box destroy halt help init package provision reload resume ssh ssh_config status suspend up version"

if [ $COMP_CWORD == 1 ]
then
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
return 0
fi

if [ $COMP_CWORD == 2 ]
then
case "$prev" in
"init")
local box_list=$(find $HOME/.vagrant.d/boxes -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
return 0
;;
"ssh")
vagrant_state_file=$(__vagrantinvestigate) || return 1
#Got lazy here.. I'd like to eventually replace this with a pure bash solution.
running_vm_list=$(grep 'active' $vagrant_state_file | sed -e 's/"active"://' | tr ',' '\n' | cut -d '"' -f 2 | tr '\n' ' ')
COMPREPLY=($(compgen -W "${running_vm_list}" -- ${cur}))
return 0
;;
"box")
box_commands="add help list remove repackage"
COMPREPLY=($(compgen -W "${box_commands}" -- ${cur}))
return 0
;;
"help")
COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
return 0
;;
*)
;;
esac
fi

if [ $COMP_CWORD == 3 ]
then
action="${COMP_WORDS[COMP_CWORD-2]}"
if [ $action == 'box' ]
then
case "$prev" in
"remove"|"repackage")
local box_list=$(find $HOME/.vagrant.d/boxes -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
COMPREPLY=($(compgen -W "${box_list}" -- ${cur}))
return 0
;;
*)
;;
esac
fi
fi

}
complete -F _vagrant vagrant

7 changes: 4 additions & 3 deletions dotbashrc
@@ -1,11 +1,12 @@
source ~/dotfiles/bash/env.sh
source ~/dotfiles/bash/alias.sh
source ~/dotfiles/bash/git_prompt.sh
source ~/dotfiles/bash/git-completion.bash
source ~/dotfiles/bash/cap_thor_bash_autocomplete.sh
source ~/dotfiles/bash/git-autocomplete.bash
source ~/dotfiles/bash/cap-thor-bash-autocomplete.sh
source ~/dotfiles/bash/vagrant-autocomplete.sh
source ~/dotfiles/bash/bundler-exec.sh
source ~/dotfiles/bash/rvm.sh
source ~/dotfiles/bash/personal.sh

complete -C ~/dotfiles/bash/rake_autocomplete.rb -o default rake
complete -C ~/dotfiles/bash/rake-autocomplete.rb -o default rake
complete -C ~/dotfiles/bash/tddium_autocomplete.rb -o default tddium

0 comments on commit 16c027b

Please sign in to comment.