#!/usr/bin/env bash # Shared helpers for the prospector local scripts (install / migrate / app). # Sourced, not executed. Provides: logging, env loading, psql locating. # Colors (best effort; degrade to empty on dumb terminals). RED=$'\e[31m' YELLOW=$'\e[33m' BLUE=$'\e[34m' GREEN=$'\e[32m' NC=$'\e[0m' || true REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" info() { echo -e "${BLUE}==>${NC} $*"; } ok() { echo -e "${GREEN}✓${NC} $*"; } warn() { echo -e "${YELLOW}⚠️ ${NC} $*" >&2; } die() { echo -e "${RED}error:${NC} $*" >&2; exit 1; } # Load KEY=VALUE pairs from .env.local (if present) into the environment. load_env() { local f="$REPO_ROOT/.env.local" if [[ -f "$f" ]]; then set -a; . "$f"; set +a fi } # Locate a usable psql, honoring $PSQL, then PATH, then common macOS installs. # Echoes the path; empty if none found. find_psql() { if [[ -n "${PSQL:-}" && -x "${PSQL}" ]]; then echo "$PSQL"; return; fi if command -v psql >/dev/null 2>&1; then command -v psql; return; fi local c for c in \ /opt/homebrew/bin/psql \ /opt/homebrew/opt/postgresql@*/bin/psql \ /usr/local/bin/psql \ /usr/local/opt/postgresql@*/bin/psql \ /Applications/Postgres.app/Contents/Versions/latest/bin/psql \ /Library/PostgreSQL/*/bin/psql; do [[ -x "$c" ]] && { echo "$c"; return; } done echo "" } # Build a psql connection string from PROSPECTOR_DB_* env (defaults to a local # database named in PROSPECTOR_DB_NAME). Pass the target database as $1. pg_uri() { local db="$1" local host="${PROSPECTOR_DB_HOST:-127.0.0.1}" local port="${PROSPECTOR_DB_PORT:-5432}" local user="${PROSPECTOR_DB_USER:-$USER}" local pass="${PROSPECTOR_DB_PASSWORD:-}" if [[ -n "$pass" ]]; then echo "postgresql://${user}:${pass}@${host}:${port}/${db}" else echo "postgresql://${user}@${host}:${port}/${db}" fi }