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

Add strict option #8

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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your
```
./wait-for host:port [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-s | --strict Only execute subcommand if the test succeeds
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
```
Expand All @@ -26,6 +27,15 @@ Connection to www.eficode.com port 80 [tcp/http] succeeded!
Eficode site is up
```

The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail:

```
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up"
Operation timed out
google is up
```

To wait for database container to become available:


Expand Down
13 changes: 13 additions & 0 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ usage() {
Usage:
$cmdname host:port [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-s | --strict Only execute subcommand if the test succeeds
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
Expand All @@ -33,6 +34,12 @@ wait_for() {
sleep 1
done
echo "Operation timed out" >&2
if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
fi
exit 1
}

Expand All @@ -48,6 +55,10 @@ do
QUIET=1
shift 1
;;
-s | --strict)
STRICT=1
shift 1
;;
-t)
TIMEOUT="$2"
if [ "$TIMEOUT" = "" ]; then break; fi
Expand Down Expand Up @@ -76,4 +87,6 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then
usage 2
fi

STRICT=${STRICT:-0}

wait_for "$@"