Files
mytoolkit/bin/commands/templates.py
T
Zhengshou Lai 0317189623 refactor(templates): rename show to list for CLI consistency
Other subcommand groups (server, env, ssh, webpage, git proxy) use
`list` for enumeration; align templates with the same convention.
2026-05-08 20:56:14 +08:00

78 lines
2.2 KiB
Python

"""Templates registry CLI."""
from pathlib import Path
import click
from bin import templates_registry
@click.group(name="templates")
def templates_cmd():
"""Manage external templates registry."""
pass
@templates_cmd.command("register")
@click.argument("path", type=click.Path(exists=True, file_okay=False, dir_okay=True))
def templates_register(path):
"""Register a directory as the templates root."""
resolved = templates_registry.set_root(Path(path))
click.secho(f"Registered: {resolved}", fg="green")
@templates_cmd.command("list")
def templates_list():
"""List the current registry and available templates."""
if not templates_registry.CONFIG_PATH.exists():
click.echo("No registry. Run: mytoolkit templates register <path>")
return
import json
with open(templates_registry.CONFIG_PATH, encoding="utf-8") as f:
data = json.load(f)
raw = data.get("dir")
click.echo(f"Registry: {templates_registry.CONFIG_PATH}")
click.echo(f"Root: {raw}")
if not raw or not Path(raw).is_dir():
click.secho("(root path missing)", fg="red")
return
root = Path(raw)
pdf_dir = root / "md-to-pdf"
docx_dir = root / "md-to-docx"
click.echo("\nmd-to-pdf:")
if pdf_dir.is_dir():
entries = sorted(
(p.name, p / "template.typ")
for p in pdf_dir.iterdir()
if (p / "template.typ").exists()
)
if not entries:
click.echo(" (none)")
else:
width = max(len(n) for n, _ in entries)
for name, path in entries:
click.echo(f" - {name:<{width}} {path}")
else:
click.secho(" (subdirectory missing)", fg="yellow")
click.echo("\nmd-to-docx:")
if docx_dir.is_dir():
entries = sorted(
(p.name, p / "template.docx")
for p in docx_dir.iterdir()
if (p / "template.docx").exists()
)
if not entries:
click.echo(" (none)")
else:
width = max(len(n) for n, _ in entries)
for name, path in entries:
click.echo(f" - {name:<{width}} {path}")
else:
click.secho(" (subdirectory missing)", fg="yellow")