#!/bin/bash # Harness: Cold 啟動環境檢查 # 用途:init 完成後強制檢查環境就緒,不靠提醒,違反條件直接擋住(exit 2) # 由 acr init 末尾調用 set -e REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo ".")" ARCRUN_CONFIG="${HOME}/.arcrun/config.yaml" # 顏色 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color FAILED=0 echo -e "${YELLOW}🔍 冷啟動環境檢查${NC}" echo "" # 檢查 1: config.yaml 存在 if [ ! -f "$ARCRUN_CONFIG" ]; then echo -e "${RED}❌ 缺少 $ARCRUN_CONFIG${NC}" echo " 執行:acr init" FAILED=1 else echo -e "${GREEN}✓ config.yaml 存在${NC}" fi # 檢查 2: api_key 有值 if grep -q "^api_key:" "$ARCRUN_CONFIG" 2>/dev/null; then api_key=$(grep "^api_key:" "$ARCRUN_CONFIG" | cut -d' ' -f2) if [ -z "$api_key" ] || [ "$api_key" = "null" ]; then echo -e "${RED}❌ api_key 未設${NC}" FAILED=1 else echo -e "${GREEN}✓ api_key 已設${NC}" fi else echo -e "${RED}❌ config.yaml 缺少 api_key${NC}" FAILED=1 fi # 檢查 3: .mcp.json 存在且指向自己(self-hosted 用) if grep -q "mode.*self-hosted" "$ARCRUN_CONFIG" 2>/dev/null; then if [ ! -f ".mcp.json" ]; then echo -e "${RED}❌ .mcp.json 缺失(self-hosted 必需)${NC}" echo " 執行:acr init 會自動生成" FAILED=1 else # 檢查 mcp.json 是否指向自己(不是官方 URL) if grep -q "mcp.arcrun.dev" ".mcp.json" && ! grep -q "workers.dev" ".mcp.json"; then echo -e "${RED}❌ .mcp.json 指向官方 mcp.arcrun.dev(應指向自己的 worker)${NC}" FAILED=1 else echo -e "${GREEN}✓ .mcp.json 配置正確${NC}" fi fi fi # 檢查 4: acr 能執行 if ! command -v acr &>/dev/null; then echo -e "${RED}❌ acr 未裝或不在 PATH${NC}" echo " 執行:npm i -g arcrun" FAILED=1 else echo -e "${GREEN}✓ acr 可執行${NC}" fi # 檢查 5: D1 database 存在(如果 self-hosted) if grep -q "mode.*self-hosted" "$ARCRUN_CONFIG" 2>/dev/null; then if grep -q "d1_database_id" "$ARCRUN_CONFIG" 2>/dev/null; then db_id=$(grep "d1_database_id" "$ARCRUN_CONFIG" | cut -d' ' -f2) if [ -z "$db_id" ]; then echo -e "${RED}❌ D1 database_id 未設${NC}" echo " 執行:acr init 會建立 D1,或手動設定 d1_database_id" FAILED=1 else echo -e "${GREEN}✓ D1 database_id 已設${NC}" fi fi fi echo "" if [ $FAILED -eq 0 ]; then echo -e "${GREEN}✅ 環境就緒${NC}" exit 0 else echo -e "${RED}❌ 環境檢查失敗${NC}" echo "" echo "修復步驟:" echo "1. 檢查 ~/.arcrun/config.yaml 是否完整" echo "2. 若 self-hosted,確認 .mcp.json 指向自己的 mcp worker(不是官方)" echo "3. 若缺少 D1,重跑 acr init --self-hosted --account-id=... --api-token=..." echo "" exit 2 fi