Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 65 lines (50 sloc) 1.6 KB
#!/usr/bin/env bash
# Unoffical Bash "strict mode"
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
#ORIGINAL_IFS=$IFS
IFS=$'\t\n' # Stricter IFS settings
usage() {
cat <<EOF
$(basename $0) [command or script] [args]
Provides an easy way of running commands on a Vagrant box.
EOF
}
upfind() {
local result
while [[ $PWD != / ]] ; do
result=$(find "$PWD" -maxdepth 1 "$@")
if [[ -n $result ]]; then
echo "$result"
break
else
cd ..
fi
done
}
command_or_script="${1:-}"
remaining_args=${*:2}
if [[ -z "$command_or_script" ]]
then
echo "ERROR: no command or script specified"
usage
exit 1
fi
host_dir=$(dirname $(upfind -name '.vagrant'))
current_dir=$(pwd)
provider=$(vagrant status --machine-readable | grep "provider-name" | cut -f 4 -d,)
# First line is guest dir, second line is host dir
synced_dirs_info=$(cat $host_dir/.vagrant/machines/default/*/synced_folders | jq -r ".$provider[] | .[]")
guest_dir=$(echo "$synced_dirs_info" | sed -n 1p)
#host_dir=$(echo "$synced_dirs_info" | sed -n 2p)
#echo "$host_dir"
#echo "$current_dir"
# TODO: Need to compare the project root with the current directory and then
# `cd` to the correct directory on the vagrant box before running the command
if [[ -x "$command_or_script" ]]; then
vagrant_command="vagrant ssh -- 'cd $current_dir' < '$command_or_script $remaining_args'"
else
vagrant_command="vagrant ssh -- 'cd $current_dir' '$command_or_script $remaining_args'"
fi
set -x # Show debug info when running the erl command
eval $vagrant_command