#!/usr/bin/env bash
# nerf-az-devops-set-default-project -- Set the default Azure DevOps project for subsequent az boards, az pipelines, and az repos commands. Persists to the user's az config (~/.azure). Useful when the git remote's project is not the project you want to operate on by default.
# Generated from az-devops manifest. Do not edit directly.
# nerf:threat:read=none
# nerf:threat:write=machine

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-az-devops-set-default-project 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-devops-set-default-project <project>

Arguments:
  <project> (required)
      Project name or ID to set as the default

Maps to: az devops configure --defaults project=<project>

Set the default Azure DevOps project for subsequent az boards, az pipelines, and az repos commands. Persists to the user's az config (~/.azure). Useful when the git remote's project is not the project you want to operate on by default.
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

_PROJECT_SET=""
if [[ $# -gt 0 ]]; then
  PROJECT="$1"
  _PROJECT_SET=true
  shift
else
  PROJECT=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-az-devops-set-default-project: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_PROJECT_SET}" ]] && [[ "${PROJECT}" == -* ]]; then
  echo "error: nerf-az-devops-set-default-project: <project> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${PROJECT}" ]]; then
  echo "error: nerf-az-devops-set-default-project: missing required argument <project>" >&2
  echo "  hint: provide a value for <project>" >&2
  usage
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az devops configure --defaults "project=${PROJECT}")
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec az devops configure --defaults "project=${PROJECT}"
