#!/usr/bin/env bash
# nerf-gh-run-list -- List recent workflow runs
# Generated from gh manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-gh-run-list [--workflow|-w <workflow>] [--branch|-b <branch>] [--status|-s <status>] [--limit|-L <limit>]

Options:
  --workflow, -w <workflow>
      Filter by workflow name or filename
  --branch, -b <branch>
      Filter by branch
  --status, -s <status>
      Filter by status
      Allowed values: queued, in_progress, completed, waiting, requested, action_required, cancelled, failure, neutral, skipped, stale, startup_failure, success, timed_out
  --limit, -L <limit>
      Maximum number of runs to list (default 20)
      Must match: ^[0-9]+$

Maps to: gh run list <workflow> <branch> <status> <limit>

List recent workflow runs.
EOF
  exit 1
}

WORKFLOW=""
BRANCH=""
STATUS=""
LIMIT=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --workflow|-w) if [[ -n "${WORKFLOW}" ]]; then echo "error: --workflow can only be specified once" >&2; exit 1; fi; WORKFLOW="$2"; shift 2 ;;
    --branch|-b) if [[ -n "${BRANCH}" ]]; then echo "error: --branch can only be specified once" >&2; exit 1; fi; BRANCH="$2"; shift 2 ;;
    --status|-s) if [[ -n "${STATUS}" ]]; then echo "error: --status can only be specified once" >&2; exit 1; fi; STATUS="$2"; shift 2 ;;
    --limit|-L) if [[ -n "${LIMIT}" ]]; then echo "error: --limit can only be specified once" >&2; exit 1; fi; LIMIT="$2"; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) echo "error: unknown argument: $1" >&2; usage ;;
  esac
done

if [[ -n "${STATUS}" ]] && [[ "${STATUS}" != "queued" && "${STATUS}" != "in_progress" && "${STATUS}" != "completed" && "${STATUS}" != "waiting" && "${STATUS}" != "requested" && "${STATUS}" != "action_required" && "${STATUS}" != "cancelled" && "${STATUS}" != "failure" && "${STATUS}" != "neutral" && "${STATUS}" != "skipped" && "${STATUS}" != "stale" && "${STATUS}" != "startup_failure" && "${STATUS}" != "success" && "${STATUS}" != "timed_out" ]]; then
  echo "error: nerf-gh-run-list: option --status is not an allowed value" >&2
  echo "  value:   \"${STATUS}\"" >&2
  echo "  allowed: queued, in_progress, completed, waiting, requested, action_required, cancelled, failure, neutral, skipped, stale, startup_failure, success, timed_out" >&2
  echo "  hint: use one of the allowed values" >&2
  exit 1
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${LIMIT}" ]] && ! [[ "${LIMIT}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-gh-run-list: option --limit does not match required pattern" >&2
  echo "  value:   \"${LIMIT}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: gh run list ${WORKFLOW:+"--workflow"} ${WORKFLOW:+"$WORKFLOW"} ${BRANCH:+"--branch"} ${BRANCH:+"$BRANCH"} ${STATUS:+"--status"} ${STATUS:+"$STATUS"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}"
  exit 0
fi

exec gh run list ${WORKFLOW:+"--workflow"} ${WORKFLOW:+"$WORKFLOW"} ${BRANCH:+"--branch"} ${BRANCH:+"$BRANCH"} ${STATUS:+"--status"} ${STATUS:+"$STATUS"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}
