refactor(cli): streamline repository status logic
- Replace `RepoCategory` with `RepoCatalogState` for improved abstraction - Simplify repository data processing and remove redundant functions - Enhance code readability and maintainability by consolidating logic
This commit is contained in:
@@ -1,71 +1,45 @@
|
||||
from typer import Typer
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from repocat.config import load_config
|
||||
from repocat.models.config import RepoCategory
|
||||
from repocat.models.catalog import RepoCatalogState
|
||||
|
||||
app = Typer()
|
||||
console = Console()
|
||||
|
||||
|
||||
def get_dir_size(path: Path) -> int:
|
||||
"""Returns size in bytes."""
|
||||
return sum(f.stat().st_size for f in path.rglob("*") if f.is_file())
|
||||
|
||||
|
||||
def summarize_category(category: RepoCategory, base_path: Path):
|
||||
cat_path = base_path / category.subdir
|
||||
if not cat_path.exists():
|
||||
return 0, 0.0, None, None
|
||||
|
||||
now = datetime.now()
|
||||
repo_infos = []
|
||||
|
||||
for repo in cat_path.iterdir():
|
||||
if not repo.is_dir():
|
||||
continue
|
||||
|
||||
size_bytes = get_dir_size(repo)
|
||||
mtime = datetime.fromtimestamp(repo.stat().st_mtime)
|
||||
age_days = (now - mtime).days
|
||||
|
||||
repo_infos.append((repo.name, size_bytes, age_days, mtime))
|
||||
|
||||
if not repo_infos:
|
||||
return 0, 0.0, None, None
|
||||
|
||||
total_size = sum(info[1] for info in repo_infos) / 1024 / 1024
|
||||
avg_age = sum(info[2] for info in repo_infos) // len(repo_infos)
|
||||
|
||||
oldest = max(repo_infos, key=lambda x: x[2]) # größtes Alter
|
||||
|
||||
return len(repo_infos), total_size, avg_age, (oldest[0], oldest[2])
|
||||
|
||||
|
||||
@app.command("status")
|
||||
def status():
|
||||
"""
|
||||
Zeigt eine Übersicht über alle Repository-Kategorien.
|
||||
"""
|
||||
config = load_config()
|
||||
base = config.base_dir
|
||||
catalog = RepoCatalogState.from_config(load_config())
|
||||
|
||||
table = Table(title="Repository-Status")
|
||||
table.add_column("Kategorie")
|
||||
table.add_column("Repos", justify="right")
|
||||
table.add_column("Gesamtgröße", justify="right")
|
||||
table.add_column("Ø Alter", justify="right")
|
||||
table.add_column("Ältestes Repo", justify="left")
|
||||
table.add_column("\u00d8 Alter", justify="right")
|
||||
table.add_column("\u00c4ltestes Repo", justify="left")
|
||||
|
||||
for cat in config.categories:
|
||||
if cat.is_archive:
|
||||
for category in catalog.categories:
|
||||
if category.config.is_archive:
|
||||
continue
|
||||
count, total_size, avg_age, oldest = summarize_category(cat, base)
|
||||
|
||||
repos = category.repos
|
||||
count = len(repos)
|
||||
total_size = category.total_size_mb()
|
||||
|
||||
if not repos:
|
||||
avg_age = None
|
||||
oldest = None
|
||||
else:
|
||||
ages = [repo.age_days for repo in repos]
|
||||
avg_age = sum(ages) // len(ages)
|
||||
oldest_repo = max(repos, key=lambda r: r.age_days)
|
||||
oldest = (oldest_repo.name, oldest_repo.age_days)
|
||||
|
||||
table.add_row(
|
||||
cat.name,
|
||||
category.config.name,
|
||||
str(count),
|
||||
f"{total_size:.1f} MB",
|
||||
f"{avg_age or '–'} d",
|
||||
|
Reference in New Issue
Block a user