88 lines
1.6 KiB
Nix
88 lines
1.6 KiB
Nix
{
|
|
writeShellScriptBin,
|
|
imv,
|
|
curl,
|
|
imagemagick,
|
|
}:
|
|
|
|
writeShellScriptBin "whst" ''
|
|
print_help () {
|
|
cat << EOF
|
|
Usage: whst <status_code> [options]
|
|
|
|
Check the meaning or details of a given HTTP status code.
|
|
|
|
Arguments:
|
|
status_code Integer HTTP status code to look up (e.g., 200, 404).
|
|
|
|
Options:
|
|
-h Show this help message and exit.
|
|
|
|
Examples:
|
|
whst 200
|
|
whst 404 -h
|
|
|
|
EOF
|
|
}
|
|
|
|
OPTSTRING="h"
|
|
|
|
while getopts "$OPTSTRING" opt; do
|
|
case "$opt" in
|
|
h)
|
|
print_help
|
|
exit 0
|
|
;;
|
|
?)
|
|
echo "Invalid option: -$OPTARG." >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
if (( $# < 1 )); then
|
|
echo "Error: Missing required status_code argument." >&2
|
|
echo "Use -h for help." >&2
|
|
exit 1
|
|
fi
|
|
|
|
status_code="$1"
|
|
|
|
if ! [[ "$status_code" =~ ^[0-9]+$ ]]; then
|
|
echo "Error: status_code must be an integer." >&2
|
|
exit 1
|
|
fi
|
|
|
|
url="https://http.cat/$status_code"
|
|
|
|
tmpfile="$(mktemp --suffix=.jpg)"
|
|
|
|
if ! ${curl}/bin/curl -fsSL "$url" -o "$tmpfile" 2>/dev/null; then
|
|
echo "Error: The status code $status_code is non existent :(" >&2
|
|
rm -f "$tmpfile"
|
|
exit 1
|
|
fi
|
|
|
|
dims="$(${imagemagick}/bin/identify -format '%w %h' "$tmpfile" 2>/dev/null)"
|
|
if [ -z "$dims" ]; then
|
|
echo "Error: Failed to determine image size." >&2
|
|
rm -f "$tmpfile"
|
|
exit 1
|
|
fi
|
|
|
|
# Split into width and height using shell
|
|
set -- $dims
|
|
width="$1"
|
|
height="$2"
|
|
|
|
if ! ${imv}/bin/imv -W "$width" -H "$height" "$tmpfile"; then
|
|
echo "Error: Failed to display image with imv." >&2
|
|
rm -f "$tmpfile"
|
|
exit 1
|
|
fi
|
|
|
|
rm -f "$tmpfile"
|
|
exit 0
|
|
''
|