From 22d68b4494433143dc6621f1776a7cdd1b762ed7 Mon Sep 17 00:00:00 2001 From: Nicholas Yip Date: Thu, 3 Dec 2020 01:55:49 +0900 Subject: [PATCH 1/4] feat(option): Restrict the timeout input to non-negative integers --- wait-for | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wait-for b/wait-for index e28e54c..a685a69 100755 --- a/wait-for +++ b/wait-for @@ -55,7 +55,6 @@ do ;; -t) TIMEOUT="$2" - if [ "$TIMEOUT" = "" ]; then break; fi shift 2 ;; --timeout=*) @@ -76,6 +75,11 @@ do 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 From 23bbf6de3de3b0a65e0ff30f2d570602c4cf755c Mon Sep 17 00:00:00 2001 From: Nicholas Yip Date: Thu, 3 Dec 2020 19:40:13 +0900 Subject: [PATCH 2/4] fix(iteration): Remember to try one last time before giving up --- wait-for | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wait-for b/wait-for index a685a69..28b9b23 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 From 526918b462f1745163d28df38aa3b72ad8d88fda Mon Sep 17 00:00:00 2001 From: Nicholas Yip Date: Thu, 3 Dec 2020 02:39:09 +0900 Subject: [PATCH 3/4] feat(option): Support more conventional formats in the option parser --- wait-for | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/wait-for b/wait-for index 28b9b23..a8367c7 100755 --- a/wait-for +++ b/wait-for @@ -59,10 +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" shift 2 ;; + -t*) + TIMEOUT="${1#-t}" + shift 1 + ;; --timeout=*) TIMEOUT="${1#*=}" shift 1 @@ -74,7 +89,13 @@ do --help) usage 0 ;; + -*) + QUIET=0 + echoerr "Unknown option: $1" + usage 1 + ;; *) + QUIET=0 echoerr "Unknown argument: $1" usage 1 ;; From 988d3f0d65058a5e39971ca1f5bc7c8ee43264c3 Mon Sep 17 00:00:00 2001 From: Adriaan Knapen Date: Tue, 2 Feb 2021 07:51:58 +0900 Subject: [PATCH 4/4] test(option): Include test cases --- wait-for.bats | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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" ] +}