Created
December 9, 2014 18:11
-
-
Save titosemi/013f13afa97785fb9f90 to your computer and use it in GitHub Desktop.
Vagrant plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Vm related tasks | |
################ | |
# Dependencies # | |
################ | |
function vagrant --description 'Extends vagrant command' | |
# Extends function waiting pull request https://github.com/bpinto/oh-my-fish/pull/279 | |
_extends $argv | |
end | |
function _vagrant_config | |
# Temporary. Still need to decide where to put it | |
set -g configs "$HOME/.config/fish/plugins/vagrant.cnf" | |
set -g BOXES 0 | |
set MOUNTS 1 | |
for file in (ls $configs) | |
# Count of Vagrant Box configurations | |
set -g BOXES (math "$BOXES+1") | |
# Host of the box | |
set -g BOX_HOST[$BOXES] (grep 'host' $configs/$file | cut -d'=' -f2) | |
# Name of the box | |
set -g BOX_NAME[$BOXES] (grep 'name' $configs/$file | cut -d'=' -f2) | |
# Path of the box | |
set -g BOX_PATH[$BOXES] (grep 'path' $configs/$file | cut -d'=' -f2) | |
for mount in (grep "mount" $configs/$file | cut -d'=' -f2) | |
# Number of mounts for this box | |
set -g BOX_MOUNTS[$BOXES] (math "$MOUNTS+1") | |
# All the host mounts of this box. Strip multi spaced | |
set HOSTS $HOSTS (echo "$mount" | cut -d':' -f2 | tr "\n" " " | sed 's/^ *//;s/ *$//;s/ \{1,\}/ /g') | |
# All the guest mounts of this box. Strip multi spaced | |
set GUESTS $GUESTS (echo "$mount" | cut -d':' -f1 | tr "\n" " " | sed 's/^ *//;s/ *$//;s/ \{1,\}/ /g') | |
end | |
# [String] Host Mounts as string | |
set -g BOX_HOSTS[$BOXES] "$HOSTS" | |
# [String] Guest Mounts as string | |
set -g BOX_GUESTS[$BOXES] "$GUESTS" | |
# Reset counter | |
set MOUNTS 1 | |
end | |
set -g VAGRANT_BOX "/Users/josemi/Development/Westwing/vagrant" | |
end | |
function _vagrant_arguments | |
if test (count $argv) -lt 2 | |
set_color red | |
echo "Please enter a box name" | |
exit 1 | |
end | |
end | |
function _vagrant_box_not_found | |
set_color red | |
echo "There is no box configured with that name" | |
exit 1 | |
end | |
function _vagrant_mount | |
set HOST $argv[1] | |
set GUEST $argv[2] | |
set OPERATION $argv[3] | |
set NAME | |
if test (count $argv) -eq 4 | |
set NAME $argv[4] | |
else | |
set NAME | |
end | |
switch $argv[3] | |
case 'mount' | |
if not mount | grep "$HOST" 1>/dev/null 2>/dev/null | |
set_color yellow | |
echo "Mounting vagrant $GUEST on $HOST" | |
command sshfs -o reconnect,workaround=truncate:rename,cache=no,noappledouble,volname=vagrant vagrant:$GUEST $HOST | |
set_color green | |
ls "$HOST" | |
else | |
set_color yellow | |
echo "vagrant $GUEST already mounted on $HOST" | |
end | |
case 'umount' | |
if mount | grep "$HOST" 1>/dev/null 2>/dev/null | |
set_color green | |
echo "Un-Mounting $HOST" | |
umount "$HOST" | |
else | |
set_color yellow | |
echo "$HOST is not mounted" | |
end | |
end | |
set_color normal | |
end | |
function _vagrant-mount --description 'Mount vagrant shared folders' | |
_vagrant_config | |
_vagrant_arguments $argv | |
set i 0 | |
while test $i -lt $BOXES | |
set i (math "$i+1") | |
set j 1 | |
if test $BOX_NAME[$i] = $argv[2] | |
cd "$BOX_PATH[$i]" | |
if not vagrant status | grep running 1>/dev/null 2>/dev/null | |
set_color yellow | |
echo "Vagrant is not running" | |
set_color normal | |
# https://github.com/fish-shell/fish-shell/issues/1844 | |
#if read_input "Do you want to boot it" | |
#echo "i would have started " | |
vagrant up | |
#else | |
# return 0 | |
#end | |
end | |
while test $j -le $BOX_MOUNTS[$i] | |
for mount in (echo $BOX_HOSTS[$i] | tr ' ' '\n') | |
set HOSTS $HOSTS $mount | |
end | |
for mount in (echo $BOX_GUESTS[$i] | tr ' ' '\n') | |
set GUESTS $GUESTS $mount | |
end | |
_vagrant_mount "$HOSTS[$j]" "$GUESTS[$j]" "mount" | |
set j (math "$j+1") | |
end | |
cd - | |
exit 0 | |
end | |
end | |
_vagrant_box_not_found | |
end | |
function _vagrant-umount --description 'Umount vagrant shared folders' | |
_vagrant_config | |
_vagrant_arguments $argv | |
set i 0 | |
while test $i -lt $BOXES | |
set i (math "$i+1") | |
set j 1 | |
if test $BOX_NAME[$i] = $argv[2] | |
while test $j -le $BOX_MOUNTS[$i] | |
for mount in (echo $BOX_HOSTS[$i] | tr ' ' '\n') | |
set HOSTS $HOSTS $mount | |
end | |
for mount in (echo $BOX_GUESTS[$i] | tr ' ' '\n') | |
set GUESTS $GUESTS $mount | |
end | |
_vagrant_mount "$HOSTS[$j]" "$GUESTS[$j]" "umount" | |
set j (math "$j+1") | |
end | |
exit 0 | |
end | |
end | |
_vagrant_box_not_found | |
end | |
function _vagrant-cd --description 'Change directory to vagrant folder' | |
_vagrant_config | |
_vagrant_arguments $argv | |
set i 0 | |
while test $i -lt $BOXES | |
set i (math "$i+1") | |
if test $BOX_NAME[$i] = $argv[2] | |
cd $BOX_PATH[$i] | |
return 0 | |
end | |
end | |
_vagrant_box_not_found | |
end | |
function _vagrant-list --description 'List configured vagrants' | |
_vagrant_config | |
set i 0 | |
while test $i -lt $BOXES | |
set i (math "$i+1") | |
set_color yellow | |
echo $BOX_NAME[$i] | |
end | |
end | |
# -------------------------------- | |
# Example Config file | |
# -------------------------------- | |
# $HOME was not working still need to figure out why | |
host=vagrant | |
name=vagrant | |
path=/Users/josemi/Development/Westwing/vagrant/ | |
mount=/:/Users/josemi/Development/mnt/vagrant | |
mount=/shop/www/htdocs:/Users/josemi/Sites/vagrant |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment