diff --git a/wait-for b/wait-for index e28e54c..a8367c7 100755 --- a/wait-for +++ b/wait-for @@ -25,7 +25,7 @@ wait_for() { exit 1 fi - for i in `seq $TIMEOUT` ; do + while :; do nc -z "$HOST" "$PORT" > /dev/null 2>&1 result=$? @@ -35,6 +35,12 @@ wait_for() { fi exit 0 fi + + if [ "$TIMEOUT" -le 0 ]; then + break + fi + TIMEOUT=$((TIMEOUT - 1)) + sleep 1 done echo "Operation timed out" >&2 @@ -53,11 +59,25 @@ do QUIET=1 shift 1 ;; - -t) + -q-*) + QUIET=0 + echoerr "Unknown option: $1" + usage 1 + ;; + -q*) + QUIET=1 + result=$1 + shift 1 + set -- -"${result#-q}" "$@" + ;; + -t | --timeout) TIMEOUT="$2" - if [ "$TIMEOUT" = "" ]; then break; fi shift 2 ;; + -t*) + TIMEOUT="${1#-t}" + shift 1 + ;; --timeout=*) TIMEOUT="${1#*=}" shift 1 @@ -69,13 +89,24 @@ do --help) usage 0 ;; + -*) + QUIET=0 + echoerr "Unknown option: $1" + usage 1 + ;; *) + QUIET=0 echoerr "Unknown argument: $1" usage 1 ;; esac done +if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then + echoerr "Error: invalid timeout '$TIMEOUT'" + usage 3 +fi + if [ "$HOST" = "" -o "$PORT" = "" ]; then echoerr "Error: you need to provide a host and port to test." usage 2 diff --git a/wait-for.bats b/wait-for.bats index cbea6a4..e5ff462 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -12,3 +12,23 @@ [ "$status" -ne 0 ] [ "$output" != "success" ] } + +@test "support condensed option style" { + run ./wait-for -qt1 google.com:80 -- echo 'success' + + [ "$output" = "success" ] +} + +@test "timeout cannot be negative" { + run ./wait-for -t -1 google.com:80 -- echo 'success' + + [ "$status" -ne 0 ] + [ "$output" != "success" ] +} + +@test "timeout cannot be empty" { + run ./wait-for -t -- google.com:80 -- echo 'success' + + [ "$status" -ne 0 ] + [ "$output" != "success" ] +}