-
Notifications
You must be signed in to change notification settings - Fork 1
/
vagrant-exec
executable file
·44 lines (35 loc) · 1.16 KB
/
vagrant-exec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
Executes the provided arguments inside the current vagrant VM,
if present. Otherwise just executes the arguments. The entire
command string MUST be quoted. All paths used must be relative
to the root project folder shared between the host and VM.
Usage:
bin/${0##*/} "Command [--with args]"
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi
VAGRANT_EXEC=$( which vagrant )
VAGRANT_PRESENT=$?
if [ $VAGRANT_PRESENT -eq 0 ] && ! mount | grep -q '^/vagrant'; then
echo '## Running command in vagrant.'
vagrant ssh -c "cd /vagrant; $@"
else
echo '## Running command natively.'
eval "$@"
fi
# It is important that the last command is either the `vagrant ssh`
# or the `eval ...` so that the exit code of **this** script
# is the exit code from those calls.
# Logic matrix:
# vagrant installed + outside vm (no /vagrant mount point) = run in vagrant
# vagrant installed + inside vm = run native
# no vagrant + outside vm = run native
# no vagrant + inside vm = run native