nix-config/packages/whst/default.nix

67 lines
1.1 KiB
Nix

{
writeShellScriptBin,
}:
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"
xdg-open "$url" >/dev/null 2>&1
ret=$?
if (( ret != 0 )); then
echo "Failed to open browser automatically. Here's the URL instead:"
echo "$url"
exit 1
fi
exit 0
''