|
| 1 | +#!/usr/bin/env sh |
| 2 | +# |
| 3 | +# preflight.sh — environment checks for scaffolding a Strata Rails app. |
| 4 | +# Folds the old "check/install CLI" and "check Docker / Postgres port / git" steps. |
| 5 | +# |
| 6 | +# Prints labeled status lines. On a condition the user must resolve, prints a |
| 7 | +# NEEDS_* marker and exits non-zero so the calling skill can stop and ask. |
| 8 | +# On full success prints PREFLIGHT_OK and exits 0. |
| 9 | +# |
| 10 | +# Markers the skill watches for: |
| 11 | +# NEEDS_UV — nava-platform CLI missing and uv not installed |
| 12 | +# NEEDS_DOCKER — Docker daemon not running |
| 13 | +# NEEDS_PORT_FREE — port 5432 held by a process we won't auto-stop |
| 14 | +# PREFLIGHT_OK — all checks passed |
| 15 | + |
| 16 | +set -u |
| 17 | + |
| 18 | +# 1. nava-platform CLI ------------------------------------------------------- |
| 19 | +if nava-platform --help >/dev/null 2>&1; then |
| 20 | + echo "CLI: nava-platform already installed" |
| 21 | +else |
| 22 | + echo "CLI: nava-platform not found — attempting install via uv" |
| 23 | + if uv --version >/dev/null 2>&1; then |
| 24 | + if uv tool install git+https://github.com/navapbc/platform-cli; then |
| 25 | + if nava-platform --help >/dev/null 2>&1; then |
| 26 | + echo "CLI: nava-platform installed" |
| 27 | + else |
| 28 | + echo "NEEDS_UV: installed via uv but nava-platform still not on PATH (restart shell / check ~/.local/bin)" |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + else |
| 32 | + echo "NEEDS_UV: 'uv tool install' failed — see output above" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + else |
| 36 | + echo "NEEDS_UV: uv is not installed. Install it (https://docs.astral.sh/uv/getting-started/installation/) then re-run." |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | +fi |
| 40 | + |
| 41 | +# 2. Docker daemon ----------------------------------------------------------- |
| 42 | +if docker ps >/dev/null 2>&1; then |
| 43 | + echo "DOCKER: daemon running" |
| 44 | +else |
| 45 | + echo "NEEDS_DOCKER: Docker daemon not running — start Docker Desktop then re-run." |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# 3. Postgres port 5432 ------------------------------------------------------ |
| 50 | +PG_PIDS=$(lsof -iTCP:5432 -sTCP:LISTEN -t 2>/dev/null || true) |
| 51 | +if [ -z "$PG_PIDS" ]; then |
| 52 | + echo "PORT: 5432 free" |
| 53 | +else |
| 54 | + PG_CMD=$(lsof -iTCP:5432 -sTCP:LISTEN 2>/dev/null | awk 'NR==2 {print $1}') |
| 55 | + case "$PG_CMD" in |
| 56 | + com.docke* | docker | docker-proxy) |
| 57 | + echo "PORT: 5432 held by Docker ($PG_CMD) — stopping the container" |
| 58 | + CONTAINER=$(docker ps --filter "publish=5432" -q) |
| 59 | + if [ -n "$CONTAINER" ]; then |
| 60 | + docker stop $CONTAINER >/dev/null 2>&1 |
| 61 | + fi |
| 62 | + # re-check |
| 63 | + if [ -n "$(lsof -iTCP:5432 -sTCP:LISTEN -t 2>/dev/null || true)" ]; then |
| 64 | + echo "NEEDS_PORT_FREE: docker container stopped but 5432 still busy" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + echo "PORT: 5432 freed" |
| 68 | + ;; |
| 69 | + postgres) |
| 70 | + echo "NEEDS_PORT_FREE: native postgres on 5432 — do NOT auto-stop. Ask user to stop it (e.g. 'brew services stop postgresql@16') then re-run." |
| 71 | + exit 1 |
| 72 | + ;; |
| 73 | + *) |
| 74 | + echo "NEEDS_PORT_FREE: 5432 held by '${PG_CMD:-unknown}' — ask user to free the port then re-run." |
| 75 | + exit 1 |
| 76 | + ;; |
| 77 | + esac |
| 78 | +fi |
| 79 | + |
| 80 | +# 4. git repository ---------------------------------------------------------- |
| 81 | +if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then |
| 82 | + echo "GIT: already inside a git repo" |
| 83 | +else |
| 84 | + echo "GIT: not a repo — running git init" |
| 85 | + git init >/dev/null |
| 86 | + echo "GIT: initialized" |
| 87 | +fi |
| 88 | + |
| 89 | +echo "PREFLIGHT_OK" |
| 90 | +exit 0 |
0 commit comments