summaryrefslogtreecommitdiff
path: root/port-wait
blob: 7f12d27179f2e890f2f3f182568f1d1c45cb37dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash

set -euo pipefail

help() {
  cat <<EOF
Usage: ${0##*/} [-p] [-h] [HOST] PORT

Wait until specified PORT is available on HOST (localhost if HOST ommited).

  -p  print progress every second
  -h  print this help
EOF
}

main() {
  show_progress=""
  while getopts "ph" opt; do
    case "${opt}" in
    p)
      show_progress=t
      ;;
    h)
      help
      exit
      ;;
    *)
      help
      exit 1
      ;;
    esac
  done
  shift $((OPTIND - 1))

  if [ $# -eq 2 ]; then
    host="$1"
    port="$2"
  elif [ $# -eq 1 ]; then
    port="$1"
  else
    echo "No host/port provided"
    help
    exit 1
  fi

  until nc -z "${host:-localhost}" "$port"; do
    [ -n "$show_progress" ] && echo -n .
    sleep 1
  done
}

main "$@"