refactor(cli): use RepoCatalogState for repository operations

- Replaces direct config usage with RepoCatalogState to manage categories
  and repositories, improving modularity and encapsulation.
- Updates repository movement logic to use `RepoCatalogState` methods
  for better abstraction and error handling.
This commit is contained in:
2025-05-11 14:38:18 +02:00
parent 458d965062
commit 9bff626f30

View File

@@ -1,10 +1,9 @@
from typer import Typer, Argument
from rich.console import Console
from pathlib import Path
import shutil
import typer
from repocat.config import load_config
from repocat.models.catalog import RepoCatalogState
app = Typer()
console = Console()
@@ -12,20 +11,16 @@ console = Console()
def complete_source_repo(incomplete: str):
config = load_config()
base = config.base_dir
catalog = RepoCatalogState.from_config(config)
results = []
for cat in config.categories:
if cat.is_archive:
for cat in catalog.categories:
if cat.config.is_archive:
continue
cat_path = base / cat.subdir
if not cat_path.exists():
continue
for item in cat_path.iterdir():
if item.is_dir():
full_name = f"{cat.name}/{item.name}"
if full_name.startswith(incomplete):
results.append(full_name)
for repo in cat.repos:
full_name = f"{cat.config.name}/{repo.name}"
if full_name.startswith(incomplete):
results.append(full_name)
return sorted(results)
@@ -43,39 +38,35 @@ def move_command(
Verschiebt ein Repository in eine andere Kategorie.
"""
config = load_config()
base = config.base_dir
catalog = RepoCatalogState.from_config(config)
# Quelle aufspalten in Kategorie und Repo
try:
source_cat_name, repo_name = source.split("/", 1)
except ValueError:
console.print("[red]Ungültiges Format für Quelle. Bitte <kategorie>/<reponame> angeben.[/]")
raise typer.Exit(1)
source_cat = next((c for c in config.categories if c.name == source_cat_name), None)
source_cat = catalog.get_category(source_cat_name)
if not source_cat:
console.print(f"[red]Unbekannte Quellkategorie:[/] {source_cat_name}")
raise typer.Exit(1)
target_cat = next((c for c in config.categories if c.name == target), None)
target_cat = catalog.get_category(target)
if not target_cat:
console.print(f"[red]Ungültige Zielkategorie:[/] {target}")
raise typer.Exit(1)
source_path = base / source_cat.subdir / repo_name
destination_path = base / target_cat.subdir / repo_name
if not source_path.exists():
console.print(f"[red]Quell-Repository existiert nicht:[/] {source_path}")
raise typer.Exit(1)
if destination_path.exists():
console.print(f"[red]Zielordner existiert bereits:[/] {destination_path}")
repo = source_cat.find_repo(repo_name)
if not repo:
console.print(f"[red]Repository '{repo_name}' nicht gefunden in Kategorie '{source_cat_name}'.[/]")
raise typer.Exit(1)
try:
shutil.move(str(source_path), str(destination_path))
console.print(f"[green]✓ Erfolgreich verschoben:[/] {source_cat.name}/{repo_name}{target}")
repo.move_to(target_cat.path)
console.print(f"[green]✓ Erfolgreich verschoben:[/] {source_cat_name}/{repo_name}{target}")
except FileExistsError as e:
console.print(f"[red]{e}[/]")
raise typer.Exit(1)
except Exception as e:
console.print(f"[red]Fehler beim Verschieben:[/] {e}")
raise typer.Exit(1)
raise typer.Exit(1)