← back to Pm2 Migration

patch-connections.sh

43 lines

#!/usr/bin/env bash
# patch-connections.sh <dir>
# Rewrite hardcoded Kamatera DB ports to the local autossh tunnel ports.
#   postgres :5432 → :15432
#   redis    :6379 → :16379
# Only touches .js, .ts, .json, .py, .sh files (not node_modules).

set -euo pipefail
TARGET="${1:?usage: patch-connections.sh <dir>}"

if [[ ! -d "$TARGET" ]]; then
  echo "no such dir: $TARGET" >&2
  exit 1
fi

# Find candidate files. Skip node_modules and .git.
files=$(find "$TARGET" -type f \
  \( -name '*.js' -o -name '*.ts' -o -name '*.cjs' -o -name '*.mjs' \
     -o -name '*.json' -o -name '*.py' -o -name '*.sh' -o -name '.env*' \) \
  -not -path '*/node_modules/*' -not -path '*/.git/*')

count=0
for f in $files; do
  # postgres — rewrite with separate sed calls to avoid macOS sed delimiter conflict
  if grep -qE '(127\.0\.0\.1|localhost):5432' "$f" 2>/dev/null; then
    sed -i '' \
      -e 's|127\.0\.0\.1:5432|127.0.0.1:15432|g' \
      -e 's|localhost:5432|localhost:15432|g' \
      "$f"
    count=$((count+1))
  fi
  # redis
  if grep -qE '(127\.0\.0\.1|localhost):6379' "$f" 2>/dev/null; then
    sed -i '' \
      -e 's|127\.0\.0\.1:6379|127.0.0.1:16379|g' \
      -e 's|localhost:6379|localhost:16379|g' \
      "$f"
    count=$((count+1))
  fi
done

echo "patched $count file occurrence(s) in $TARGET"