Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple host:port expressions #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your
## Usage

```
./wait-for host:port [-t timeout] [-- command args]
./wait-for host:port [host2:port2 ... [hostN:portN]] [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
Expand Down
37 changes: 26 additions & 11 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
TIMEOUT=15
QUIET=0

CONNS=
HOST=
PORT=

echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}
Expand All @@ -11,7 +15,7 @@ usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
$cmdname host:port [host2:port2 ... [hostN:portN]] [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
Expand All @@ -22,26 +26,23 @@ USAGE
wait_for() {
for i in `seq $TIMEOUT` ; do
nc -z "$HOST" "$PORT" > /dev/null 2>&1

result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
return 0
fi
sleep 1
done
echo "Operation timed out" >&2
echo "Operation timed out waiting for $HOST:$PORT" >&2
exit 1
}


while [ $# -gt 0 ]
do
case "$1" in
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
CONNS="$CONNS $1"
shift 1
;;
-q | --quiet)
Expand Down Expand Up @@ -71,9 +72,23 @@ do
esac
done

if [ "$HOST" = "" -o "$PORT" = "" ]; then
if [ "$CONNS" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi

wait_for "$@"
result=

for conn in $CONNS
do
HOST="$(printf "%s\n" "$conn"| cut -d : -f 1)"
PORT="$(printf "%s\n" "$conn"| cut -d : -f 2)"

wait_for "$@"
result=$?
done

if [ $result -eq 0 -a $# -gt 0 ]
then
exec "$@"
fi