#!/usr/bin/env bash # onboard-project.sh — create a new project + ingest key in error_reporting. # # Usage: # onboard-project.sh [label] # # Generates a random ingest token, prints the SQL to apply and the # plaintext token to configure in the client. Requires: openssl, sha256sum. # # Example: # ./scripts/onboard-project.sh crm "BitReport CRM" # ./scripts/onboard-project.sh ott "BitReport OTT" staging # # Environment: # DATABASE_URL If set and mysql client available, applies SQL directly. # Otherwise prints SQL to stdout. set -euo pipefail _die() { echo "error: $*" >&2; exit 1; } [[ $# -ge 2 ]] || _die "Usage: onboard-project.sh [label]" SLUG="$1" NAME="$2" LABEL="${3:-default}" # Validate slug: lowercase alphanumeric + hyphens, 2-64 chars [[ "${SLUG}" =~ ^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$ ]] \ || _die "slug must be 2-64 lowercase alphanumeric/hyphens: ${SLUG}" # Generate a random 32-byte token (er__) SLUG_PREFIX="$(echo "${SLUG}" | tr -d '-' | head -c6)" RANDOM_PART="$(openssl rand -hex 20)" TOKEN="er_${SLUG_PREFIX}_${RANDOM_PART}" # SHA-256 hash (stored in DB) TOKEN_HASH="$(printf '%s' "${TOKEN}" | sha256sum | awk '{print $1}')" # Token prefix for display (first 8 chars) TOKEN_PREFIX="$(printf '%s' "${TOKEN}" | head -c8)" # Build SQL SQL="$(cat </dev/null 2>&1; then echo "DATABASE_URL is set. Applying SQL..." # Parse mysql:// URL DB_URL="${DATABASE_URL#mysql://}" DB_USER="${DB_URL%%:*}" DB_REST="${DB_URL#*:}" DB_PASS="${DB_REST%%@*}" DB_REST="${DB_REST#*@}" DB_HOST="${DB_REST%%:*}" DB_REST="${DB_REST#*:}" DB_PORT="${DB_REST%%/*}" DB_NAME="${DB_REST#*/}" echo "${SQL}" | mysql -h"${DB_HOST}" -P"${DB_PORT}" -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" echo "Applied successfully." fi