Skip to content

Commit

Permalink
add more verbosity if not in quiet mode, enforce it if we are
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatz-drizly committed Nov 30, 2018
1 parent 9102543 commit 96511d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 13 additions & 3 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}

conditionally_output() {
if [ "$QUIET" -ne 1 ]; then
"$@"
else
"$@" > /dev/null 2>&1
fi
}

usage() {
exitcode="$1"
cat << USAGE >&2
Expand All @@ -33,12 +41,14 @@ USAGE
}

test_connection() {
conditionally_output echo "Testing connection to $1:$2..."

# force a 1-second timeout on darwin (https://stackoverflow.com/a/20460402/2063546)
# POSIX-compliant string inclusion test https://stackoverflow.com/a/8811800/2063546
if [ "${OSTYPE#*darwin*}" != "$OSTYPE" ] ; then
nc -z -w 1 -G 1 "$1" "$2"
conditionally_output nc -z -w 1 -G 1 "$1" "$2"
else
nc -z -w 1 "$1" "$2" > /dev/null 2>&1
conditionally_output nc -z -w 1 "$1" "$2"
fi
}

Expand All @@ -51,7 +61,7 @@ wait_for() {
if [ $result -eq 0 ] ; then break ; fi
sleep 1
done
[ $result -ne 0 ] && echo "Operation timed out" >&2
[ $result -ne 0 ] && echoerr "Operation timed out"
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
fi
Expand Down
9 changes: 4 additions & 5 deletions wait-for.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bats

@test "google should be immediately found" {
run ./wait-for google.com:80 -- echo 'success'
@test "google should be immediately found, no output other than our own" {
run ./wait-for -q google.com:80 -- echo 'success'

[ "$output" = "success" ]
}
Expand All @@ -14,12 +14,11 @@
}

@test "nonexistent server should start command if loose option is specified" {
run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1
run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1

[ "$status" -eq 0 ]

[ "${lines[0]}" = "Operation timed out" ]
[ "${lines[1]}" = "passable" ]
[ "$output" = "passable" ]
}

@test "preserve existing environment variables" {
Expand Down

0 comments on commit 96511d6

Please sign in to comment.