mirror of
https://github.com/thegeeklab/wait-for.git
synced 2024-11-22 04:10:40 +00:00
feat: add fallback to /dev/tcp on bash if netcat not found (#51)
This commit is contained in:
parent
3774a8541a
commit
3cd05d4e3f
11
.drone.yml
11
.drone.yml
@ -12,11 +12,18 @@ steps:
|
|||||||
commands:
|
commands:
|
||||||
- shellcheck ./wait-for
|
- shellcheck ./wait-for
|
||||||
|
|
||||||
- name: test
|
- name: test-nc
|
||||||
image: bats/bats
|
image: bats/bats
|
||||||
commands:
|
commands:
|
||||||
- bats ./wait-for.bats
|
- bats ./wait-for.bats
|
||||||
|
|
||||||
|
- name: test-bash
|
||||||
|
image: bats/bats
|
||||||
|
commands:
|
||||||
|
- rm -rf /usr/bin/nc
|
||||||
|
- apk add bash
|
||||||
|
- bats ./wait-for.bats
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
ref:
|
ref:
|
||||||
- refs/heads/main
|
- refs/heads/main
|
||||||
@ -220,6 +227,6 @@ depends_on:
|
|||||||
|
|
||||||
---
|
---
|
||||||
kind: signature
|
kind: signature
|
||||||
hmac: 52a6f7275d25c344cfe1d072d1a2b2a8eb95ffa82962921ae872443565a52b33
|
hmac: bd88ea52d53fb53375b5a4e4e905f2b7e76760ec88fe81d4a0df93319ad635e4
|
||||||
|
|
||||||
...
|
...
|
||||||
|
22
wait-for
22
wait-for
@ -3,6 +3,9 @@
|
|||||||
WAITFOR_TIMEOUT=${WAITFOR_TIMEOUT:-15}
|
WAITFOR_TIMEOUT=${WAITFOR_TIMEOUT:-15}
|
||||||
WAITFOR_QUIET=${WAITFOR_QUIET:-0}
|
WAITFOR_QUIET=${WAITFOR_QUIET:-0}
|
||||||
|
|
||||||
|
HAS_NC=0
|
||||||
|
HAS_BASH=0
|
||||||
|
|
||||||
echoerr() {
|
echoerr() {
|
||||||
if [ "$WAITFOR_QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
|
if [ "$WAITFOR_QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
|
||||||
}
|
}
|
||||||
@ -24,7 +27,12 @@ USAGE
|
|||||||
|
|
||||||
wait_for() {
|
wait_for() {
|
||||||
for _ in $(seq "$WAITFOR_TIMEOUT"); do
|
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
|
||||||
|
bash -c "</dev/tcp/$WAITFOR_HOST/$WAITFOR_PORT" >/dev/null 2>&1
|
||||||
|
fi
|
||||||
|
|
||||||
result=$?
|
result=$?
|
||||||
if [ $result -eq 0 ]; then
|
if [ $result -eq 0 ]; then
|
||||||
@ -73,8 +81,16 @@ while [ $# -gt 0 ]; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if ! [ -x "$(command -v nc)" ]; then
|
if [ -x "$(command -v nc)" ]; then
|
||||||
echoerr "error: netcat is required for wait-for to run"
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user