104 lines
2.5 KiB
Python
104 lines
2.5 KiB
Python
"""
|
|
Command-line interface for FastAPI OpenAPI-first scaffolding utilities.
|
|
|
|
---
|
|
|
|
## Summary
|
|
|
|
This CLI bootstraps OpenAPI-first FastAPI applications from versioned,
|
|
bundled templates packaged with the library.
|
|
"""
|
|
|
|
import argparse
|
|
import shutil
|
|
from pathlib import Path
|
|
from importlib import resources
|
|
|
|
|
|
DEFAULT_TEMPLATE = "health_app"
|
|
|
|
|
|
def available_templates() -> list[str]:
|
|
"""
|
|
Return a list of available application templates.
|
|
|
|
Returns:
|
|
list[str]:
|
|
Sorted list of template names found in the internal templates directory.
|
|
"""
|
|
root = resources.files("openapi_first.templates")
|
|
return sorted(
|
|
item.name
|
|
for item in root.iterdir()
|
|
if item.is_dir() and not item.name.startswith("_")
|
|
)
|
|
|
|
|
|
def copy_template(template: str, target_dir: Path) -> None:
|
|
"""
|
|
Copy a bundled OpenAPI-first application template into a directory.
|
|
|
|
Args:
|
|
template (str):
|
|
Name of the template to copy.
|
|
target_dir (Path):
|
|
Filesystem path where the template should be copied.
|
|
|
|
Raises:
|
|
FileNotFoundError:
|
|
If the requested template does not exist.
|
|
"""
|
|
target_dir = target_dir.resolve()
|
|
target_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
root = resources.files("openapi_first.templates")
|
|
src = root / template
|
|
|
|
if not src.exists():
|
|
raise FileNotFoundError(
|
|
f"Template '{template}' not found. "
|
|
f"Available templates: {', '.join(available_templates())}"
|
|
)
|
|
|
|
with resources.as_file(src) as path:
|
|
shutil.copytree(path, target_dir, dirs_exist_ok=True)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(
|
|
description="FastAPI OpenAPI-first scaffolding tools"
|
|
)
|
|
parser.add_argument(
|
|
"template",
|
|
nargs="?",
|
|
default=DEFAULT_TEMPLATE,
|
|
help=f"Template name (default: {DEFAULT_TEMPLATE})",
|
|
)
|
|
parser.add_argument(
|
|
"path",
|
|
nargs="?",
|
|
default=None,
|
|
help="Target directory (defaults to template name)",
|
|
)
|
|
parser.add_argument(
|
|
"--list",
|
|
action="store_true",
|
|
help="List available templates and exit",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.list:
|
|
for name in available_templates():
|
|
print(name)
|
|
return
|
|
|
|
target = Path(args.path or args.template.replace("_", "-"))
|
|
|
|
try:
|
|
copy_template(args.template, target)
|
|
except Exception as exc:
|
|
raise SystemExit(str(exc)) from exc
|
|
|
|
print(f"Template '{args.template}' created at {target}")
|