forked from openshift-eng/edge-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarketplace
More file actions
executable file
·804 lines (692 loc) · 27.7 KB
/
Copy pathmarketplace
File metadata and controls
executable file
·804 lines (692 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#!/usr/bin/env bash
#
# Plugin Marketplace CLI
# Developer tool for scaffolding, validating, and cataloging
# Claude Code plugins for OpenShift and edge workflows
#
set -euo pipefail
# Constants
SCRIPT_DIR="$(cd "$(dirname "$(realpath "${BASH_SOURCE[0]}" 2>/dev/null || readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")")" && pwd -P)"
PLUGINS_DIR="${SCRIPT_DIR}/plugins"
MARKETPLACE_FILE="${SCRIPT_DIR}/.claude-plugin/marketplace.json"
TEMPLATES_DIR="${PLUGINS_DIR}/.templates"
# Colors for output (disable when stdout is not a terminal)
RED=''
GREEN=''
YELLOW=''
BLUE=''
MAGENTA=''
CYAN=''
NC=''
BOLD=''
if [[ -t 1 ]]; then
RED=$'\033[0;31m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[1;33m'
BLUE=$'\033[0;34m'
MAGENTA=$'\033[0;35m'
CYAN=$'\033[0;36m'
NC=$'\033[0m'
BOLD=$'\033[1m'
fi
# Helper functions
error() {
echo -e "${RED}Error:${NC} $*" >&2
exit 1
}
warn() {
echo -e "${YELLOW}Warning:${NC} $*" >&2
}
info() {
echo -e "${BLUE}Info:${NC} $*"
}
success() {
echo -e "${GREEN}Success:${NC} $*"
}
# Check dependencies
check_dependencies() {
local deps=("jq" "git" "python3")
local missing=()
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
missing+=("$dep")
fi
done
if [ ${#missing[@]} -gt 0 ]; then
error "Missing required dependencies: ${missing[*]}\nInstall with: dnf install jq git python3"
fi
}
# Read a field from a plugin.json file
json_get() {
local file="$1"
local query="$2"
jq -r "$query // empty" "$file" 2>/dev/null || echo ""
}
# Generate bash completion script
_generate_bash_completion() {
local escaped_dir="${PLUGINS_DIR//\'/\'\\\'\'}"
echo "_MARKETPLACE_PLUGINS_DIR='${escaped_dir}'"
cat <<'BASH_COMP'
_marketplace_completions() {
local cur prev commands plugin_cmds flag_cmds
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="list ls search show info validate new create add catalog-update completion help"
plugin_cmds="show info validate add"
flag_cmds="new create add"
if [[ $COMP_CWORD -eq 1 ]]; then
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
return
fi
local cmd="${COMP_WORDS[1]}"
# Complete plugin names for commands that take them
for pc in $plugin_cmds; do
if [[ "$cmd" == "$pc" && $COMP_CWORD -eq 2 ]]; then
local names=""
if [[ -d "$_MARKETPLACE_PLUGINS_DIR" ]]; then
local dir
for dir in "$_MARKETPLACE_PLUGINS_DIR"/*/; do
[[ ! -d "$dir" ]] && continue
local name
name=$(basename "$dir")
[[ "$name" == ".templates" || "$name" == "docs" ]] && continue
[[ -f "$dir/.claude-plugin/plugin.json" ]] && names+="$name "
done
fi
COMPREPLY=($(compgen -W "$names" -- "$cur"))
return
fi
done
# Complete flags for new/create/add
for fc in $flag_cmds; do
if [[ "$cmd" == "$fc" && "$cur" == --* ]]; then
COMPREPLY=($(compgen -W "--skill --hook --mcp --agent" -- "$cur"))
return
fi
done
# Complete shell name for completion command
if [[ "$cmd" == "completion" && $COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "bash zsh" -- "$cur"))
return
fi
}
complete -F _marketplace_completions marketplace
BASH_COMP
}
# Generate zsh completion script
_generate_zsh_completion() {
local escaped_dir="${PLUGINS_DIR//\'/\'\\\'\'}"
echo "_MARKETPLACE_PLUGINS_DIR='${escaped_dir}'"
cat <<'ZSH_COMP'
#compdef marketplace
_marketplace_plugin_names() {
[[ ! -d "$_MARKETPLACE_PLUGINS_DIR" ]] && return
local -a names
local dir
for dir in "$_MARKETPLACE_PLUGINS_DIR"/*/; do
[[ ! -d "$dir" ]] && continue
local name="${dir:t}"
[[ "$name" == ".templates" || "$name" == "docs" ]] && continue
[[ -f "$dir/.claude-plugin/plugin.json" ]] && names+=("$name")
done
compadd -a names
}
_marketplace() {
local -a commands
commands=(
'list:List all available plugins'
'ls:List all available plugins'
'search:Search plugins by name or description'
'show:Show detailed information about a plugin'
'info:Show detailed information about a plugin'
'validate:Validate plugin structure and metadata'
'new:Create new plugin from template'
'create:Create new plugin from template'
'add:Add components to an existing plugin'
'catalog-update:Rebuild marketplace catalog'
'completion:Generate shell completion script'
'help:Show help text'
)
_arguments -C \
'1:command:->command' \
'*::arg:->args'
case $state in
command)
_describe 'command' commands
;;
args)
case ${words[1]} in
show|info|validate)
_marketplace_plugin_names
;;
add|new|create)
_arguments \
'1:plugin-name:' \
'--skill[Include a skill]' \
'--hook[Include hooks]' \
'--mcp[Include MCP server config]' \
'--agent[Include agent definition]'
;;
completion)
_values 'shell' bash zsh
;;
esac
;;
esac
}
compdef _marketplace marketplace
ZSH_COMP
}
# Update catalog: scan plugins and generate .claude-plugin/marketplace.json
update_catalog() {
info "Scanning plugins directory..."
mkdir -p "$(dirname "$MARKETPLACE_FILE")"
local plugin_count=0
local plugins_json="[]"
# Find all plugin directories (contain .claude-plugin/plugin.json)
while IFS= read -r -d '' plugin_file; do
local plugin_dir
plugin_dir=$(dirname "$(dirname "$plugin_file")")
local plugin_name
plugin_name=$(basename "$plugin_dir")
# Skip template and docs directories
[[ "$plugin_name" == ".templates" || "$plugin_name" == "docs" ]] && continue
# Validate it has required fields
local name
name=$(json_get "$plugin_file" ".name")
if [[ -z "$name" ]]; then
warn "Skipping $plugin_name: missing name in plugin.json"
continue
fi
# Compute relative path from repo root
local rel_path="plugins/${plugin_name}"
local description
description=$(json_get "$plugin_file" ".description")
local version
version=$(json_get "$plugin_file" ".version")
plugins_json=$(echo "$plugins_json" | jq \
--arg name "$name" \
--arg source "./$rel_path" \
--arg description "$description" \
--arg version "$version" \
'. += [{"name": $name, "source": $source, "description": $description, "version": $version}]')
((plugin_count++)) || true
done < <(find "$PLUGINS_DIR" -maxdepth 3 -path '*/.claude-plugin/plugin.json' -print0)
# Write marketplace.json
jq -n \
--arg name "edge-tooling" \
--arg desc "Claude Code plugins for OpenShift and edge computing workflows" \
--arg owner_name "brandisher" \
--arg owner_email "chad@redhat.com" \
--argjson plugins "$plugins_json" \
'{"name": $name, "description": $desc, "owner": {"name": $owner_name, "email": $owner_email}, "plugins": ($plugins | sort_by(.name))}' \
> "$MARKETPLACE_FILE"
success "Catalog updated: $plugin_count plugin(s) found"
}
# List all available plugins
list_plugins() {
local filter="${1:-}"
# Ensure marketplace.json exists
if [[ ! -f "$MARKETPLACE_FILE" ]]; then
update_catalog
fi
echo -e "${BOLD}Available Plugins:${NC}\n"
local count=0
local plugin_names
plugin_names=$(jq -r '.plugins[].name' "$MARKETPLACE_FILE")
while IFS= read -r name; do
[[ -z "$name" ]] && continue
local plugin_path
plugin_path=$(jq -r --arg n "$name" '.plugins[] | select(.name == $n) | .source' "$MARKETPLACE_FILE")
local plugin_json="${SCRIPT_DIR}/${plugin_path}/.claude-plugin/plugin.json"
if [[ ! -f "$plugin_json" ]]; then
warn "Plugin metadata not found for $name"
continue
fi
local version description author
version=$(json_get "$plugin_json" ".version")
description=$(json_get "$plugin_json" ".description")
author=$(json_get "$plugin_json" ".author.name")
# Apply filter if provided
if [[ -n "$filter" ]]; then
if [[ ! "$name" =~ $filter && ! "${description:-}" =~ $filter ]]; then
continue
fi
fi
echo -e "${CYAN}${name}${NC} ${MAGENTA}(v${version})${NC}"
echo -e " ${BOLD}Author:${NC} $author"
echo -e " $description"
echo ""
((count++)) || true
done <<< "$plugin_names"
if [ $count -eq 0 ]; then
if [[ -n "$filter" ]]; then
warn "No plugins found matching filter: $filter"
else
info "No plugins found. Run 'marketplace catalog-update' to scan."
fi
else
echo -e "${BOLD}Total:${NC} $count plugin(s)"
fi
}
# Show plugin details
show_plugin() {
local plugin_name="$1"
if [[ ! -f "$MARKETPLACE_FILE" ]]; then
update_catalog
fi
local plugin_path
plugin_path=$(jq -r --arg n "$plugin_name" '.plugins[] | select(.name == $n) | .source' "$MARKETPLACE_FILE")
if [[ -z "$plugin_path" || "$plugin_path" == "null" ]]; then
error "Plugin not found: $plugin_name"
fi
local plugin_json="${SCRIPT_DIR}/${plugin_path}/.claude-plugin/plugin.json"
if [[ ! -f "$plugin_json" ]]; then
error "Plugin metadata not found: $plugin_json"
fi
echo -e "${BOLD}Plugin Details: ${CYAN}${plugin_name}${NC}\n"
local version description author homepage license
version=$(json_get "$plugin_json" ".version")
description=$(json_get "$plugin_json" ".description")
author=$(json_get "$plugin_json" ".author.name")
homepage=$(json_get "$plugin_json" ".homepage")
license=$(json_get "$plugin_json" ".license")
echo -e "${BOLD}Version:${NC} $version"
echo -e "${BOLD}Author:${NC} $author"
echo -e "${BOLD}Description:${NC} $description"
[[ -n "$homepage" ]] && echo -e "${BOLD}Homepage:${NC} $homepage"
[[ -n "$license" ]] && echo -e "${BOLD}License:${NC} $license"
# Show components
local plugin_dir="${SCRIPT_DIR}/${plugin_path}"
echo -e "\n${BOLD}Components:${NC}"
[[ -d "${plugin_dir}/skills" ]] && echo " - Skills"
[[ -d "${plugin_dir}/hooks" ]] && echo " - Hooks"
[[ -f "${plugin_dir}/.mcp.json" ]] && echo " - MCP Server"
[[ -d "${plugin_dir}/agents" ]] && echo " - Agents"
# Show README path if available
if [[ -f "${plugin_dir}/README.md" ]]; then
echo -e "\n${BOLD}Documentation:${NC}"
echo " See: ${plugin_path}/README.md"
fi
}
# Validate plugin structure
validate_plugin() {
local plugin_name="$1"
# Resolve plugin directory: prefer catalog path for accuracy, fall back to convention
local plugin_dir
if [[ -f "$MARKETPLACE_FILE" ]]; then
local plugin_path
plugin_path=$(jq -r --arg n "$plugin_name" '.plugins[] | select(.name == $n) | .source' "$MARKETPLACE_FILE")
if [[ -n "$plugin_path" && "$plugin_path" != "null" ]]; then
plugin_dir="${SCRIPT_DIR}/${plugin_path}"
fi
fi
plugin_dir="${plugin_dir:-${PLUGINS_DIR}/${plugin_name}}"
if [[ ! -d "$plugin_dir" ]]; then
error "Plugin directory not found: $plugin_dir"
fi
info "Validating plugin: $plugin_name"
local errors=0
# Check .claude-plugin/plugin.json exists
local plugin_json="${plugin_dir}/.claude-plugin/plugin.json"
if [[ ! -f "$plugin_json" ]]; then
warn "Missing .claude-plugin/plugin.json"
((errors++)) || true
else
# Validate JSON is parseable
if ! jq empty "$plugin_json" 2>/dev/null; then
warn "plugin.json is not valid JSON"
((errors++)) || true
else
# Validate required fields
local required_fields=("name" "description" "version")
for field in "${required_fields[@]}"; do
local value
value=$(json_get "$plugin_json" ".$field")
if [[ -z "$value" ]]; then
warn "Missing required field in plugin.json: $field"
((errors++)) || true
fi
done
# Validate author object
local author_name
author_name=$(json_get "$plugin_json" ".author.name")
if [[ -z "$author_name" ]]; then
warn "Missing .author.name in plugin.json"
((errors++)) || true
fi
# Validate version format (semver)
local version
version=$(json_get "$plugin_json" ".version")
if [[ -n "$version" && ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$ ]]; then
warn "Invalid version format: $version (expected semver: X.Y.Z[-prerelease][+build])"
((errors++)) || true
fi
# Validate plugin name matches directory
local json_name
json_name=$(json_get "$plugin_json" ".name")
if [[ "$json_name" != "$plugin_name" ]]; then
warn "Plugin name in plugin.json ($json_name) does not match directory name ($plugin_name)"
((errors++)) || true
fi
fi
fi
# Check README.md exists
if [[ ! -f "${plugin_dir}/README.md" ]]; then
warn "Missing README.md"
((errors++)) || true
fi
# Check at least one component exists
local has_component=false
[[ -d "${plugin_dir}/skills" ]] && has_component=true
[[ -d "${plugin_dir}/hooks" ]] && has_component=true
[[ -f "${plugin_dir}/.mcp.json" ]] && has_component=true
[[ -d "${plugin_dir}/agents" ]] && has_component=true
if [[ "$has_component" == "false" ]]; then
warn "No components found (expected at least one of: skills/, hooks/, .mcp.json, agents/)"
((errors++)) || true
fi
if [ $errors -eq 0 ]; then
success "Validation passed"
return 0
else
warn "Validation completed with $errors warning(s)"
return 1
fi
}
# Helper: substitute template placeholders
_scaffold_from_template() {
local src="$1"
local dst="$2"
local plugin_name="$3"
local category="${4:-}"
sed \
-e "s/{{PLUGIN_NAME}}/${plugin_name}/g" \
-e "s/{{CATEGORY}}/${category}/g" \
"$src" > "$dst"
}
# Create new plugin from template, or add components to an existing plugin
create_plugin() {
local plugin_name="$1"
shift
# Validate name
if [[ ! "$plugin_name" =~ ^[a-z][a-z0-9-]*$ ]]; then
error "Invalid plugin name. Must start with a letter; use only lowercase letters, numbers, and hyphens."
fi
local plugin_dir="${PLUGINS_DIR}/${plugin_name}"
local adding_to_existing=false
if [[ -d "$plugin_dir" ]]; then
if [[ -f "${plugin_dir}/.claude-plugin/plugin.json" ]]; then
adding_to_existing=true
info "Plugin '${plugin_name}' already exists — adding components"
else
error "Directory '${plugin_dir}' exists but is not a valid plugin (missing .claude-plugin/plugin.json)"
fi
fi
# Parse component flags
local has_skill=false
local has_hook=false
local has_mcp=false
local has_agent=false
while [[ $# -gt 0 ]]; do
case $1 in
--skill) has_skill=true; shift ;;
--hook) has_hook=true; shift ;;
--mcp) has_mcp=true; shift ;;
--agent) has_agent=true; shift ;;
*) error "Unknown flag: $1" ;;
esac
done
# If no flags given, prompt interactively
if [[ "$has_skill" == "false" && "$has_hook" == "false" && "$has_mcp" == "false" && "$has_agent" == "false" ]]; then
echo -e "${BOLD}Select components to include (space-separated numbers):${NC}"
echo " 1) Skill (SKILL.md -- slash command or auto-invoked capability)"
echo " 2) Hook (hooks.json -- event-driven automation)"
echo " 3) MCP (.mcp.json -- external tool integration)"
echo " 4) Agent (agent definition for subagent spawning)"
echo ""
read -p "Components [1]: " selections
selections="${selections:-1}"
for sel in $selections; do
case $sel in
1) has_skill=true ;;
2) has_hook=true ;;
3) has_mcp=true ;;
4) has_agent=true ;;
*) error "Invalid selection: $sel" ;;
esac
done
fi
# --- Add components to existing plugin ---
if [[ "$adding_to_existing" == "true" ]]; then
echo -e "\n${BOLD}Adding components to: ${CYAN}${plugin_name}${NC}\n"
# Read description from existing plugin.json for skill scaffolding
local description=""
local plugin_json="${plugin_dir}/.claude-plugin/plugin.json"
if [[ -f "$plugin_json" ]]; then
description=$(json_get "$plugin_json" ".description")
fi
local added=0
if [[ "$has_skill" == "true" ]]; then
local skill_dst="${plugin_dir}/skills/${plugin_name}/SKILL.md"
if [[ -f "$skill_dst" ]]; then
warn "Skill already exists: skills/${plugin_name}/SKILL.md (skipping)"
else
mkdir -p "${plugin_dir}/skills/${plugin_name}"
(python3 - "${TEMPLATES_DIR}/SKILL.md.template" "$skill_dst" \
"$plugin_name" "$description" <<'PYEOF'
import sys
tmpl, dst, name, desc = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
content = open(tmpl, encoding='utf-8').read()
yaml_desc = desc.replace('\\', '\\\\').replace('"', '\\"')
for placeholder, value in [('{{PLUGIN_NAME}}', name), ('{{DESCRIPTION}}', yaml_desc)]:
content = content.replace(placeholder, value)
open(dst, 'w', encoding='utf-8').write(content)
PYEOF
) || error "Failed to generate SKILL.md from template"
echo " + Skill: skills/${plugin_name}/SKILL.md"
((added++)) || true
fi
fi
if [[ "$has_hook" == "true" ]]; then
if [[ -f "${plugin_dir}/hooks/hooks.json" ]]; then
warn "Hooks already exist: hooks/hooks.json (skipping)"
else
mkdir -p "${plugin_dir}/hooks"
_scaffold_from_template "${TEMPLATES_DIR}/hooks.json.template" "${plugin_dir}/hooks/hooks.json" "$plugin_name" ""
echo " + Hook: hooks/hooks.json"
((added++)) || true
fi
fi
if [[ "$has_mcp" == "true" ]]; then
if [[ -f "${plugin_dir}/.mcp.json" ]]; then
warn "MCP config already exists: .mcp.json (skipping)"
else
_scaffold_from_template "${TEMPLATES_DIR}/mcp.json.template" "${plugin_dir}/.mcp.json" "$plugin_name" ""
echo " + MCP: .mcp.json"
((added++)) || true
fi
fi
if [[ "$has_agent" == "true" ]]; then
local agent_dst="${plugin_dir}/agents/${plugin_name}.md"
if [[ -f "$agent_dst" ]]; then
warn "Agent already exists: agents/${plugin_name}.md (skipping)"
else
mkdir -p "${plugin_dir}/agents"
_scaffold_from_template "${TEMPLATES_DIR}/agent.md.template" "$agent_dst" "$plugin_name" ""
echo " + Agent: agents/${plugin_name}.md"
((added++)) || true
fi
fi
echo ""
if [[ $added -eq 0 ]]; then
warn "No new components added (all requested components already exist)"
else
success "$added component(s) added to ${plugin_name}"
echo "Next steps:"
echo " 1. Implement your new components"
echo " 2. Fill in TODO markers"
echo " 3. Test with: ./marketplace validate ${plugin_name}"
fi
return
fi
# --- Create new plugin ---
echo -e "\n${BOLD}Creating new plugin: ${CYAN}${plugin_name}${NC}\n"
# Prompt for metadata
read -p "Category (cluster-ops/debug/deploy/network/operator/ci-cd/util): " category
[[ ! "$category" =~ ^(cluster-ops|debug|deploy|network|operator|ci-cd|util)$ ]] && error "Invalid category"
read -p "Short description: " description
[[ -z "$description" ]] && error "Description cannot be empty"
read -p "Author (your GitHub handle): " author
# Validate required templates exist
[[ ! -f "${TEMPLATES_DIR}/plugin.json.template" ]] && error "Template not found: plugin.json.template"
[[ ! -f "${TEMPLATES_DIR}/README.md.template" ]] && error "Template not found: README.md.template"
# Create plugin directory with cleanup trap
trap 'rm -rf "$plugin_dir"' ERR
mkdir -p "${plugin_dir}/.claude-plugin"
# Generate plugin.json: substitute safe fields via sed, then inject
# description and author through jq to prevent JSON injection.
_scaffold_from_template "${TEMPLATES_DIR}/plugin.json.template" "${plugin_dir}/.claude-plugin/plugin.json" "$plugin_name" ""
local tmp_json
tmp_json=$(jq --arg desc "$description" --arg auth "$author" \
'.description = $desc | .author.name = $auth' \
"${plugin_dir}/.claude-plugin/plugin.json")
echo "$tmp_json" > "${plugin_dir}/.claude-plugin/plugin.json"
# Scaffold components
if [[ "$has_skill" == "true" ]]; then
mkdir -p "${plugin_dir}/skills/${plugin_name}"
(python3 - "${TEMPLATES_DIR}/SKILL.md.template" "${plugin_dir}/skills/${plugin_name}/SKILL.md" \
"$plugin_name" "$description" <<'PYEOF'
import sys
tmpl, dst, name, desc = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
content = open(tmpl, encoding='utf-8').read()
# Escape backslashes and double quotes so the description is valid inside YAML double-quoted strings
yaml_desc = desc.replace('\\', '\\\\').replace('"', '\\"')
for placeholder, value in [('{{PLUGIN_NAME}}', name), ('{{DESCRIPTION}}', yaml_desc)]:
content = content.replace(placeholder, value)
open(dst, 'w', encoding='utf-8').write(content)
PYEOF
) || error "Failed to generate SKILL.md from template"
fi
if [[ "$has_hook" == "true" ]]; then
mkdir -p "${plugin_dir}/hooks"
_scaffold_from_template "${TEMPLATES_DIR}/hooks.json.template" "${plugin_dir}/hooks/hooks.json" "$plugin_name" ""
fi
if [[ "$has_mcp" == "true" ]]; then
_scaffold_from_template "${TEMPLATES_DIR}/mcp.json.template" "${plugin_dir}/.mcp.json" "$plugin_name" ""
fi
if [[ "$has_agent" == "true" ]]; then
mkdir -p "${plugin_dir}/agents"
_scaffold_from_template "${TEMPLATES_DIR}/agent.md.template" "${plugin_dir}/agents/${plugin_name}.md" "$plugin_name" ""
fi
# Generate README.md
(python3 - "${TEMPLATES_DIR}/README.md.template" "${plugin_dir}/README.md" \
"$plugin_name" "$category" "$description" "$author" <<'PYEOF'
import sys
tmpl, dst, name, cat, desc, auth = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6]
content = open(tmpl, encoding='utf-8').read()
for placeholder, value in [('{{PLUGIN_NAME}}', name), ('{{CATEGORY}}', cat),
('{{DESCRIPTION}}', desc), ('{{AUTHOR}}', auth)]:
content = content.replace(placeholder, value)
open(dst, 'w', encoding='utf-8').write(content)
PYEOF
) || error "Failed to generate README.md from template"
trap - ERR
# Show summary
success "Plugin scaffolding created: ${plugin_dir}"
echo -e "\n${BOLD}Components:${NC}"
[[ "$has_skill" == "true" ]] && echo " - Skill: skills/${plugin_name}/SKILL.md"
[[ "$has_hook" == "true" ]] && echo " - Hook: hooks/hooks.json"
[[ "$has_mcp" == "true" ]] && echo " - MCP: .mcp.json"
[[ "$has_agent" == "true" ]] && echo " - Agent: agents/${plugin_name}.md"
echo ""
echo "Next steps:"
echo " 1. Implement your plugin components"
echo " 2. Fill in TODO markers"
echo " 3. Test with: ./marketplace validate ${plugin_name}"
echo " 4. Update catalog: ./marketplace catalog-update"
echo " 5. Users install via: /plugin install ${plugin_name}"
}
# Main CLI handler
main() {
# Check dependencies
check_dependencies
# Parse command
local command="${1:-help}"
shift || true
case "$command" in
list|ls)
list_plugins "${1:-}"
;;
show|info)
[[ -z "${1:-}" ]] && error "Usage: marketplace show <plugin-name>"
show_plugin "$1"
;;
search)
[[ -z "${1:-}" ]] && error "Usage: marketplace search <query>"
list_plugins "$1"
;;
validate)
[[ -z "${1:-}" ]] && error "Usage: marketplace validate <plugin-name>"
validate_plugin "$1"
;;
new|create|add)
[[ -z "${1:-}" ]] && error "Usage: marketplace new <plugin-name> [--skill] [--hook] [--mcp] [--agent]"
create_plugin "$@"
;;
catalog-update)
update_catalog
;;
completion)
local shell="${1:-bash}"
case "$shell" in
bash) _generate_bash_completion ;;
zsh) _generate_zsh_completion ;;
*) error "Unsupported shell: $shell (supported: bash, zsh)" ;;
esac
;;
help|--help|-h)
cat <<EOF
${BOLD}Plugin Marketplace CLI${NC} (Developer Tool)
${BOLD}Usage:${NC}
marketplace <command> [arguments]
${BOLD}Commands:${NC}
${CYAN}list${NC} [filter] List all available plugins (optionally filtered)
${CYAN}search${NC} <query> Search plugins by name or description
${CYAN}show${NC} <plugin> Show detailed information about a plugin
${CYAN}validate${NC} <plugin> Validate plugin structure and metadata
${CYAN}new${NC} <name> [flags] Create new plugin from template
${CYAN}add${NC} <name> [flags] Add components to an existing plugin
${CYAN}catalog-update${NC} Rebuild .claude-plugin/marketplace.json
${CYAN}completion${NC} <shell> Generate shell completion (bash, zsh)
${BOLD}Component Flags for 'new' / 'add':${NC}
--skill Include a skill (SKILL.md)
--hook Include hooks (hooks.json)
--mcp Include MCP server config (.mcp.json)
--agent Include agent definition
${BOLD}Examples:${NC}
marketplace list # List all plugins
marketplace search network # Search for network plugins
marketplace show ovn-topology # Show plugin details
marketplace new my-plugin --skill --agent # Create plugin with skill + agent
marketplace new my-plugin # Create plugin (interactive)
marketplace add my-plugin --hook # Add a hook to an existing plugin
marketplace catalog-update # Rebuild marketplace catalog
marketplace completion bash # Output bash completion script
source <(marketplace completion bash) # Enable completion in current shell
${BOLD}User Installation:${NC}
/plugin marketplace add openshift-eng/edge-tooling
/plugin install <plugin-name>
${BOLD}Documentation:${NC}
Plugin development: plugins/docs/DEVELOPMENT.md
Contributing: plugins/docs/CONTRIBUTING.md
Plugin structure: plugins/README.md
EOF
;;
*)
error "Unknown command: $command\nRun 'marketplace help' for usage information"
;;
esac
}
# Run main
main "$@"