✨ feat(cli): parse toml config and dispatch actions
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import tomllib
|
||||
|
||||
ORDER = ["vendor", "sync_templates", "sync_standards", "install_skills", "format_md"]
|
||||
|
||||
|
||||
def usage() -> str:
|
||||
return "Usage:\n python scripts/playbook.py -config <path>\n python scripts/playbook.py -h"
|
||||
|
||||
|
||||
def load_config(path: Path) -> dict:
|
||||
return tomllib.loads(path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def run_action(name: str, config: dict, context: dict) -> int:
|
||||
_ = config, context
|
||||
print(f"[action] {name}")
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
if "-h" in argv or "-help" in argv:
|
||||
print(usage())
|
||||
@@ -13,6 +28,31 @@ def main(argv: list[str]) -> int:
|
||||
if "-config" not in argv:
|
||||
print("ERROR: -config is required.\n" + usage(), file=sys.stderr)
|
||||
return 2
|
||||
idx = argv.index("-config")
|
||||
if idx + 1 >= len(argv) or not argv[idx + 1]:
|
||||
print("ERROR: -config requires a path.\n" + usage(), file=sys.stderr)
|
||||
return 2
|
||||
|
||||
config_path = Path(argv[idx + 1]).expanduser()
|
||||
if not config_path.is_file():
|
||||
print(f"ERROR: config not found: {config_path}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
config = load_config(config_path)
|
||||
playbook_config = config.get("playbook", {})
|
||||
project_root = playbook_config.get("project_root")
|
||||
if project_root:
|
||||
root = Path(project_root).expanduser()
|
||||
else:
|
||||
root = config_path.parent
|
||||
context = {"project_root": root.resolve(), "config_path": config_path.resolve()}
|
||||
|
||||
for name in ORDER:
|
||||
if name in config:
|
||||
result = run_action(name, config[name], context)
|
||||
if result != 0:
|
||||
return result
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user