← back to Secrets Manager

auto-ingest.sh

52 lines

#!/usr/bin/env bash
# Auto-ingest watcher.
# Runs every 30s via launchd com.steve.secrets-auto-ingest.
# - Hashes ~/Desktop/site-factory.env. If changed since last run AND contains
#   non-empty values for keys not yet in registry → run cli.js sync.
# - If after sync the "site-factory trigger set" is complete, fire run-all-stages.sh.
# - Plays a sound when something happens (so Steve knows from anywhere).
set -uo pipefail

DESK=/Users/macstudio3/Desktop/site-factory.env
ROOT=/Users/macstudio3/Projects/secrets-manager
STATE=$ROOT/.auto-ingest-state
LOG=$ROOT/auto-ingest.log
TS() { date +%FT%T%z; }

[ -f "$DESK" ] || exit 0

# Hash the desktop file (only the KEY=value lines, not comments)
HASH=$(grep -E '^[A-Z_]+=.+$' "$DESK" 2>/dev/null | sort | shasum -a 256 | awk '{print $1}')
PREV=""
[ -f "$STATE" ] && PREV=$(cat "$STATE")

[ "$HASH" = "$PREV" ] && exit 0   # no change

mkdir -p "$ROOT"
echo "[$(TS)] desktop env changed (hash $HASH) — running sync" >> "$LOG"

# Pipe non-empty key=value lines into import-paste (which validates each)
NEW_LINES=$(grep -E '^[A-Z_]+=[^[:space:]#]+' "$DESK" 2>/dev/null)
if [ -n "$NEW_LINES" ]; then
  printf '%s\n' "$NEW_LINES" | node "$ROOT/cli.js" import-paste >> "$LOG" 2>&1 || true
fi

# Check if site-factory trigger set is complete enough to fire pipeline
HAVE() { node "$ROOT/cli.js" list 2>/dev/null | grep -q "✓ $1 "; }

CAN_FIRE=true
for k in CLOUDFLARE_API_TOKEN STRIPE_SECRET_KEY VERCEL_TOKEN; do
  HAVE "$k" || CAN_FIRE=false
done

if $CAN_FIRE; then
  echo "[$(TS)] trigger set complete — firing run-all-stages.sh" >> "$LOG"
  bash /Users/macstudio3/Projects/site-factory/scripts/run-all-stages.sh >> "$LOG" 2>&1 &
  /usr/bin/afplay /System/Library/Sounds/Glass.aiff 2>/dev/null &
  /usr/bin/osascript -e 'display notification "Site Factory pipeline firing" with title "Secrets Manager"' 2>/dev/null &
else
  /usr/bin/afplay /System/Library/Sounds/Tink.aiff 2>/dev/null &
fi

echo "$HASH" > "$STATE"