#!/usr/bin/env bash
# nerf-az-pipelines-check -- Show details and the 10 most recent runs for a specific pipeline (by ID)
# Generated from az-pipelines 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-az-pipelines-check <pipeline_id>

Arguments:
  <pipeline_id> (required)
      Pipeline ID (numeric, from az-pipelines-list)
      Must match: ^[0-9]+$

Maps to: az pipelines runs list --pipeline-ids <pipeline_id> --top 10 --output json

Show details and the 10 most recent runs for a specific pipeline (by ID).
EOF
  exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

PIPELINE_ID="${1:-}"
shift 2>/dev/null || true

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

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

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${PIPELINE_ID}" ]] && ! [[ "${PIPELINE_ID}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-pipelines-check: argument <pipeline_id> does not match required pattern" >&2
  echo "  value:   \"${PIPELINE_ID}\"" >&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: az pipelines runs list --pipeline-ids "${PIPELINE_ID}" --top 10 --output json"
  exit 0
fi

exec az pipelines runs list --pipeline-ids "${PIPELINE_ID}" --top 10 --output json
