#!/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

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-gh-run-list requires bash 4+. Found bash ${BASH_VERSION:-unknown}" >&2
  echo "  hint: on macOS, install a newer bash via 'brew install bash'" >&2
  exit 1
fi

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=""
_WORKFLOW_SET=""
BRANCH=""
_BRANCH_SET=""
STATUS=""
_STATUS_SET=""
LIMIT=""
_LIMIT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --workflow|-w) if [[ -n "${_WORKFLOW_SET}" ]]; then echo "error: --workflow can only be specified once" >&2; exit 1; fi; WORKFLOW="$2"; _WORKFLOW_SET=true; shift 2 ;;
    --branch|-b) if [[ -n "${_BRANCH_SET}" ]]; then echo "error: --branch can only be specified once" >&2; exit 1; fi; BRANCH="$2"; _BRANCH_SET=true; shift 2 ;;
    --status|-s) if [[ -n "${_STATUS_SET}" ]]; then echo "error: --status can only be specified once" >&2; exit 1; fi; STATUS="$2"; _STATUS_SET=true; shift 2 ;;
    --limit|-L) if [[ -n "${_LIMIT_SET}" ]]; then echo "error: --limit can only be specified once" >&2; exit 1; fi; LIMIT="$2"; _LIMIT_SET=true; 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_SET}" ]] && [[ "${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_SET}" ]] && ! [[ "${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
  _NERF_DRY_CMD=(gh run list ${_WORKFLOW_SET:+"--workflow"} ${_WORKFLOW_SET:+"$WORKFLOW"} ${_BRANCH_SET:+"--branch"} ${_BRANCH_SET:+"$BRANCH"} ${_STATUS_SET:+"--status"} ${_STATUS_SET:+"$STATUS"} ${_LIMIT_SET:+"--limit"} ${_LIMIT_SET:+"$LIMIT"})
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

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