auto-detect-components.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # Auto-Detect Components Script v2.0.0
  4. # Scans .opencode directory for new components not in registry
  5. # Validates existing entries, fixes typos, removes deleted components
  6. # Performs security checks on component files
  7. #############################################################################
  8. set -e
  9. # Colors
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. BLUE='\033[0;34m'
  14. CYAN='\033[0;36m'
  15. MAGENTA='\033[0;35m'
  16. BOLD='\033[1m'
  17. NC='\033[0m'
  18. # Configuration
  19. REGISTRY_FILE="registry.json"
  20. REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
  21. AUTO_ADD=false
  22. DRY_RUN=false
  23. VALIDATE_EXISTING=true
  24. SECURITY_CHECK=true
  25. # Arrays to store components
  26. declare -a NEW_COMPONENTS
  27. declare -a FIXED_COMPONENTS
  28. declare -a REMOVED_COMPONENTS
  29. declare -a SECURITY_ISSUES
  30. # Counters
  31. TOTAL_FIXED=0
  32. TOTAL_REMOVED=0
  33. TOTAL_SECURITY_ISSUES=0
  34. #############################################################################
  35. # Utility Functions
  36. #############################################################################
  37. print_header() {
  38. echo -e "${CYAN}${BOLD}"
  39. echo "╔════════════════════════════════════════════════════════════════╗"
  40. echo "║ ║"
  41. echo "║ Auto-Detect Components v2.0.0 ║"
  42. echo "║ Enhanced with Security & Validation ║"
  43. echo "║ ║"
  44. echo "╚════════════════════════════════════════════════════════════════╝"
  45. echo -e "${NC}"
  46. }
  47. print_success() {
  48. echo -e "${GREEN}✓${NC} $1"
  49. }
  50. print_error() {
  51. echo -e "${RED}✗${NC} $1"
  52. }
  53. print_warning() {
  54. echo -e "${YELLOW}⚠${NC} $1"
  55. }
  56. print_info() {
  57. echo -e "${BLUE}ℹ${NC} $1"
  58. }
  59. print_security() {
  60. echo -e "${MAGENTA}🔒${NC} $1"
  61. }
  62. usage() {
  63. echo "Usage: $0 [OPTIONS]"
  64. echo ""
  65. echo "Options:"
  66. echo " -a, --auto-add Automatically add new components to registry"
  67. echo " -d, --dry-run Show what would be changed without modifying registry"
  68. echo " -s, --skip-validation Skip validation of existing registry entries"
  69. echo " -n, --no-security Skip security checks on component files"
  70. echo " -h, --help Show this help message"
  71. echo ""
  72. echo "Features:"
  73. echo " • Detects new components in .opencode directory"
  74. echo " • Validates existing registry entries"
  75. echo " • Auto-fixes typos and wrong paths"
  76. echo " • Removes entries for deleted components"
  77. echo " • Performs security checks (permissions, secrets, path validation)"
  78. echo ""
  79. exit 0
  80. }
  81. #############################################################################
  82. # Security Functions
  83. #############################################################################
  84. check_file_security() {
  85. local file=$1
  86. local issues=()
  87. # For markdown files, be less strict (they contain examples and documentation)
  88. if [[ "$file" == *.md ]]; then
  89. # Only check for executable permissions on markdown
  90. if [ -x "$file" ]; then
  91. issues+=("Markdown file should not be executable")
  92. fi
  93. # Check for actual secrets (not examples) - very specific patterns
  94. # Look for real API keys like sk-proj-xxxxx or ghp_xxxxx
  95. if grep -qE '(sk-proj-[a-zA-Z0-9]{40,}|ghp_[a-zA-Z0-9]{36,}|xox[baprs]-[a-zA-Z0-9-]{10,})' "$file" 2>/dev/null; then
  96. issues+=("Potential real API key detected")
  97. fi
  98. else
  99. # For non-markdown files, be more strict
  100. # Check file permissions (should not be world-writable)
  101. if [ -w "$file" ] && [ "$(stat -f '%A' "$file" 2>/dev/null || stat -c '%a' "$file" 2>/dev/null)" -gt 664 ]; then
  102. issues+=("File has overly permissive permissions")
  103. fi
  104. # Check for potential secrets
  105. if grep -qiE '(password|secret|api[_-]?key|token|credential|private[_-]?key).*[:=].*[a-zA-Z0-9]{20,}' "$file" 2>/dev/null; then
  106. issues+=("Potential hardcoded secrets detected")
  107. fi
  108. fi
  109. # Return issues
  110. if [ ${#issues[@]} -gt 0 ]; then
  111. printf '%s\n' "${issues[@]}"
  112. return 1
  113. fi
  114. return 0
  115. }
  116. run_security_checks() {
  117. if [ "$SECURITY_CHECK" = false ]; then
  118. return 0
  119. fi
  120. print_info "Running security checks..."
  121. echo ""
  122. local categories=("agent" "command" "tool" "plugin" "context")
  123. for category in "${categories[@]}"; do
  124. local category_dir="$REPO_ROOT/.opencode/$category"
  125. if [ ! -d "$category_dir" ]; then
  126. continue
  127. fi
  128. while IFS= read -r file; do
  129. local rel_path="${file#$REPO_ROOT/}"
  130. # Skip excluded directories
  131. if [[ "$rel_path" == *"/node_modules/"* ]] || \
  132. [[ "$rel_path" == *"/tests/"* ]] || \
  133. [[ "$rel_path" == *"/docs/"* ]]; then
  134. continue
  135. fi
  136. # Check security
  137. local security_output
  138. if ! security_output=$(check_file_security "$file"); then
  139. TOTAL_SECURITY_ISSUES=$((TOTAL_SECURITY_ISSUES + 1))
  140. SECURITY_ISSUES+=("${rel_path}|${security_output}")
  141. print_security "Security issue in: ${rel_path}"
  142. while IFS= read -r issue; do
  143. echo " - ${issue}"
  144. done <<< "$security_output"
  145. echo ""
  146. fi
  147. done < <(find "$category_dir" -type f -name "*.md" 2>/dev/null)
  148. done
  149. if [ $TOTAL_SECURITY_ISSUES -eq 0 ]; then
  150. print_success "No security issues found"
  151. echo ""
  152. fi
  153. }
  154. #############################################################################
  155. # Path Validation and Fixing
  156. #############################################################################
  157. find_similar_path() {
  158. local wrong_path=$1
  159. # Get directory and filename
  160. local dir=$(dirname "$wrong_path")
  161. local filename=$(basename "$wrong_path")
  162. # First, try to find exact filename match in category subdirectories
  163. # e.g., .opencode/agent/opencoder.md → .opencode/agent/core/opencoder.md
  164. local base_dir=$(echo "$dir" | cut -d'/' -f1-2) # e.g., .opencode/agent
  165. if [ -d "$REPO_ROOT/$base_dir" ]; then
  166. # Search recursively in the base directory for exact filename match
  167. while IFS= read -r candidate; do
  168. local candidate_rel="${candidate#$REPO_ROOT/}"
  169. local candidate_name=$(basename "$candidate")
  170. # Exact filename match
  171. if [[ "$candidate_name" == "$filename" ]]; then
  172. echo "$candidate_rel"
  173. return 0
  174. fi
  175. done < <(find "$REPO_ROOT/$base_dir" -type f -name "$filename" 2>/dev/null)
  176. fi
  177. # Fallback: search for similar names in the entire .opencode directory
  178. local search_dirs=("$REPO_ROOT/$dir" "$REPO_ROOT/.opencode")
  179. for search_dir in "${search_dirs[@]}"; do
  180. if [ ! -d "$search_dir" ]; then
  181. continue
  182. fi
  183. # Find files with similar names
  184. while IFS= read -r candidate; do
  185. local candidate_rel="${candidate#$REPO_ROOT/}"
  186. local candidate_name=$(basename "$candidate")
  187. # Simple similarity check
  188. if [[ "$candidate_name" == *"$filename"* ]] || [[ "$filename" == *"$candidate_name"* ]]; then
  189. echo "$candidate_rel"
  190. return 0
  191. fi
  192. done < <(find "$search_dir" -type f -name "*.md" 2>/dev/null)
  193. done
  194. return 1
  195. }
  196. validate_existing_entries() {
  197. if [ "$VALIDATE_EXISTING" = false ]; then
  198. return 0
  199. fi
  200. print_info "Validating existing registry entries..."
  201. echo ""
  202. # Get all component types from registry
  203. local component_types=$(jq -r '.components | keys[]' "$REGISTRY_FILE" 2>/dev/null)
  204. while IFS= read -r comp_type; do
  205. # Get all components of this type
  206. local count=$(jq -r ".components.${comp_type} | length" "$REGISTRY_FILE" 2>/dev/null)
  207. for ((i=0; i<count; i++)); do
  208. local id=$(jq -r ".components.${comp_type}[$i].id" "$REGISTRY_FILE" 2>/dev/null)
  209. local name=$(jq -r ".components.${comp_type}[$i].name" "$REGISTRY_FILE" 2>/dev/null)
  210. local path=$(jq -r ".components.${comp_type}[$i].path" "$REGISTRY_FILE" 2>/dev/null)
  211. # Skip if path is null or empty
  212. if [ -z "$path" ] || [ "$path" = "null" ]; then
  213. continue
  214. fi
  215. local full_path="$REPO_ROOT/$path"
  216. # Check if file exists
  217. if [ ! -f "$full_path" ]; then
  218. print_warning "Component file not found: ${name} (${path})"
  219. # Try to find similar path
  220. local similar_path
  221. if similar_path=$(find_similar_path "$path"); then
  222. print_info "Found similar path: ${similar_path}"
  223. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  224. fix_component_path "$comp_type" "$i" "$id" "$name" "$path" "$similar_path"
  225. else
  226. FIXED_COMPONENTS+=("${comp_type}|${i}|${id}|${name}|${path}|${similar_path}")
  227. echo " Would fix: ${path} → ${similar_path}"
  228. fi
  229. else
  230. # No similar path found, mark for removal
  231. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  232. remove_component_from_registry "$comp_type" "$id" "$name" "$path"
  233. else
  234. REMOVED_COMPONENTS+=("${comp_type}|${id}|${name}|${path}")
  235. echo " Would remove: ${name} (deleted)"
  236. fi
  237. fi
  238. echo ""
  239. fi
  240. done
  241. done <<< "$component_types"
  242. }
  243. fix_component_path() {
  244. local comp_type=$1
  245. local index=$2
  246. local id=$3
  247. local name=$4
  248. local old_path=$5
  249. local new_path=$6
  250. local temp_file="${REGISTRY_FILE}.tmp"
  251. jq --arg type "$comp_type" \
  252. --argjson idx "$index" \
  253. --arg newpath "$new_path" \
  254. ".components[\$type][\$idx].path = \$newpath" \
  255. "$REGISTRY_FILE" > "$temp_file"
  256. if [ $? -eq 0 ]; then
  257. mv "$temp_file" "$REGISTRY_FILE"
  258. print_success "Fixed path for ${name}: ${old_path} → ${new_path}"
  259. TOTAL_FIXED=$((TOTAL_FIXED + 1))
  260. else
  261. print_error "Failed to fix path for ${name}"
  262. rm -f "$temp_file"
  263. return 1
  264. fi
  265. }
  266. remove_component_from_registry() {
  267. local comp_type=$1
  268. local id=$2
  269. local name=$3
  270. local path=$4
  271. local temp_file="${REGISTRY_FILE}.tmp"
  272. jq --arg type "$comp_type" \
  273. --arg id "$id" \
  274. ".components[\$type] = [.components[\$type][] | select(.id != \$id)]" \
  275. "$REGISTRY_FILE" > "$temp_file"
  276. if [ $? -eq 0 ]; then
  277. mv "$temp_file" "$REGISTRY_FILE"
  278. print_success "Removed deleted component: ${name}"
  279. TOTAL_REMOVED=$((TOTAL_REMOVED + 1))
  280. else
  281. print_error "Failed to remove component: ${name}"
  282. rm -f "$temp_file"
  283. return 1
  284. fi
  285. }
  286. #############################################################################
  287. # Component Detection
  288. #############################################################################
  289. extract_metadata_from_file() {
  290. local file=$1
  291. local id=""
  292. local name=""
  293. local description=""
  294. local tags=""
  295. local dependencies=""
  296. # Try to extract from frontmatter (YAML)
  297. if grep -q "^---$" "$file" 2>/dev/null; then
  298. # Extract description from frontmatter
  299. description=$(sed -n '/^---$/,/^---$/p' "$file" | grep "^description:" | sed 's/^description: *//; s/^"//; s/"$//' | head -1)
  300. # Extract tags from frontmatter (YAML array format)
  301. # Handles both: tags: [tag1, tag2] and multi-line format
  302. local in_tags=false
  303. local frontmatter=$(sed -n '/^---$/,/^---$/p' "$file")
  304. while IFS= read -r line; do
  305. if [[ "$line" =~ ^tags: ]]; then
  306. # Inline array format: tags: [tag1, tag2]
  307. if [[ "$line" =~ \[.*\] ]]; then
  308. tags=$(echo "$line" | sed 's/^tags: *\[//; s/\].*//; s/ //g')
  309. else
  310. in_tags=true
  311. fi
  312. elif [[ "$in_tags" == true ]]; then
  313. # Multi-line array format
  314. if [[ "$line" =~ ^---$ ]]; then
  315. # End of frontmatter
  316. in_tags=false
  317. elif [[ "$line" =~ ^[[:space:]]*- ]]; then
  318. local tag=$(echo "$line" | sed 's/^[[:space:]]*- *//')
  319. if [ -z "$tags" ]; then
  320. tags="$tag"
  321. else
  322. tags="$tags,$tag"
  323. fi
  324. elif [[ ! "$line" =~ ^[[:space:]] ]]; then
  325. in_tags=false
  326. fi
  327. fi
  328. done <<< "$frontmatter"
  329. # Extract dependencies from frontmatter (similar to tags)
  330. # Handles: dependencies: [dep1, dep2] or multi-line format
  331. local in_deps=false
  332. while IFS= read -r line; do
  333. if [[ "$line" =~ ^dependencies: ]]; then
  334. # Inline array format
  335. if [[ "$line" =~ \[.*\] ]]; then
  336. dependencies=$(echo "$line" | sed 's/^dependencies: *\[//; s/\].*//; s/ //g; s/"//g; s/'"'"'//g')
  337. else
  338. in_deps=true
  339. fi
  340. elif [[ "$in_deps" == true ]]; then
  341. # Multi-line array format
  342. if [[ "$line" =~ ^---$ ]]; then
  343. # End of frontmatter
  344. in_deps=false
  345. elif [[ "$line" =~ ^[[:space:]]*- ]]; then
  346. # Extract dependency (remove leading dash and whitespace)
  347. local dep=$(echo "$line" | sed 's/^[[:space:]]*- *//' | sed 's/"//g; s/'"'"'//g' | sed 's/#.*//' | xargs)
  348. # Skip empty lines and comments
  349. if [ -n "$dep" ] && [[ ! "$dep" =~ ^# ]]; then
  350. if [ -z "$dependencies" ]; then
  351. dependencies="$dep"
  352. else
  353. dependencies="$dependencies,$dep"
  354. fi
  355. fi
  356. elif [[ "$line" =~ ^[[:space:]]*# ]] || [[ -z "$line" ]]; then
  357. # Skip comments and empty lines within dependencies
  358. continue
  359. elif [[ ! "$line" =~ ^[[:space:]] ]]; then
  360. # Non-indented line means we've moved to a new field
  361. in_deps=false
  362. fi
  363. fi
  364. done <<< "$frontmatter"
  365. fi
  366. # If no description in frontmatter, try to get from first heading or paragraph
  367. if [ -z "$description" ]; then
  368. description=$(grep -m 1 "^# " "$file" | sed 's/^# //' || echo "")
  369. fi
  370. # Generate ID from filename
  371. local filename=$(basename "$file" .md)
  372. id=$(echo "$filename" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
  373. # Generate name from filename (capitalize words)
  374. name=$(echo "$filename" | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1')
  375. echo "${id}|${name}|${description}|${tags}|${dependencies}"
  376. }
  377. detect_component_type() {
  378. local path=$1
  379. # Handle category-based paths (e.g., .opencode/agent/core/openagent.md)
  380. # and flat paths (e.g., .opencode/agent/openagent.md)
  381. if [[ "$path" == *"/agent/subagents/"* ]]; then
  382. echo "subagent"
  383. elif [[ "$path" == *"/agent/"* ]]; then
  384. echo "agent"
  385. elif [[ "$path" == *"/command/"* ]]; then
  386. echo "command"
  387. elif [[ "$path" == *"/tool/"* ]]; then
  388. echo "tool"
  389. elif [[ "$path" == *"/plugin/"* ]]; then
  390. echo "plugin"
  391. elif [[ "$path" == *"/context/"* ]]; then
  392. echo "context"
  393. else
  394. echo "unknown"
  395. fi
  396. }
  397. extract_category_from_path() {
  398. local path=$1
  399. # Extract category from path like .opencode/agent/core/openagent.md → core
  400. # or .opencode/agent/subagents/code/tester.md → code (for subagents)
  401. if [[ "$path" == *"/agent/subagents/"* ]]; then
  402. # For subagents: .opencode/agent/subagents/code/tester.md → code
  403. echo "$path" | sed -E 's|.*/agent/subagents/([^/]+)/.*|\1|'
  404. elif [[ "$path" == *"/agent/"* ]]; then
  405. # For agents: .opencode/agent/core/openagent.md → core
  406. # Check if there's a category subdirectory
  407. local category=$(echo "$path" | sed -E 's|.*/agent/([^/]+)/.*|\1|')
  408. # If category is the filename, it's a flat structure (no category)
  409. if [[ "$category" == *.md ]]; then
  410. echo "standard"
  411. else
  412. echo "$category"
  413. fi
  414. else
  415. echo "standard"
  416. fi
  417. }
  418. get_registry_key() {
  419. local type=$1
  420. case "$type" in
  421. config) echo "config" ;;
  422. *) echo "${type}s" ;;
  423. esac
  424. }
  425. scan_for_new_components() {
  426. print_info "Scanning for new components..."
  427. echo ""
  428. # Get all paths from registry
  429. local registry_paths=$(jq -r '.components | to_entries[] | .value[] | .path' "$REGISTRY_FILE" 2>/dev/null | sort -u)
  430. # Scan .opencode directory
  431. local categories=("agent" "command" "tool" "plugin" "context")
  432. for category in "${categories[@]}"; do
  433. local category_dir="$REPO_ROOT/.opencode/$category"
  434. if [ ! -d "$category_dir" ]; then
  435. continue
  436. fi
  437. # Find all .md files recursively (excluding node_modules, tests, docs, templates)
  438. while IFS= read -r file; do
  439. local rel_path="${file#$REPO_ROOT/}"
  440. # Skip symlinks (backward compatibility links)
  441. if [ -L "$file" ]; then
  442. continue
  443. fi
  444. # Skip node_modules, tests, docs, templates
  445. if [[ "$rel_path" == *"/node_modules/"* ]] || \
  446. [[ "$rel_path" == *"/tests/"* ]] || \
  447. [[ "$rel_path" == *"/docs/"* ]] || \
  448. [[ "$rel_path" == *"/template"* ]] || \
  449. [[ "$rel_path" == *"README.md" ]] || \
  450. [[ "$rel_path" == *"index.md" ]]; then
  451. continue
  452. fi
  453. # Check if this path is in registry
  454. if ! echo "$registry_paths" | grep -q "^${rel_path}$"; then
  455. # Extract metadata
  456. local metadata=$(extract_metadata_from_file "$file")
  457. IFS='|' read -r id name description tags dependencies <<< "$metadata"
  458. # Detect component type
  459. local comp_type=$(detect_component_type "$rel_path")
  460. # Extract category from path
  461. local comp_category=$(extract_category_from_path "$rel_path")
  462. if [ "$comp_type" != "unknown" ]; then
  463. NEW_COMPONENTS+=("${comp_type}|${id}|${name}|${description}|${rel_path}|${comp_category}|${tags}|${dependencies}")
  464. print_warning "New ${comp_type}: ${name} (${id})"
  465. echo " Path: ${rel_path}"
  466. echo " Category: ${comp_category}"
  467. [ -n "$description" ] && echo " Description: ${description}"
  468. [ -n "$tags" ] && echo " Tags: ${tags}"
  469. [ -n "$dependencies" ] && echo " Dependencies: ${dependencies}"
  470. # Check dependencies
  471. if [ -n "$dependencies" ]; then
  472. check_dependencies "$dependencies" "$name"
  473. fi
  474. echo ""
  475. fi
  476. fi
  477. done < <(find "$category_dir" -type f -name "*.md" 2>/dev/null)
  478. done
  479. }
  480. check_dependencies() {
  481. local deps_str=$1
  482. local component_name=$2
  483. if [ -z "$deps_str" ]; then
  484. return 0
  485. fi
  486. # Split dependencies by comma
  487. IFS=',' read -ra deps <<< "$deps_str"
  488. for dep in "${deps[@]}"; do
  489. dep=$(echo "$dep" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  490. if [ -z "$dep" ]; then
  491. continue
  492. fi
  493. # Parse dependency: type:id
  494. if [[ ! "$dep" =~ ^([^:]+):(.+)$ ]]; then
  495. echo " ⚠ Invalid dependency format: ${dep} (expected type:id)"
  496. continue
  497. fi
  498. local dep_type="${BASH_REMATCH[1]}"
  499. local dep_id="${BASH_REMATCH[2]}"
  500. # Map to registry category
  501. local category=""
  502. case "$dep_type" in
  503. agent) category="agents" ;;
  504. subagent) category="subagents" ;;
  505. command) category="commands" ;;
  506. tool) category="tools" ;;
  507. plugin) category="plugins" ;;
  508. context) category="contexts" ;;
  509. config) category="config" ;;
  510. *)
  511. echo " ⚠ Unknown dependency type: ${dep}"
  512. continue
  513. ;;
  514. esac
  515. # Check if exists in registry
  516. local exists=$(jq -r ".components.${category}[]? | select(.id == \"${dep_id}\") | .id" "$REGISTRY_FILE" 2>/dev/null)
  517. if [ -z "$exists" ]; then
  518. echo " ⚠ Dependency not found in registry: ${dep}"
  519. fi
  520. done
  521. }
  522. add_component_to_registry() {
  523. local comp_type=$1
  524. local id=$2
  525. local name=$3
  526. local description=$4
  527. local path=$5
  528. local comp_category=${6:-"standard"}
  529. local tags_str=${7:-""}
  530. local deps_str=${8:-""}
  531. # Default description if empty
  532. if [ -z "$description" ]; then
  533. description="Component: ${name}"
  534. fi
  535. # Escape quotes and special characters in description
  536. description=$(echo "$description" | sed 's/"/\\"/g' | sed "s/'/\\'/g")
  537. # Convert comma-separated strings to JSON arrays
  538. local tags_json="[]"
  539. if [ -n "$tags_str" ]; then
  540. tags_json=$(echo "$tags_str" | awk -F',' '{printf "["; for(i=1;i<=NF;i++) {gsub(/^[ \t]+|[ \t]+$/, "", $i); printf "\"%s\"", $i; if(i<NF) printf ","}; printf "]"}')
  541. fi
  542. local deps_json="[]"
  543. if [ -n "$deps_str" ]; then
  544. deps_json=$(echo "$deps_str" | awk -F',' '{printf "["; for(i=1;i<=NF;i++) {gsub(/^[ \t]+|[ \t]+$/, "", $i); printf "\"%s\"", $i; if(i<NF) printf ","}; printf "]"}')
  545. fi
  546. # Get registry key (agents, subagents, commands, etc.)
  547. local registry_key=$(get_registry_key "$comp_type")
  548. # Use jq to properly construct JSON (avoids escaping issues)
  549. local temp_file="${REGISTRY_FILE}.tmp"
  550. jq --arg id "$id" \
  551. --arg name "$name" \
  552. --arg type "$comp_type" \
  553. --arg path "$path" \
  554. --arg desc "$description" \
  555. --arg cat "$comp_category" \
  556. --argjson tags "$tags_json" \
  557. --argjson deps "$deps_json" \
  558. ".components.${registry_key} += [{
  559. \"id\": \$id,
  560. \"name\": \$name,
  561. \"type\": \$type,
  562. \"path\": \$path,
  563. \"description\": \$desc,
  564. \"tags\": \$tags,
  565. \"dependencies\": \$deps,
  566. \"category\": \$cat
  567. }]" "$REGISTRY_FILE" > "$temp_file"
  568. if [ $? -eq 0 ]; then
  569. mv "$temp_file" "$REGISTRY_FILE"
  570. print_success "Added ${comp_type}: ${name} (category: ${comp_category})"
  571. else
  572. print_error "Failed to add ${comp_type}: ${name}"
  573. rm -f "$temp_file"
  574. return 1
  575. fi
  576. }
  577. #############################################################################
  578. # Main
  579. #############################################################################
  580. main() {
  581. # Parse arguments
  582. while [ $# -gt 0 ]; do
  583. case "$1" in
  584. -a|--auto-add)
  585. AUTO_ADD=true
  586. shift
  587. ;;
  588. -d|--dry-run)
  589. DRY_RUN=true
  590. shift
  591. ;;
  592. -s|--skip-validation)
  593. VALIDATE_EXISTING=false
  594. shift
  595. ;;
  596. -n|--no-security)
  597. SECURITY_CHECK=false
  598. shift
  599. ;;
  600. -h|--help)
  601. usage
  602. ;;
  603. *)
  604. echo "Unknown option: $1"
  605. usage
  606. ;;
  607. esac
  608. done
  609. print_header
  610. # Check dependencies
  611. if ! command -v jq &> /dev/null; then
  612. print_error "jq is required but not installed"
  613. exit 1
  614. fi
  615. # Validate registry file
  616. if [ ! -f "$REGISTRY_FILE" ]; then
  617. print_error "Registry file not found: $REGISTRY_FILE"
  618. exit 1
  619. fi
  620. if ! jq empty "$REGISTRY_FILE" 2>/dev/null; then
  621. print_error "Registry file is not valid JSON"
  622. exit 1
  623. fi
  624. # Run security checks
  625. run_security_checks
  626. # Validate existing entries (fixes and removals)
  627. validate_existing_entries
  628. # Scan for new components
  629. scan_for_new_components
  630. # Summary
  631. echo ""
  632. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  633. echo -e "${BOLD}Summary${NC}"
  634. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  635. echo ""
  636. # Display counts
  637. echo -e "Security Issues: ${MAGENTA}${TOTAL_SECURITY_ISSUES}${NC}"
  638. echo -e "Fixed Paths: ${GREEN}${TOTAL_FIXED}${NC}"
  639. echo -e "Removed Components: ${RED}${TOTAL_REMOVED}${NC}"
  640. echo -e "New Components: ${YELLOW}${#NEW_COMPONENTS[@]}${NC}"
  641. echo ""
  642. # Show pending fixes if in dry-run mode
  643. if [ ${#FIXED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  644. echo -e "${BOLD}Pending Path Fixes:${NC}"
  645. for entry in "${FIXED_COMPONENTS[@]}"; do
  646. IFS='|' read -r comp_type index id name old_path new_path <<< "$entry"
  647. echo " • ${name}: ${old_path} → ${new_path}"
  648. done
  649. echo ""
  650. fi
  651. # Show pending removals if in dry-run mode
  652. if [ ${#REMOVED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  653. echo -e "${BOLD}Pending Removals:${NC}"
  654. for entry in "${REMOVED_COMPONENTS[@]}"; do
  655. IFS='|' read -r comp_type id name path <<< "$entry"
  656. echo " • ${name} (${path})"
  657. done
  658. echo ""
  659. fi
  660. # Check if everything is up to date
  661. if [ ${#NEW_COMPONENTS[@]} -eq 0 ] && \
  662. [ ${#FIXED_COMPONENTS[@]} -eq 0 ] && \
  663. [ ${#REMOVED_COMPONENTS[@]} -eq 0 ] && \
  664. [ $TOTAL_FIXED -eq 0 ] && \
  665. [ $TOTAL_REMOVED -eq 0 ]; then
  666. print_success "Registry is up to date!"
  667. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  668. echo ""
  669. print_warning "Please review and fix the ${TOTAL_SECURITY_ISSUES} security issue(s) found"
  670. fi
  671. exit 0
  672. fi
  673. # Add components if auto-add is enabled
  674. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  675. if [ ${#NEW_COMPONENTS[@]} -gt 0 ]; then
  676. print_info "Adding new components to registry..."
  677. echo ""
  678. local added=0
  679. for entry in "${NEW_COMPONENTS[@]}"; do
  680. IFS='|' read -r comp_type id name description path comp_category tags dependencies <<< "$entry"
  681. if add_component_to_registry "$comp_type" "$id" "$name" "$description" "$path" "$comp_category" "$tags" "$dependencies"; then
  682. added=$((added + 1))
  683. fi
  684. done
  685. echo ""
  686. print_success "Added ${added} component(s) to registry"
  687. fi
  688. # Update timestamp
  689. jq '.metadata.lastUpdated = (now | strftime("%Y-%m-%d"))' "$REGISTRY_FILE" > "${REGISTRY_FILE}.tmp"
  690. mv "${REGISTRY_FILE}.tmp" "$REGISTRY_FILE"
  691. elif [ "$DRY_RUN" = true ]; then
  692. print_info "Dry run mode - no changes made to registry"
  693. echo ""
  694. echo "Run without --dry-run to apply these changes"
  695. else
  696. print_info "Run with --auto-add to apply these changes to registry"
  697. echo ""
  698. echo "Or manually update registry.json"
  699. fi
  700. # Final security warning
  701. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  702. echo ""
  703. print_warning "⚠️ ${TOTAL_SECURITY_ISSUES} security issue(s) require attention"
  704. fi
  705. exit 0
  706. }
  707. main "$@"