Last active
September 27, 2019 08:42
-
-
Save zessx/2453316a1f0cd220dd0e2c148be54364 to your computer and use it in GitHub Desktop.
Manage Vagrant easily
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
#!/bin/zsh | |
# Let Funky Fox 🦊 manage your Vagrants! | |
# Available commands: | |
# - vag list | |
# - vag NAME up | |
# - vag NAME halt | |
# - vag NAME go | |
# - vag NAME ssh | |
function vag() { | |
workingdir='htdocs'; | |
if [ -z "$1" ] | |
then | |
print -P "🦊 %F{red}Vagrant name required%f" | |
elif [ $1 = "help" ] || [ $1 = "list" ] || [ $1 = "status" ] | |
then | |
print -P "🦊 %F{blue}Vagrants available:%f" | |
vagrantlist=$(vagrant global-status | grep virtualbox | sed "s/.* virtualbox \(.*\) *.*$workingdir\/\(.*\)$/\2 \1/g" | sed "s/\/trellis//g" | sed "s/ */ /g" | sed "s/poweroff/%F{red}poweroff%f/g" | sed "s/running/%F{green}running%f/g") | |
print -P "${vagrantlist}" | |
else | |
vagrantpath=$(vagrant global-status | grep virtualbox | grep -i $1 | sed "s/^.* \//\//") | |
if [ -z "$vagrantpath" ] | |
then | |
print -P "🦊 %F{red}Unknown vagrant%f" | |
else | |
if [ -z "$2" ] | |
then | |
print -P "🦊 %F{red}Action required (go | up | halt | ssh)%f" | |
elif [ $2 = "go" ] | |
then | |
print -P "🦊 %F{green}Move to the [${1}] vagrant folder%f" | |
cd $(echo $vagrantpath) | |
elif [ $2 = "up" ] | |
then | |
alreadyrunning=$(vagrant global-status | grep virtualbox | grep running | sed "s/.* virtualbox .*$workingdir\/\(.*\)$/\1/g" | sed "s/\/trellis//g" | sed "s/ //g") | |
if [ -z "$alreadyrunning" ] | |
then | |
print -P "🦊 %F{green}Enable sudo mode%f" | |
(sudo pwd > /dev/null) | |
print -P "🦊 %F{green}Run the [${1}] vagrant%f" | |
(cd $(echo $vagrantpath) && vagrant up) | |
else | |
print -P "🦊 %F{red}Another vagrant (${alreadyrunning}) is already running%f" | |
fi | |
elif [ $2 = "halt" ] | |
then | |
print -P "🦊 %F{green}Stop the [${1}] vagrant%f" | |
(cd $(echo $vagrantpath) && vagrant halt) | |
elif [ $2 = "ssh" ] | |
then | |
print -P "🦊 %F{green}Log into the [${1}] vagrant%f" | |
(cd $(echo $vagrantpath) && vagrant ssh) | |
fi | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment