Files
jpm/jeAddons/cli.py
T
Christoph d5f1dd6366 Implements jeaddons CLI tool
Adds a CLI tool to copy files or folders from a git repository into the current project based on a registry.

The tool provides commands to:
- `add`: Copies files/folders based on registry entries.
- `list`: Lists available registry entries.

Registry entries are defined in `jeAddons/registry.py`.
The tool supports overriding the registry's ref and destination.
2026-02-11 22:18:38 +01:00

71 lines
2.4 KiB
Python

import typer
from pathlib import Path
from jeAddons.copier import fetch_path
from jeAddons.registry import load_registry, get_registry_entry
app = typer.Typer(no_args_is_help=True)
@app.callback()
def main():
"""Copy files or folders from a git repository into your current project."""
return
@app.command()
def add(
name: str = typer.Argument(..., help="Registry name from jeAddons/registry.py."),
ref: str | None = typer.Option(None, "--ref", "-r", help="Override registry git ref."),
dest: Path | None = typer.Option(None, "--dest", "-d", help="Override registry destination directory."),
force: bool = typer.Option(False, "--force", "-f", help="Overwrite existing file/folder with same name."),
):
try:
entry = get_registry_entry(name)
except KeyError:
typer.echo(f"Unknown registry entry: '{name}'.")
typer.echo("Run `jeaddons list` to show available entries.")
raise typer.Exit(1)
repo = entry.get("repo")
repo_path = entry.get("path")
if not repo or not repo_path:
typer.echo(f"Registry entry '{name}' is missing required keys: 'repo' and/or 'path'.")
raise typer.Exit(1)
effective_ref = ref or entry.get("ref", "HEAD")
effective_dest = dest or Path.cwd() / entry.get("dest", ".")
try:
target = fetch_path(repo, repo_path, effective_dest, ref=effective_ref, force=force)
except FileNotFoundError:
typer.echo(f"Path '{repo_path}' was not found in repository '{repo}'.")
raise typer.Exit(1)
except FileExistsError as exc:
typer.echo(f"Target already exists: {exc}. Use --force to overwrite.")
raise typer.Exit(1)
except Exception as exc:
typer.echo(f"Failed to copy from repository: {exc}")
raise typer.Exit(1)
typer.echo(f"Added: {target}")
@app.command("list")
def list_entries():
"""List available registry entries."""
registry = load_registry()
if not registry:
typer.echo("Registry is empty. Add entries in jeAddons/registry.py")
return
for name, entry in registry.items():
repo = entry.get("repo", "<missing repo>")
repo_path = entry.get("path", "<missing path>")
ref = entry.get("ref", "HEAD")
dest = entry.get("dest", ".")
typer.echo(f"{name}: repo={repo} path={repo_path} ref={ref} dest={dest}")
if __name__ == "__main__":
app()