#!/usr/bin/env bash
#MISE description="Run teral CLI (usage: mise run teral -- dev seed local)"
set -euo pipefail

# True when the invocation is `teral dev stripe ...` (clock, reset, invoices, grants).
needs_stripe_env() {
  local prev=""
  for arg in "$@" ${ARGS:-}; do
    if [[ "$prev" == "dev" && "$arg" == "stripe" ]]; then
      return 0
    fi
    prev="$arg"
  done
  return 1
}

set -a
[ -f .env ] && . ./.env
if needs_stripe_env "$@"; then
  # Match Tilt/ctrl: the shell's `stripe login` key may point at a different
  # sandbox than dev/.env.stripe, so load it for stripe dev tools only.
  [ -f dev/.env.stripe ] && . ./dev/.env.stripe
fi
set +a

go run . "$@" ${ARGS:-}
