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