auto-detect-components.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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
  161. dir=$(dirname "$wrong_path")
  162. local filename
  163. filename=$(basename "$wrong_path")
  164. # First, try to find exact filename match in category subdirectories
  165. # e.g., .opencode/agent/opencoder.md → .opencode/agent/core/opencoder.md
  166. local base_dir
  167. base_dir=$(echo "$dir" | cut -d'/' -f1-2) # e.g., .opencode/agent
  168. if [ -d "$REPO_ROOT/$base_dir" ]; then
  169. # Search recursively in the base directory for exact filename match
  170. while IFS= read -r candidate; do
  171. local candidate_rel
  172. candidate_rel="${candidate#$REPO_ROOT/}"
  173. local candidate_name
  174. candidate_name=$(basename "$candidate")
  175. # Exact filename match
  176. if [[ "$candidate_name" == "$filename" ]]; then
  177. echo "$candidate_rel"
  178. return 0
  179. fi
  180. done < <(find "$REPO_ROOT/$base_dir" -type f -name "$filename" 2>/dev/null)
  181. fi
  182. # Fallback: search for similar names in the entire .opencode directory
  183. local search_dirs=("$REPO_ROOT/$dir" "$REPO_ROOT/.opencode")
  184. for search_dir in "${search_dirs[@]}"; do
  185. if [ ! -d "$search_dir" ]; then
  186. continue
  187. fi
  188. # Find files with similar names
  189. while IFS= read -r candidate; do
  190. local candidate_rel
  191. candidate_rel="${candidate#$REPO_ROOT/}"
  192. local candidate_name
  193. candidate_name=$(basename "$candidate")
  194. # Simple similarity check
  195. if [[ "$candidate_name" == *"$filename"* ]] || [[ "$filename" == *"$candidate_name"* ]]; then
  196. echo "$candidate_rel"
  197. return 0
  198. fi
  199. done < <(find "$search_dir" -type f -name "*.md" 2>/dev/null)
  200. done
  201. return 1
  202. }
  203. validate_existing_entries() {
  204. if [ "$VALIDATE_EXISTING" = false ]; then
  205. return 0
  206. fi
  207. print_info "Validating existing registry entries..."
  208. echo ""
  209. # Get all component types from registry
  210. local component_types
  211. component_types=$(jq -r '.components | keys[]' "$REGISTRY_FILE" 2>/dev/null)
  212. while IFS= read -r comp_type; do
  213. # Get all components of this type
  214. local count
  215. count=$(jq -r ".components.${comp_type} | length" "$REGISTRY_FILE" 2>/dev/null)
  216. for ((i=0; i<count; i++)); do
  217. local id
  218. id=$(jq -r ".components.${comp_type}[$i].id" "$REGISTRY_FILE" 2>/dev/null)
  219. local name
  220. name=$(jq -r ".components.${comp_type}[$i].name" "$REGISTRY_FILE" 2>/dev/null)
  221. local path
  222. path=$(jq -r ".components.${comp_type}[$i].path" "$REGISTRY_FILE" 2>/dev/null)
  223. # Skip if path is null or empty
  224. if [ -z "$path" ] || [ "$path" = "null" ]; then
  225. continue
  226. fi
  227. local full_path="$REPO_ROOT/$path"
  228. # Check if file exists
  229. if [ ! -f "$full_path" ]; then
  230. print_warning "Component file not found: ${name} (${path})"
  231. # Try to find similar path
  232. local similar_path
  233. if similar_path=$(find_similar_path "$path"); then
  234. print_info "Found similar path: ${similar_path}"
  235. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  236. fix_component_path "$comp_type" "$i" "$id" "$name" "$path" "$similar_path"
  237. else
  238. FIXED_COMPONENTS+=("${comp_type}|${i}|${id}|${name}|${path}|${similar_path}")
  239. echo " Would fix: ${path} → ${similar_path}"
  240. fi
  241. else
  242. # No similar path found, mark for removal
  243. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  244. remove_component_from_registry "$comp_type" "$id" "$name" "$path"
  245. else
  246. REMOVED_COMPONENTS+=("${comp_type}|${id}|${name}|${path}")
  247. echo " Would remove: ${name} (deleted)"
  248. fi
  249. fi
  250. echo ""
  251. fi
  252. done
  253. done <<< "$component_types"
  254. }
  255. fix_component_path() {
  256. local comp_type=$1
  257. local index=$2
  258. local id=$3
  259. local name=$4
  260. local old_path=$5
  261. local new_path=$6
  262. local temp_file="${REGISTRY_FILE}.tmp"
  263. jq --arg type "$comp_type" \
  264. --argjson idx "$index" \
  265. --arg newpath "$new_path" \
  266. ".components[\$type][\$idx].path = \$newpath" \
  267. "$REGISTRY_FILE" > "$temp_file"
  268. if [ $? -eq 0 ]; then
  269. mv "$temp_file" "$REGISTRY_FILE"
  270. print_success "Fixed path for ${name}: ${old_path} → ${new_path}"
  271. TOTAL_FIXED=$((TOTAL_FIXED + 1))
  272. else
  273. print_error "Failed to fix path for ${name}"
  274. rm -f "$temp_file"
  275. return 1
  276. fi
  277. }
  278. remove_component_from_registry() {
  279. local comp_type=$1
  280. local id=$2
  281. local name=$3
  282. local path=$4
  283. local temp_file="${REGISTRY_FILE}.tmp"
  284. jq --arg type "$comp_type" \
  285. --arg id "$id" \
  286. ".components[\$type] = [.components[\$type][] | select(.id != \$id)]" \
  287. "$REGISTRY_FILE" > "$temp_file"
  288. if [ $? -eq 0 ]; then
  289. mv "$temp_file" "$REGISTRY_FILE"
  290. print_success "Removed deleted component: ${name}"
  291. TOTAL_REMOVED=$((TOTAL_REMOVED + 1))
  292. else
  293. print_error "Failed to remove component: ${name}"
  294. rm -f "$temp_file"
  295. return 1
  296. fi
  297. }
  298. #############################################################################
  299. # Component Detection
  300. #############################################################################
  301. extract_metadata_from_file() {
  302. local file=$1
  303. local id=""
  304. local name=""
  305. local description=""
  306. local tags=""
  307. local dependencies=""
  308. # Try to extract from frontmatter (YAML)
  309. if grep -q "^---$" "$file" 2>/dev/null; then
  310. # Extract description from frontmatter
  311. description=$(sed -n '/^---$/,/^---$/p' "$file" | grep "^description:" | sed 's/^description: *//; s/^"//; s/"$//' | head -1)
  312. # Extract tags from frontmatter (YAML array format)
  313. # Handles both: tags: [tag1, tag2] and multi-line format
  314. local in_tags=false
  315. local frontmatter
  316. frontmatter=$(sed -n '/^---$/,/^---$/p' "$file")
  317. while IFS= read -r line; do
  318. if [[ "$line" =~ ^tags: ]]; then
  319. # Inline array format: tags: [tag1, tag2]
  320. if [[ "$line" =~ \[.*\] ]]; then
  321. tags=$(echo "$line" | sed 's/^tags: *\[//; s/\].*//; s/ //g')
  322. else
  323. in_tags=true
  324. fi
  325. elif [[ "$in_tags" == true ]]; then
  326. # Multi-line array format
  327. if [[ "$line" =~ ^---$ ]]; then
  328. # End of frontmatter
  329. in_tags=false
  330. elif [[ "$line" =~ ^[[:space:]]*- ]]; then
  331. local tag
  332. tag=$(echo "$line" | sed 's/^[[:space:]]*- *//')
  333. if [ -z "$tags" ]; then
  334. tags="$tag"
  335. else
  336. tags="$tags,$tag"
  337. fi
  338. elif [[ ! "$line" =~ ^[[:space:]] ]]; then
  339. in_tags=false
  340. fi
  341. fi
  342. done <<< "$frontmatter"
  343. # Extract dependencies from frontmatter (similar to tags)
  344. # Handles: dependencies: [dep1, dep2] or multi-line format
  345. local in_deps=false
  346. while IFS= read -r line; do
  347. if [[ "$line" =~ ^dependencies: ]]; then
  348. # Inline array format
  349. if [[ "$line" =~ \[.*\] ]]; then
  350. dependencies=$(echo "$line" | sed 's/^dependencies: *\[//; s/\].*//; s/ //g; s/"//g; s/'"'"'//g')
  351. else
  352. in_deps=true
  353. fi
  354. elif [[ "$in_deps" == true ]]; then
  355. # Multi-line array format
  356. if [[ "$line" =~ ^---$ ]]; then
  357. # End of frontmatter
  358. in_deps=false
  359. elif [[ "$line" =~ ^[[:space:]]*- ]]; then
  360. # Extract dependency (remove leading dash and whitespace)
  361. local dep
  362. dep=$(echo "$line" | sed 's/^[[:space:]]*- *//' | sed 's/"//g; s/'"'"'//g' | sed 's/#.*//' | xargs)
  363. # Skip empty lines and comments
  364. if [ -n "$dep" ] && [[ ! "$dep" =~ ^# ]]; then
  365. if [ -z "$dependencies" ]; then
  366. dependencies="$dep"
  367. else
  368. dependencies="$dependencies,$dep"
  369. fi
  370. fi
  371. elif [[ "$line" =~ ^[[:space:]]*# ]] || [[ -z "$line" ]]; then
  372. # Skip comments and empty lines within dependencies
  373. continue
  374. elif [[ ! "$line" =~ ^[[:space:]] ]]; then
  375. # Non-indented line means we've moved to a new field
  376. in_deps=false
  377. fi
  378. fi
  379. done <<< "$frontmatter"
  380. fi
  381. # If no description in frontmatter, try to get from first heading or paragraph
  382. if [ -z "$description" ]; then
  383. description=$(grep -m 1 "^# " "$file" | sed 's/^# //' || echo "")
  384. fi
  385. # Generate ID from filename
  386. local filename
  387. filename=$(basename "$file" .md)
  388. id=$(echo "$filename" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
  389. # Generate name from filename (capitalize words)
  390. name=$(echo "$filename" | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1')
  391. echo "${id}|${name}|${description}|${tags}|${dependencies}"
  392. }
  393. detect_component_type() {
  394. local path=$1
  395. # Handle category-based paths (e.g., .opencode/agent/core/openagent.md)
  396. # and flat paths (e.g., .opencode/agent/openagent.md)
  397. if [[ "$path" == *"/agent/subagents/"* ]]; then
  398. echo "subagent"
  399. elif [[ "$path" == *"/agent/"* ]]; then
  400. echo "agent"
  401. elif [[ "$path" == *"/command/"* ]]; then
  402. echo "command"
  403. elif [[ "$path" == *"/tool/"* ]]; then
  404. echo "tool"
  405. elif [[ "$path" == *"/plugin/"* ]]; then
  406. echo "plugin"
  407. elif [[ "$path" == *"/context/"* ]]; then
  408. echo "context"
  409. else
  410. echo "unknown"
  411. fi
  412. }
  413. extract_category_from_path() {
  414. local path=$1
  415. # Extract category from path like .opencode/agent/core/openagent.md → core
  416. # or .opencode/agent/subagents/code/tester.md → code (for subagents)
  417. if [[ "$path" == *"/agent/subagents/"* ]]; then
  418. # For subagents: .opencode/agent/subagents/code/tester.md → code
  419. echo "$path" | sed -E 's|.*/agent/subagents/([^/]+)/.*|\1|'
  420. elif [[ "$path" == *"/agent/"* ]]; then
  421. # For agents: .opencode/agent/core/openagent.md → core
  422. # Check if there's a category subdirectory
  423. local category
  424. category=$(echo "$path" | sed -E 's|.*/agent/([^/]+)/.*|\1|')
  425. # If category is the filename, it's a flat structure (no category)
  426. if [[ "$category" == *.md ]]; then
  427. echo "standard"
  428. else
  429. echo "$category"
  430. fi
  431. else
  432. echo "standard"
  433. fi
  434. }
  435. get_registry_key() {
  436. local type=$1
  437. case "$type" in
  438. config) echo "config" ;;
  439. *) echo "${type}s" ;;
  440. esac
  441. }
  442. scan_for_new_components() {
  443. print_info "Scanning for new components..."
  444. echo ""
  445. # Get all paths from registry
  446. local registry_paths
  447. registry_paths=$(jq -r '.components | to_entries[] | .value[] | .path' "$REGISTRY_FILE" 2>/dev/null | sort -u)
  448. # Scan .opencode directory
  449. local categories=("agent" "command" "tool" "plugin" "context")
  450. for category in "${categories[@]}"; do
  451. local category_dir="$REPO_ROOT/.opencode/$category"
  452. if [ ! -d "$category_dir" ]; then
  453. continue
  454. fi
  455. # Find all .md files recursively (excluding node_modules, tests, docs, templates)
  456. while IFS= read -r file; do
  457. local rel_path="${file#$REPO_ROOT/}"
  458. # Skip symlinks (backward compatibility links)
  459. if [ -L "$file" ]; then
  460. continue
  461. fi
  462. # Skip node_modules, tests, docs, templates
  463. if [[ "$rel_path" == *"/node_modules/"* ]] || \
  464. [[ "$rel_path" == *"/tests/"* ]] || \
  465. [[ "$rel_path" == *"/docs/"* ]] || \
  466. [[ "$rel_path" == *"/template"* ]] || \
  467. [[ "$rel_path" == *"README.md" ]] || \
  468. [[ "$rel_path" == *"index.md" ]]; then
  469. continue
  470. fi
  471. # Check if this path is in registry
  472. if ! echo "$registry_paths" | grep -q "^${rel_path}$"; then
  473. # Extract metadata
  474. local metadata
  475. metadata=$(extract_metadata_from_file "$file")
  476. IFS='|' read -r id name description tags dependencies <<< "$metadata"
  477. # Detect component type
  478. local comp_type
  479. comp_type=$(detect_component_type "$rel_path")
  480. # Extract category from path
  481. local comp_category
  482. comp_category=$(extract_category_from_path "$rel_path")
  483. if [ "$comp_type" != "unknown" ]; then
  484. NEW_COMPONENTS+=("${comp_type}|${id}|${name}|${description}|${rel_path}|${comp_category}|${tags}|${dependencies}")
  485. print_warning "New ${comp_type}: ${name} (${id})"
  486. echo " Path: ${rel_path}"
  487. echo " Category: ${comp_category}"
  488. [ -n "$description" ] && echo " Description: ${description}"
  489. [ -n "$tags" ] && echo " Tags: ${tags}"
  490. [ -n "$dependencies" ] && echo " Dependencies: ${dependencies}"
  491. # Check dependencies
  492. if [ -n "$dependencies" ]; then
  493. check_dependencies "$dependencies" "$name"
  494. fi
  495. echo ""
  496. fi
  497. fi
  498. done < <(find "$category_dir" -type f -name "*.md" 2>/dev/null)
  499. done
  500. }
  501. check_dependencies() {
  502. local deps_str=$1
  503. # local component_name=$2 # Unused
  504. if [ -z "$deps_str" ]; then
  505. return 0
  506. fi
  507. # Split dependencies by comma
  508. IFS=',' read -ra deps <<< "$deps_str"
  509. for dep in "${deps[@]}"; do
  510. dep=$(echo "$dep" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  511. if [ -z "$dep" ]; then
  512. continue
  513. fi
  514. # Parse dependency: type:id
  515. if [[ ! "$dep" =~ ^([^:]+):(.+)$ ]]; then
  516. echo " ⚠ Invalid dependency format: ${dep} (expected type:id)"
  517. continue
  518. fi
  519. local dep_type="${BASH_REMATCH[1]}"
  520. local dep_id="${BASH_REMATCH[2]}"
  521. # Map to registry category
  522. local category=""
  523. case "$dep_type" in
  524. agent) category="agents" ;;
  525. subagent) category="subagents" ;;
  526. command) category="commands" ;;
  527. tool) category="tools" ;;
  528. plugin) category="plugins" ;;
  529. context) category="contexts" ;;
  530. config) category="config" ;;
  531. *)
  532. echo " ⚠ Unknown dependency type: ${dep}"
  533. continue
  534. ;;
  535. esac
  536. # Check if exists in registry
  537. local exists
  538. exists=$(jq -r ".components.${category}[]? | select(.id == \"${dep_id}\") | .id" "$REGISTRY_FILE" 2>/dev/null)
  539. if [ -z "$exists" ]; then
  540. echo " ⚠ Dependency not found in registry: ${dep}"
  541. fi
  542. done
  543. }
  544. add_component_to_registry() {
  545. local comp_type=$1
  546. local id=$2
  547. local name=$3
  548. local description=$4
  549. local path=$5
  550. local comp_category=${6:-"standard"}
  551. local tags_str=${7:-""}
  552. local deps_str=${8:-""}
  553. # Default description if empty
  554. if [ -z "$description" ]; then
  555. description="Component: ${name}"
  556. fi
  557. # Escape quotes and special characters in description
  558. description=$(echo "$description" | sed 's/"/\\"/g' | sed "s/'/\\'/g")
  559. # Convert comma-separated strings to JSON arrays
  560. local tags_json="[]"
  561. if [ -n "$tags_str" ]; then
  562. 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 "]"}')
  563. fi
  564. local deps_json="[]"
  565. if [ -n "$deps_str" ]; then
  566. 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 "]"}')
  567. fi
  568. # Get registry key (agents, subagents, commands, etc.)
  569. local registry_key
  570. registry_key=$(get_registry_key "$comp_type")
  571. # Use jq to properly construct JSON (avoids escaping issues)
  572. local temp_file="${REGISTRY_FILE}.tmp"
  573. jq --arg id "$id" \
  574. --arg name "$name" \
  575. --arg type "$comp_type" \
  576. --arg path "$path" \
  577. --arg desc "$description" \
  578. --arg cat "$comp_category" \
  579. --argjson tags "$tags_json" \
  580. --argjson deps "$deps_json" \
  581. ".components.${registry_key} += [{
  582. \"id\": \$id,
  583. \"name\": \$name,
  584. \"type\": \$type,
  585. \"path\": \$path,
  586. \"description\": \$desc,
  587. \"tags\": \$tags,
  588. \"dependencies\": \$deps,
  589. \"category\": \$cat
  590. }]" "$REGISTRY_FILE" > "$temp_file"
  591. if [ $? -eq 0 ]; then
  592. mv "$temp_file" "$REGISTRY_FILE"
  593. print_success "Added ${comp_type}: ${name} (category: ${comp_category})"
  594. else
  595. print_error "Failed to add ${comp_type}: ${name}"
  596. rm -f "$temp_file"
  597. return 1
  598. fi
  599. }
  600. #############################################################################
  601. # Main
  602. #############################################################################
  603. main() {
  604. # Parse arguments
  605. while [ $# -gt 0 ]; do
  606. case "$1" in
  607. -a|--auto-add)
  608. AUTO_ADD=true
  609. shift
  610. ;;
  611. -d|--dry-run)
  612. DRY_RUN=true
  613. shift
  614. ;;
  615. -s|--skip-validation)
  616. VALIDATE_EXISTING=false
  617. shift
  618. ;;
  619. -n|--no-security)
  620. SECURITY_CHECK=false
  621. shift
  622. ;;
  623. -h|--help)
  624. usage
  625. ;;
  626. *)
  627. echo "Unknown option: $1"
  628. usage
  629. ;;
  630. esac
  631. done
  632. print_header
  633. # Check dependencies
  634. if ! command -v jq &> /dev/null; then
  635. print_error "jq is required but not installed"
  636. exit 1
  637. fi
  638. # Validate registry file
  639. if [ ! -f "$REGISTRY_FILE" ]; then
  640. print_error "Registry file not found: $REGISTRY_FILE"
  641. exit 1
  642. fi
  643. if ! jq empty "$REGISTRY_FILE" 2>/dev/null; then
  644. print_error "Registry file is not valid JSON"
  645. exit 1
  646. fi
  647. # Run security checks
  648. run_security_checks
  649. # Validate existing entries (fixes and removals)
  650. validate_existing_entries
  651. # Scan for new components
  652. scan_for_new_components
  653. # Summary
  654. echo ""
  655. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  656. echo -e "${BOLD}Summary${NC}"
  657. echo -e "${BOLD}═══════════════════════════════════════════════════════════════${NC}"
  658. echo ""
  659. # Display counts
  660. echo -e "Security Issues: ${MAGENTA}${TOTAL_SECURITY_ISSUES}${NC}"
  661. echo -e "Fixed Paths: ${GREEN}${TOTAL_FIXED}${NC}"
  662. echo -e "Removed Components: ${RED}${TOTAL_REMOVED}${NC}"
  663. echo -e "New Components: ${YELLOW}${#NEW_COMPONENTS[@]}${NC}"
  664. echo ""
  665. # Show pending fixes if in dry-run mode
  666. if [ ${#FIXED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  667. echo -e "${BOLD}Pending Path Fixes:${NC}"
  668. for entry in "${FIXED_COMPONENTS[@]}"; do
  669. IFS='|' read -r comp_type index id name old_path new_path <<< "$entry"
  670. echo " • ${name}: ${old_path} → ${new_path}"
  671. done
  672. echo ""
  673. fi
  674. # Show pending removals if in dry-run mode
  675. if [ ${#REMOVED_COMPONENTS[@]} -gt 0 ] && [ "$DRY_RUN" = true ]; then
  676. echo -e "${BOLD}Pending Removals:${NC}"
  677. for entry in "${REMOVED_COMPONENTS[@]}"; do
  678. IFS='|' read -r comp_type id name path <<< "$entry"
  679. echo " • ${name} (${path})"
  680. done
  681. echo ""
  682. fi
  683. # Check if everything is up to date
  684. if [ ${#NEW_COMPONENTS[@]} -eq 0 ] && \
  685. [ ${#FIXED_COMPONENTS[@]} -eq 0 ] && \
  686. [ ${#REMOVED_COMPONENTS[@]} -eq 0 ] && \
  687. [ $TOTAL_FIXED -eq 0 ] && \
  688. [ $TOTAL_REMOVED -eq 0 ]; then
  689. print_success "Registry is up to date!"
  690. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  691. echo ""
  692. print_warning "Please review and fix the ${TOTAL_SECURITY_ISSUES} security issue(s) found"
  693. fi
  694. exit 0
  695. fi
  696. # Add components if auto-add is enabled
  697. if [ "$AUTO_ADD" = true ] && [ "$DRY_RUN" = false ]; then
  698. if [ ${#NEW_COMPONENTS[@]} -gt 0 ]; then
  699. print_info "Adding new components to registry..."
  700. echo ""
  701. local added=0
  702. for entry in "${NEW_COMPONENTS[@]}"; do
  703. IFS='|' read -r comp_type id name description path comp_category tags dependencies <<< "$entry"
  704. if add_component_to_registry "$comp_type" "$id" "$name" "$description" "$path" "$comp_category" "$tags" "$dependencies"; then
  705. added=$((added + 1))
  706. fi
  707. done
  708. echo ""
  709. print_success "Added ${added} component(s) to registry"
  710. fi
  711. # Update timestamp
  712. jq '.metadata.lastUpdated = (now | strftime("%Y-%m-%d"))' "$REGISTRY_FILE" > "${REGISTRY_FILE}.tmp"
  713. mv "${REGISTRY_FILE}.tmp" "$REGISTRY_FILE"
  714. elif [ "$DRY_RUN" = true ]; then
  715. print_info "Dry run mode - no changes made to registry"
  716. echo ""
  717. echo "Run without --dry-run to apply these changes"
  718. else
  719. print_info "Run with --auto-add to apply these changes to registry"
  720. echo ""
  721. echo "Or manually update registry.json"
  722. fi
  723. # Final security warning
  724. if [ $TOTAL_SECURITY_ISSUES -gt 0 ]; then
  725. echo ""
  726. print_warning "⚠️ ${TOTAL_SECURITY_ISSUES} security issue(s) require attention"
  727. fi
  728. exit 0
  729. }
  730. main "$@"