-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarge
executable file
·46 lines (39 loc) · 896 Bytes
/
barge
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
45
46
#!/bin/sh
set -e
PWD=$(pwd)
DIR=$(pwd)
while [ "${DIR}" != "/" -a ! -e "${DIR}/Vagrantfile" ]; do
cd ..
DIR=$(pwd)
done
SSH_CONFIG="${DIR}/.ssh_config"
cd "${PWD}"
if [ "$1" == "-t" ]; then
TTY="$1"
shift
fi
HOST=$1
if [ ! -e "${SSH_CONFIG}" ]; then
vagrant ssh-config > "${SSH_CONFIG}" 2>/dev/null || true
fi
if [ ! -s "${SSH_CONFIG}" ]; then
rm -f "${SSH_CONFIG}"
echo "No VMs is running." >&2
exit 1
fi
if test -z "${HOST}" || ! grep -q "^Host ${HOST}$" "${SSH_CONFIG}" 2>/dev/null; then
HOST=$(awk '/^Host/{print $2}' "${SSH_CONFIG}" 2>/dev/null | head -n 1)
else
shift
fi
trap "rm -f '${SSH_CONFIG}'; exit 1" ERR
ssh -F "${SSH_CONFIG}" "${HOST}" exit
trap "" ERR
if [ $# -ne 0 ]; then
cmd=("${@//\"/\\\"}")
cmd=("${cmd[@]/#/\"}")
cmd="${cmd[@]/%/\"}"
ssh ${TTY} -F "${SSH_CONFIG}" "${HOST}" bash -l -c "'${cmd}'"
else
ssh -F "${SSH_CONFIG}" "${HOST}"
fi