Skip to content

Instantly share code, notes, and snippets.

@haf
Created August 22, 2024 09:28
Show Gist options
  • Save haf/23752e88443aca286017a7b590fb76d0 to your computer and use it in GitHub Desktop.
Save haf/23752e88443aca286017a7b590fb76d0 to your computer and use it in GitHub Desktop.
Waiting for a URL
#compdef wait-for-url.sh
_wait_for_url() {
local -a options
options=(
'--silent[Suppress all output]'
'--timeout[Set timeout in seconds]:timeout (seconds):'
'--expect-status-code[Expected HTTP status code]:status code:'
'--path[Append this path to the URL]:path:'
'--help[Show help message and exit]'
)
_arguments \
$options \
'1:URL:_urls' \
'*::curl options:->curl_opts'
if [[ $state == curl_opts ]]; then
_curl
fi
}
_wait_for_url "$@"
# Save this file in a directory that's in your zsh's fpath. Common locations include:
#
# /usr/local/share/zsh/site-functions/
# ~/.zsh/completions/
# check your fpath:
# echo $fpath
# Then, run the following command to rehash zsh completions:
#
# autoload -U compinit && compinit
#!/usr/bin/env bash
# Function to print help message
print_help() {
cat <<EOF
Usage: $0 [OPTIONS] <URL> [CURL_OPTIONS]
Options:
--silent Suppress all output
--timeout SECONDS Set timeout in seconds (default: 60)
--expect-status-code CODE Expected HTTP status code (default: 200)
--path PATH Append this path to the URL
--help Show this help message and exit
Any additional arguments will be passed to curl.
Curl options:
EOF
curl --help
exit 0
}
# Initialize variables
SILENT=false
TIMEOUT=60
EXPECT_STATUS_CODE=200
PATH_SUFFIX=""
URL=""
CURL_ARGS=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--silent)
SILENT=true
shift
;;
--timeout)
TIMEOUT="$2"
shift 2
;;
--expect-status-code)
EXPECT_STATUS_CODE="$2"
shift 2
;;
--path)
PATH_SUFFIX="$2"
shift 2
;;
--help)
print_help
;;
http*://*)
URL="$1"
shift
;;
*)
CURL_ARGS="$CURL_ARGS $1"
shift
;;
esac
done
# Check if URL is provided
if [ -z "$URL" ]; then
echo "Error: No URL provided. Use --help for usage information." >&2
exit 1
fi
# Append path if provided
if [ -n "$PATH_SUFFIX" ]; then
URL="${URL%/}/${PATH_SUFFIX#/}"
fi
# Function to print messages if not in silent mode
print_msg() {
if [ "$SILENT" = false ]; then
echo -e "$1"
fi
}
print_msg "Waiting for $URL to become available (timeout: ${TIMEOUT}s, expected status: ${EXPECT_STATUS_CODE})..."
# Try to curl the URL
start_time=$(date +%s)
while true; do
response=$(curl -o /dev/null -s -w "%{http_code}" $CURL_ARGS -H "User-Agent: curl-wait-for-http" "$URL")
if [ "$response" = "$EXPECT_STATUS_CODE" ]; then
print_msg "\n$URL is now available with status code $response!"
exit 0
fi
current_time=$(date +%s)
elapsed=$((current_time - start_time))
if [ $elapsed -ge $TIMEOUT ]; then
print_msg ""
if [ "$SILENT" = false ]; then
echo "Timeout reached. Failed to get status code $EXPECT_STATUS_CODE from $URL (last response: $response)" >&2
fi
exit 1
fi
if [ "$SILENT" = false ]; then
printf '.'
fi
sleep 0.5
done
# Zsh completion
if [ -n "$ZSH_VERSION" ]; then
compdef _wait_for_url wait-for-url.sh
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment