-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateclaw-cli.sh
More file actions
executable file
·412 lines (353 loc) · 13.6 KB
/
templateclaw-cli.sh
File metadata and controls
executable file
·412 lines (353 loc) · 13.6 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
#!/usr/bin/env bash
# TemplateClaw CLI - Dependency-free template fetcher
# Usage: templateclaw <command> [options]
# Install: curl -fsSL https://jeromwolf.github.io/templateclaw/install.sh | bash
set -euo pipefail
# ──────────────────────────────────────────────
# Configuration
# ──────────────────────────────────────────────
VERSION="1.0.0"
BASE_URL="https://jeromwolf.github.io/templateclaw"
API_URL="${BASE_URL}/api.json"
TEMPLATE_URL="${BASE_URL}/templates"
# ──────────────────────────────────────────────
# Colors & Formatting
# ──────────────────────────────────────────────
if [[ -t 1 ]]; then
BOLD="\033[1m"
DIM="\033[2m"
RESET="\033[0m"
PURPLE="\033[38;5;141m"
PINK="\033[38;5;205m"
CYAN="\033[38;5;117m"
GREEN="\033[38;5;114m"
YELLOW="\033[38;5;221m"
RED="\033[38;5;203m"
GRAY="\033[38;5;245m"
WHITE="\033[38;5;255m"
else
BOLD="" DIM="" RESET=""
PURPLE="" PINK="" CYAN="" GREEN="" YELLOW="" RED="" GRAY="" WHITE=""
fi
# ──────────────────────────────────────────────
# Header Art
# ──────────────────────────────────────────────
print_header() {
echo ""
echo -e "${PURPLE}${BOLD} ╔╦╗┌─┐┌┬┐┌─┐┬ ┌─┐┌┬┐┌─┐${PINK}╔═╗┬ ┌─┐┬ ┬${RESET}"
echo -e "${PURPLE}${BOLD} ║ ├┤ │││├─┘│ ├─┤ │ ├┤ ${PINK}║ │ ├─┤│││${RESET}"
echo -e "${PURPLE}${BOLD} ╩ └─┘┴ ┴┴ ┴─┘┴ ┴ ┴ └─┘${PINK}╚═╝┴─┘┴ ┴└┴┘${RESET}"
echo -e " ${DIM}${GRAY}Developer Templates Hub v${VERSION}${RESET}"
echo ""
}
# ──────────────────────────────────────────────
# Helpers
# ──────────────────────────────────────────────
die() {
echo -e "${RED}${BOLD}Error:${RESET} $1" >&2
exit 1
}
info() {
echo -e "${CYAN}${BOLD}>>>${RESET} $1"
}
success() {
echo -e "${GREEN}${BOLD}>>>${RESET} $1"
}
warn() {
echo -e "${YELLOW}${BOLD}>>>${RESET} $1"
}
check_curl() {
command -v curl &>/dev/null || die "curl is required but not found. Install it and try again."
}
# Fetch API JSON and parse with python3 (fallback to jq)
fetch_api() {
check_curl
local json
json=$(curl -fsSL --connect-timeout 10 --max-time 30 "$API_URL" 2>/dev/null) || \
die "Failed to fetch catalog from ${API_URL}. Check your network connection."
echo "$json"
}
# Use python3 for JSON parsing (available on macOS, most Linux)
json_query() {
local json="$1"
local query="$2"
if command -v python3 &>/dev/null; then
echo "$json" | python3 -c "
import sys, json
data = json.load(sys.stdin)
$query
" 2>/dev/null
elif command -v jq &>/dev/null; then
echo "$json" | jq -r "$3" 2>/dev/null
else
die "Requires python3 or jq for JSON parsing. Please install one."
fi
}
# ──────────────────────────────────────────────
# Commands
# ──────────────────────────────────────────────
cmd_list() {
local category_filter=""
while [[ $# -gt 0 ]]; do
case "$1" in
--category|-c) category_filter="$2"; shift 2 ;;
*) shift ;;
esac
done
print_header
info "Fetching template catalog..."
local json
json=$(fetch_api)
echo "$json" | python3 -c "
import sys, json
data = json.load(sys.stdin)
category_filter = '${category_filter}'.lower()
templates = data.get('templates', [])
if category_filter:
templates = [t for t in templates if t.get('category', '').lower() == category_filter
or category_filter in t.get('category', '').lower()]
if not templates:
print(f' No templates found for category: ${category_filter}')
sys.exit(0)
# Group by category
groups = {}
for t in templates:
cat = t.get('category', 'other')
groups.setdefault(cat, []).append(t)
colors = {
'landing-pages': '\033[38;5;141m',
'dashboard': '\033[38;5;205m',
'components': '\033[38;5;117m',
'dev-methodology': '\033[38;5;221m',
'project-setup': '\033[38;5;114m',
'refactoring': '\033[38;5;215m',
}
RESET = '\033[0m'
BOLD = '\033[1m'
DIM = '\033[2m'
WHITE = '\033[38;5;255m'
GRAY = '\033[38;5;245m'
total = len(templates)
print(f' {BOLD}{WHITE}{total} templates found{RESET}')
print()
for cat in sorted(groups.keys()):
items = groups[cat]
color = colors.get(cat, '\033[38;5;245m')
display = cat.replace('-', ' ').title()
print(f' {color}{BOLD}{display}{RESET} ({len(items)})')
print(f' {DIM}{"─" * 50}{RESET}')
for t in items:
name = t['filename'].replace('.md', '')
desc = t.get('description', '')[:60]
diff = t.get('difficulty', '?')
diff_colors = {'beginner': '\033[38;5;114m', 'intermediate': '\033[38;5;221m', 'advanced': '\033[38;5;203m'}
dc = diff_colors.get(diff, GRAY)
print(f' {WHITE}{name:30s}{RESET} {dc}[{diff}]{RESET} {GRAY}{desc}{RESET}')
print()
print(f' {DIM}Use \"templateclaw get <name>\" to download a template{RESET}')
print()
" 2>/dev/null || die "Failed to parse catalog JSON"
}
cmd_search() {
local query="${1:-}"
[[ -z "$query" ]] && die "Usage: templateclaw search <query>"
print_header
info "Searching for \"${query}\"..."
local json
json=$(fetch_api)
echo "$json" | python3 -c "
import sys, json
data = json.load(sys.stdin)
query = '${query}'.lower()
templates = data.get('templates', [])
BOLD = '\033[1m'
RESET = '\033[0m'
WHITE = '\033[38;5;255m'
GRAY = '\033[38;5;245m'
GREEN = '\033[38;5;114m'
PURPLE = '\033[38;5;141m'
matches = []
for t in templates:
searchable = ' '.join([
t.get('title', ''),
t.get('description', ''),
t.get('category', ''),
' '.join(t.get('tags', [])),
t.get('filename', ''),
]).lower()
if query in searchable:
matches.append(t)
if not matches:
print(f' No templates matching \"{query}\"')
sys.exit(0)
print(f' {BOLD}{WHITE}{len(matches)} match(es) found{RESET}')
print()
for t in matches:
name = t['filename'].replace('.md', '')
cat = t.get('category', 'other').replace('-', ' ').title()
desc = t.get('description', '')[:70]
tags = ', '.join(t.get('tags', [])[:4])
print(f' {GREEN}{BOLD}{name}{RESET}')
print(f' {PURPLE}{cat}{RESET} {GRAY}{desc}{RESET}')
if tags:
print(f' {GRAY}Tags: {tags}{RESET}')
print()
print(f' {GRAY}Use \"templateclaw get <name>\" to download{RESET}')
print()
" 2>/dev/null || die "Failed to search catalog"
}
cmd_get() {
local name="${1:-}"
[[ -z "$name" ]] && die "Usage: templateclaw get <template-name>"
# Append .md if not present
local filename="${name%.md}.md"
print_header
info "Downloading ${filename}..."
check_curl
local url="${TEMPLATE_URL}/${filename}"
local output_file="./${filename}"
if [[ -f "$output_file" ]]; then
warn "File ${filename} already exists. Overwrite? [y/N]"
read -r confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo " Cancelled."
exit 0
fi
fi
if curl -fsSL --connect-timeout 10 --max-time 30 -o "$output_file" "$url" 2>/dev/null; then
local size
size=$(wc -c < "$output_file" | tr -d ' ')
success "Downloaded ${filename} (${size} bytes)"
echo -e " ${GRAY}Saved to: ${output_file}${RESET}"
echo ""
else
rm -f "$output_file"
die "Template \"${name}\" not found. Use 'templateclaw list' to see available templates."
fi
}
cmd_info() {
local name="${1:-}"
[[ -z "$name" ]] && die "Usage: templateclaw info <template-name>"
local filename="${name%.md}.md"
print_header
info "Fetching info for ${filename}..."
local json
json=$(fetch_api)
echo "$json" | python3 -c "
import sys, json
data = json.load(sys.stdin)
filename = '${filename}'
templates = data.get('templates', [])
BOLD = '\033[1m'
RESET = '\033[0m'
WHITE = '\033[38;5;255m'
GRAY = '\033[38;5;245m'
PURPLE = '\033[38;5;141m'
PINK = '\033[38;5;205m'
CYAN = '\033[38;5;117m'
GREEN = '\033[38;5;114m'
YELLOW = '\033[38;5;221m'
DIM = '\033[2m'
match = None
for t in templates:
if t.get('filename') == filename:
match = t
break
if not match:
print(f' Template \"{filename}\" not found.')
sys.exit(1)
diff_colors = {'beginner': GREEN, 'intermediate': YELLOW, 'advanced': '\033[38;5;203m'}
print(f' {BOLD}{WHITE}{match[\"title\"]}{RESET}')
print(f' {DIM}{\"─\" * 50}{RESET}')
print()
print(f' {PURPLE}Category{RESET} {match.get(\"category\", \"other\").replace(\"-\", \" \").title()}')
diff = match.get('difficulty', 'intermediate')
dc = diff_colors.get(diff, GRAY)
print(f' {PURPLE}Difficulty{RESET} {dc}{diff}{RESET}')
tags = ', '.join(match.get('tags', []))
print(f' {PURPLE}Tags{RESET} {tags or \"none\"}')
desc = match.get('description', 'No description')
print(f' {PURPLE}Description{RESET} {desc}')
url = match.get('download_url', '')
print(f' {PURPLE}Download{RESET} {CYAN}{url}{RESET}')
print()
print(f' {GRAY}Run \"templateclaw get {filename.replace(\".md\", \"\")}\" to download{RESET}')
print()
" 2>/dev/null || die "Failed to fetch template info"
}
cmd_agent_snippet() {
print_header
echo -e " ${BOLD}${WHITE}Agent Integration Snippet${RESET}"
echo -e " ${DIM}──────────────────────────────────────────────────${RESET}"
echo ""
echo -e " ${PURPLE}# Python (Claude Code / Agents)${RESET}"
echo -e " ${GRAY}import requests${RESET}"
echo -e " ${GRAY}catalog = requests.get(\"${API_URL}\").json()${RESET}"
echo -e " ${GRAY}for t in catalog[\"templates\"]:${RESET}"
echo -e " ${GRAY} print(t[\"title\"], t[\"download_url\"])${RESET}"
echo ""
echo -e " ${PURPLE}# curl (download a template)${RESET}"
echo -e " ${GRAY}curl -fsSL ${TEMPLATE_URL}/saas-landing.md > saas-landing.md${RESET}"
echo ""
echo -e " ${PURPLE}# Claude Code (WebFetch)${RESET}"
echo -e " ${GRAY}WebFetch ${API_URL}${RESET}"
echo -e " ${GRAY}WebFetch ${TEMPLATE_URL}/admin-dashboard.md${RESET}"
echo ""
echo -e " ${PURPLE}# API Endpoints${RESET}"
echo -e " ${CYAN}Catalog (light):${RESET} ${API_URL}"
echo -e " ${CYAN}Catalog (full):${RESET} ${BASE_URL}/api-full.json"
echo -e " ${CYAN}Template file:${RESET} ${TEMPLATE_URL}/{filename}.md"
echo -e " ${CYAN}Site:${RESET} ${BASE_URL}"
echo ""
}
cmd_help() {
print_header
echo -e " ${BOLD}${WHITE}Commands${RESET}"
echo -e " ${DIM}──────────────────────────────────────────────────${RESET}"
echo ""
echo -e " ${GREEN}list${RESET} ${GRAY}[--category <cat>]${RESET} List all templates (optionally filter by category)"
echo -e " ${GREEN}search${RESET} ${GRAY}<query>${RESET} Search templates by keyword"
echo -e " ${GREEN}get${RESET} ${GRAY}<name>${RESET} Download a template to current directory"
echo -e " ${GREEN}info${RESET} ${GRAY}<name>${RESET} Show template details"
echo -e " ${GREEN}agent-snippet${RESET} Show agent integration code"
echo -e " ${GREEN}help${RESET} Show this help message"
echo -e " ${GREEN}version${RESET} Show version"
echo ""
echo -e " ${BOLD}${WHITE}Examples${RESET}"
echo -e " ${DIM}──────────────────────────────────────────────────${RESET}"
echo ""
echo -e " ${GRAY}templateclaw list${RESET}"
echo -e " ${GRAY}templateclaw list --category dashboard${RESET}"
echo -e " ${GRAY}templateclaw search \"landing page\"${RESET}"
echo -e " ${GRAY}templateclaw get saas-landing${RESET}"
echo -e " ${GRAY}templateclaw info admin-dashboard${RESET}"
echo ""
echo -e " ${BOLD}${WHITE}Install${RESET}"
echo -e " ${DIM}──────────────────────────────────────────────────${RESET}"
echo ""
echo -e " ${GRAY}curl -fsSL https://jeromwolf.github.io/templateclaw/install.sh | bash${RESET}"
echo ""
}
cmd_version() {
echo -e "${PURPLE}${BOLD}TemplateClaw${RESET} v${VERSION}"
}
# ──────────────────────────────────────────────
# Main Dispatcher
# ──────────────────────────────────────────────
main() {
local cmd="${1:-help}"
shift 2>/dev/null || true
case "$cmd" in
list) cmd_list "$@" ;;
search) cmd_search "$@" ;;
get|download) cmd_get "$@" ;;
info) cmd_info "$@" ;;
agent-snippet) cmd_agent_snippet ;;
help|--help|-h) cmd_help ;;
version|--version|-v) cmd_version ;;
*)
die "Unknown command: ${cmd}. Run 'templateclaw help' for usage."
;;
esac
}
main "$@"