#!/usr/bin/env bash
# nerf-az-pipelines-run-show -- Show details for a single pipeline run (by run ID). Returns the run's status, result, source branch, queue/start/finish times, requesting identity, and the URL to view it in the Azure DevOps UI.
# Generated from az-pipelines manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-az-pipelines-run-show 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-az-pipelines-run-show [--project|-p <project>] <run_id>

Options:
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <run_id> (required)
      Pipeline run ID (numeric, from az-pipelines-runs or az-pipelines-check)
      Must match: ^[0-9]+$

Maps to: az pipelines runs show --id <run_id> <project> --output json

Show details for a single pipeline run (by run ID). Returns the run's status, result, source branch, queue/start/finish times, requesting identity, and the URL to view it in the Azure DevOps UI.
EOF
  exit 1
}

PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --project|-p) if [[ -n "${_PROJECT_SET}" ]]; then echo "error: --project can only be specified once" >&2; exit 1; fi; PROJECT="$2"; _PROJECT_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_RUN_ID_SET=""
if [[ $# -gt 0 ]]; then
  RUN_ID="$1"
  _RUN_ID_SET=true
  shift
else
  RUN_ID=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-az-pipelines-run-show: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_RUN_ID_SET}" ]] && [[ "${RUN_ID}" == -* ]]; then
  echo "error: nerf-az-pipelines-run-show: <run_id> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${RUN_ID}" ]]; then
  echo "error: nerf-az-pipelines-run-show: missing required argument <run_id>" >&2
  echo "  hint: provide a value for <run_id>" >&2
  usage
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${_RUN_ID_SET}" ]] && ! [[ "${RUN_ID}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-pipelines-run-show: argument <run_id> does not match required pattern" >&2
  echo "  value:   \"${RUN_ID}\"" >&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=(az pipelines runs show --id "${RUN_ID}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json)
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec az pipelines runs show --id "${RUN_ID}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json
