auto-detect-components.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. # Try to extract from frontmatter (YAML)
  295. if grep -q "^---$" "$file" 2>/dev/null; then
  296. # Extract description from frontmatter
  297. description=$(sed -n '/^---$/,/^---$/p' "$file" | grep "^description:" | sed 's/^description: *//; s/^"//; s/"$//' | head -1)
  298. fi
  299. # If no description in frontmatter, try to get from first heading or paragraph
  300. if [ -z "$description" ]; then
  301. description=$(grep -m 1 "^# " "$file" | sed 's/^# //' || echo "")
  302. fi
  303. # Generate ID from filename
  304. local filename=$(basename "$file" .md)
  305. id=$(echo "$filename" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
  306. # Generate name from filename (capitalize words)
  307. name=$(echo "$filename" | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1')
  308. echo "${id}|${name}|${description}"
  309. }
  310. detect_component_type() {
  311. local path=$1
  312. # Handle category-based paths (e.g., .opencode/agent/core/openagent.md)
  313. # and flat paths (e.g., .opencode/agent/openagent.md)
  314. if [[ "$path" == *"/agent/subagents/"* ]]; then
  315. echo "subagent"
  316. elif [[ "$path" == *"/agent/"* ]]; then
  317. echo "agent"
  318. elif [[ "$path" == *"/command/"* ]]; then
  319. echo "command"
  320. elif [[ "$path" == *"/tool/"* ]]; then
  321. echo "tool"
  322. elif [[ "$path" == *"/plugin/"* ]]; then
  323. echo "plugin"
  324. elif [[ "$path" == *"/context/"* ]]; then
  325. echo "context"
  326. else
  327. echo "unknown"
  328. fi
  329. }
  330. extract_category_from_path() {
  331. local path=$1
  332. # Extract category from path like .opencode/agent/core/openagent.md → core
  333. # or .opencode/agent/subagents/code/tester.md → code (for subagents)
  334. if [[ "$path" == *"/agent/subagents/"* ]]; then
  335. # For subagents: .opencode/agent/subagents/code/tester.md → code
  336. echo "$path" | sed -E 's|.*/agent/subagents/([^/]+)/.*|\1|'
  337. elif [[ "$path" == *"/agent/"* ]]; then
  338. # For agents: .opencode/agent/core/openagent.md → core
  339. # Check if there's a category subdirectory
  340. local category=$(echo "$path" | sed -E 's|.*/agent/([^/]+)/.*|\1|')
  341. # If category is the filename, it's a flat structure (no category)
  342. if [[ "$category" == *.md ]]; then
  343. echo "standard"
  344. else
  345. echo "$category"
  346. fi
  347. else
  348. echo "standard"
  349. fi
  350. }
  351. get_registry_key() {
  352. local type=$1
  353. case "$type" in
  354. config) echo "config" ;;
  355. *) echo "${type}s" ;;
  356. esac
  357. }
  358. scan_for_new_components() {
  359. print_info "Scanning for new components..."
  360. echo ""
  361. # Get all paths from registry
  362. local registry_paths=$(jq -r '.components | to_entries[] | .value[] | .path' "$REGISTRY_FILE" 2>/dev/null | sort -u)
  363. # Scan .opencode directory
  364. local categories=("agent" "command" "tool" "plugin" "context")
  365. for category in "${categories[@]}"; do
  366. local category_dir="$REPO_ROOT/.opencode/$category"
  367. if [ ! -d "$category_dir" ]; then
  368. continue
  369. fi
  370. # Find all .md files recursively (excluding node_modules, tests, docs, templates)
  371. while IFS= read -r file; do
  372. local rel_path="${file#$REPO_ROOT/}"
  373. # Skip symlinks (backward compatibility links)
  374. if [ -L "$file" ]; then
  375. continue
  376. fi
  377. # Skip node_modules, tests, docs, templates
  378. if [[ "$rel_path" == *"/node_modules/"* ]] || \
  379. [[ "$rel_path" == *"/tests/"* ]] || \
  380. [[ "$rel_path" == *"/docs/"* ]] || \
  381. [[ "$rel_path" == *"/template"* ]] || \
  382. [[ "$rel_path" == *"README.md" ]] || \
  383. [[ "$rel_path" == *"index.md" ]]; then
  384. continue
  385. fi
  386. # Check if this path is in registry
  387. if ! echo "$registry_paths" | grep -q "^${rel_path}$"; then
  388. # Extract metadata
  389. local metadata=$(extract_metadata_from_file "$file")
  390. IFS='|' read -r id name description <<< "$metadata"
  391. # Detect component type
  392. local comp_type=$(detect_component_type "$rel_path")
  393. # Extract category from path
  394. local comp_category=$(extract_category_from_path "$rel_path")
  395. if [ "$comp_type" != "unknown" ]; then
  396. NEW_COMPONENTS+=("${comp_type}|${id}|${name}|${description}|${rel_path}|${comp_category}")
  397. print_warning "New ${comp_type}: ${name} (${id})"
  398. echo " Path: ${rel_path}"
  399. echo " Category: ${comp_category}"
  400. [ -n "$description" ] && echo " Description: ${description}"
  401. echo ""
  402. fi
  403. fi
  404. done < <(find "$category_dir" -type f -name "*.md" 2>/dev/null)
  405. done
  406. }
  407. add_component_to_registry() {
  408. local comp_type=$1
  409. local id=$2
  410. local name=$3
  411. local description=$4
  412. local path=$5
  413. local comp_category=${6:-"standard"}
  414. # Default description if empty
  415. if [ -z "$description" ]; then
  416. description="Component: ${name}"
  417. fi
  418. # Escape quotes and special characters in description
  419. description=$(echo "$description" | sed 's/"/\\"/g' | sed "s/'/\\'/g")
  420. # Get registry key (agents, subagents, commands, etc.)
  421. local registry_key=$(get_registry_key "$comp_type")
  422. # Use jq to properly construct JSON (avoids escaping issues)
  423. local temp_file="${REGISTRY_FILE}.tmp"
  424. jq --arg id "$id" \
  425. --arg name "$name" \
  426. --arg type "$comp_type" \
  427. --arg path "$path" \
  428. --arg desc "$description" \
  429. --arg cat "$comp_category" \
  430. ".components.${registry_key} += [{
  431. \"id\": \$id,
  432. \"name\": \$name,
  433. \"type\": \$type,
  434. \"path\": \$path,
  435. \"description\": \$desc,
  436. \"tags\": [],
  437. \"dependencies\": [],
  438. \"category\": \$cat
  439. }]" "$REGISTRY_FILE" > "$temp_file"
  440. if [ $? -eq 0 ]; then
  441. mv "$temp_file" "$REGISTRY_FILE"
  442. print_success "Added ${comp_type}: ${name} (category: ${comp_category})"
  443. else
  444. print_error "Failed to add ${comp_type}: ${name}"
  445. rm -f "$temp_file"
  446. return 1
  447. fi
  448. }
  449. #############################################################################
  450. # Main
  451. #############################################################################
  452. main() {
  453. # Parse arguments
  454. while [ $# -gt 0 ]; do
  455. case "$1" in
  456. -a|--auto-add)
  457. AUTO_ADD=true
  458. shift
  459. ;;
  460. -d|--dry-run)
  461. DRY_RUN=true
  462. shift
  463. ;;
  464. -s|--skip-validation)
  465. VALIDATE_EXISTING=false
  466. shift
  467. ;;
  468. -n|--no-security)
  469. SECURITY_CHECK=false
  470. shift
  471. ;;
  472. -h|--help)
  473. usage
  474. ;;
  475. *)
  476. echo "Unknown option: $1"
  477. usage
  478. ;;
  479. esac
  480. done
  481. print_header
  482. # Check dependencies
  483. if ! command -v jq &> /dev/null; then
  484. print_error "jq is required but not installed"
  485. exit 1
  486. fi
  487. # Validate registry file
  488. if [ ! -f "$REGISTRY_FILE" ]; then
  489. print_error "Registry file not found: $REGISTRY_FILE"
  490. exit 1
  491. fi
  492. if ! jq empty "$REGISTRY_FILE" 2>/dev/null; then
  493. print_error "Registry file is not valid JSON"
  494. exit 1
  495. fi
  496. # Run security checks
  497. run_security_checks
  498. # Validate existing entries (fixes and removals)
  499. validate_existing_entries
  500. # Scan for new components
  501. scan_for_new_components
  502. # Summary
  503. echo ""
  504. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  505. echo -e "${BOLD}Summary${NC}"
  506. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  507. echo ""
  508. # Display counts
  509. echo -e "Security Issues: ${MAGENTA}${TOTAL_SECURITY_ISSUES}${NC}"
  510. echo -e "Fixed Paths: ${GREEN}${TOTAL_FIXED}${NC}"
  511. echo -e "Removed Components: ${RED}${TOTAL_REMOVED}${NC}"
  512. echo -e "New Components: ${YELLOW}${#NEW_COMPONENTS[@]}${NC}"
  513. echo ""
  514. # Show pending fixes if in dry-run mode
  515. if [ ${#FIXED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  516. echo -e "${BOLD}Pending Path Fixes:${NC}"
  517. for entry in "${FIXED_COMPONENTS[@]}"; do
  518. IFS='|' read -r comp_type index id name old_path new_path <<< "$entry"
  519. echo " • ${name}: ${old_path} → ${new_path}"
  520. done
  521. echo ""
  522. fi
  523. # Show pending removals if in dry-run mode
  524. if [ ${#REMOVED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  525. echo -e "${BOLD}Pending Removals:${NC}"
  526. for entry in "${REMOVED_COMPONENTS[@]}"; do
  527. IFS='|' read -r comp_type id name path <<< "$entry"
  528. echo " • ${name} (${path})"
  529. done
  530. echo ""
  531. fi
  532. # Check if everything is up to date
  533. if [ ${#NEW_COMPONENTS[@]} -eq 0 ] && \
  534. [ ${#FIXED_COMPONENTS[@]} -eq 0 ] && \
  535. [ ${#REMOVED_COMPONENTS[@]} -eq 0 ] && \
  536. [ $TOTAL_FIXED -eq 0 ] && \
  537. [ $TOTAL_REMOVED -eq 0 ]; then
  538. print_success "Registry is up to date!"
  539. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  540. echo ""
  541. print_warning "Please review and fix the ${TOTAL_SECURITY_ISSUES} security issue(s) found"
  542. fi
  543. exit 0
  544. fi
  545. # Add components if auto-add is enabled
  546. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  547. if [ ${#NEW_COMPONENTS[@]} -gt 0 ]; then
  548. print_info "Adding new components to registry..."
  549. echo ""
  550. local added=0
  551. for entry in "${NEW_COMPONENTS[@]}"; do
  552. IFS='|' read -r comp_type id name description path comp_category <<< "$entry"
  553. if add_component_to_registry "$comp_type" "$id" "$name" "$description" "$path" "$comp_category"; then
  554. added=$((added + 1))
  555. fi
  556. done
  557. echo ""
  558. print_success "Added ${added} component(s) to registry"
  559. fi
  560. # Update timestamp
  561. jq '.metadata.lastUpdated = (now | strftime("%Y-%m-%d"))' "$REGISTRY_FILE" > "${REGISTRY_FILE}.tmp"
  562. mv "${REGISTRY_FILE}.tmp" "$REGISTRY_FILE"
  563. elif [ "$DRY_RUN" = true ]; then
  564. print_info "Dry run mode - no changes made to registry"
  565. echo ""
  566. echo "Run without --dry-run to apply these changes"
  567. else
  568. print_info "Run with --auto-add to apply these changes to registry"
  569. echo ""
  570. echo "Or manually update registry.json"
  571. fi
  572. # Final security warning
  573. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  574. echo ""
  575. print_warning "⚠️ ${TOTAL_SECURITY_ISSUES} security issue(s) require attention"
  576. fi
  577. exit 0
  578. }
  579. main "$@"