Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemorton/8864410 to your computer and use it in GitHub Desktop.
Save lukemorton/8864410 to your computer and use it in GitHub Desktop.

Vagrant SSH Helper

This is specifically for https://github.com/madebymade/vagrant-dev

Usage

Inside your vagrant-dev directory you might run your tests normally like this:

vagrant ssh -c "cd /var/www/my-project && rake"

But if you are in vagrant-dev/www/my-project, v gives you:

v rake

Install

To install this script copy v into your vagrant-dev repo in the root directory.

You will want to symlink this into /usr/local/bin so it's contained in your path:

ln -s /full/path/to/vagrant-dev/v /usr/local/bin/v

And that's it.

How it works

The script checks what directory you are in and ensures you are inside a directory in vagrant-dev/www/. If you are in one of these directories whatever command you run it assumes you want to run it inside vagrant in that directory.

Why

Because I didn't want to keep switching into vagrant with vagrant ssh and I don't like running long commands like vagrant ssh -c "cd /var/www/my-project && rake db:migrate".

#!/bin/bash
vrake=`readlink $0`
expected_dir="${vrake%/*}/www/"
expected_dir_length=${#expected_dir}
dirname=${PWD##*/}
if [[ "${PWD:0:$expected_dir_length}" != "$expected_dir" ]]; then
echo "You need to be inside a directory in $expected_dir"
echo "You are in $PWD"
exit 1
fi
vagrant ssh -c "cd /var/www/$dirname && $*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment