Files
computer-playbook/cli/create/inventory/role_resolver.py
T
kevinveenbirkenbach a354992f27 refactor(create-inventory): modularize CLI, stabilize path handling, add unit tests
- Split create-inventory logic into focused modules (cli, project, filters, host_vars, credentials, etc.)
- Replace fragile path construction with python -m module execution
- Add robust project root detection
- Centralize password generation
- Improve test coverage with per-module unit tests
- Preserve existing inventory and host_vars behavior

https://chatgpt.com/share/69471157-79c0-800f-8dec-25c0f8e38f03
2025-12-20 22:12:44 +01:00

55 lines
1.3 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
from typing import Dict, Optional
from .subprocess_runner import run_subprocess
def resolve_role_path(
application_id: str,
roles_dir: Path,
project_root: Path,
env: Optional[Dict[str, str]],
) -> Optional[Path]:
"""
Resolve role path by calling:
python -m cli.meta.applications.role_name <app_id> -r <roles_dir>
The helper may print:
- bare folder name (e.g. web-app-nextcloud)
- relative path (e.g. roles/web-app-nextcloud)
- absolute path
"""
cmd = [
sys.executable,
"-m",
"cli.meta.applications.role_name",
application_id,
"-r",
str(roles_dir),
]
result = run_subprocess(cmd, capture_output=True, env=env)
raw = (result.stdout or "").strip()
if not raw:
return None
printed = Path(raw)
if printed.is_absolute():
role_path = printed
else:
cand1 = roles_dir / printed
if cand1.exists():
role_path = cand1
else:
cand2 = project_root / printed
if cand2.exists():
role_path = cand2
else:
return None
return role_path if role_path.exists() else None