diff --git a/.drone.yml b/.drone.yml index 236671c..c9da4dc 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,11 +12,18 @@ steps: commands: - shellcheck ./wait-for - - name: test + - name: test-nc image: bats/bats commands: - bats ./wait-for.bats + - name: test-bash + image: bats/bats + commands: + - apk add bash + - rm -rf /usr/bin/nc + - bats ./wait-for.bats + trigger: ref: - refs/heads/main diff --git a/wait-for b/wait-for index 930bae8..2143b6a 100755 --- a/wait-for +++ b/wait-for @@ -3,6 +3,9 @@ WAITFOR_TIMEOUT=${WAITFOR_TIMEOUT:-15} WAITFOR_QUIET=${WAITFOR_QUIET:-0} +HAS_NC=0 +HAS_BASH=0 + echoerr() { if [ "$WAITFOR_QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi } @@ -24,7 +27,12 @@ USAGE wait_for() { for _ in $(seq "$WAITFOR_TIMEOUT"); do - nc -w 1 -z "$WAITFOR_HOST" "$WAITFOR_PORT" >/dev/null 2>&1 + if [ $HAS_NC = 1 ]; then + nc -w 1 -z "$WAITFOR_HOST" "$WAITFOR_PORT" >/dev/null 2>&1 + elif [ $HAS_BASH = 1 ]; then + # shellcheck disable=SC3025 + (echo >/dev/tcp/"$WAITFOR_HOST"/"$WAITFOR_PORT") >/dev/null 2>&1 + fi result=$? if [ $result -eq 0 ]; then @@ -73,8 +81,16 @@ while [ $# -gt 0 ]; do esac done -if ! [ -x "$(command -v nc)" ]; then - echoerr "error: netcat is required for wait-for to run" +if [ -x "$(command -v nc)" ]; then + HAS_NC=1 +fi + +if [ -x "$(command -v bash)" ]; then + HAS_BASH=1 +fi + +if [ $HAS_NC = 0 ] || [ $HAS_BASH = 0 ]; then + echoerr "error: netcat or bash is required for wait-for to run" exit 1 fi