2026-05-14 11:32:25 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from .errors import InfospaceError
|
|
|
|
|
from .lifecycle import add_artifact, create_infospace, load_infospace
|
2026-05-14 14:53:16 +02:00
|
|
|
from .markdown_adapter import validate_infospace_artifacts
|
2026-05-14 11:32:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
|
|
|
parser = argparse.ArgumentParser(prog="infospace-bench")
|
|
|
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
|
|
|
|
|
|
|
|
create = sub.add_parser("create", help="Create an infospace")
|
|
|
|
|
create.add_argument("workspace")
|
|
|
|
|
create.add_argument("slug")
|
|
|
|
|
create.add_argument("--name", required=True)
|
|
|
|
|
create.add_argument("--topic-domain", default="")
|
|
|
|
|
|
|
|
|
|
inspect = sub.add_parser("inspect", help="Inspect an infospace")
|
|
|
|
|
inspect.add_argument("root")
|
|
|
|
|
|
|
|
|
|
add = sub.add_parser("add-artifact", help="Add an artifact to an infospace")
|
|
|
|
|
add.add_argument("root")
|
|
|
|
|
add.add_argument("source")
|
|
|
|
|
add.add_argument("--kind", required=True)
|
|
|
|
|
add.add_argument("--title", default="")
|
|
|
|
|
|
|
|
|
|
export = sub.add_parser("export", help="Print the infospace representation")
|
|
|
|
|
export.add_argument("root")
|
|
|
|
|
|
2026-05-14 14:53:16 +02:00
|
|
|
validate = sub.add_parser("validate", help="Validate infospace artifacts")
|
|
|
|
|
validate.add_argument("root")
|
|
|
|
|
|
2026-05-14 11:32:25 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
|
|
|
parser = build_parser()
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
try:
|
|
|
|
|
if args.command == "create":
|
|
|
|
|
infospace = create_infospace(
|
|
|
|
|
Path(args.workspace),
|
|
|
|
|
args.slug,
|
|
|
|
|
name=args.name,
|
|
|
|
|
topic_domain=args.topic_domain,
|
|
|
|
|
)
|
|
|
|
|
_write_json({"slug": infospace.config.slug, "root": str(infospace.root)})
|
|
|
|
|
elif args.command == "inspect":
|
|
|
|
|
_write_json(load_infospace(Path(args.root)).to_dict())
|
|
|
|
|
elif args.command == "add-artifact":
|
|
|
|
|
artifact = add_artifact(
|
|
|
|
|
Path(args.root),
|
|
|
|
|
Path(args.source),
|
|
|
|
|
kind=args.kind,
|
|
|
|
|
title=args.title,
|
|
|
|
|
)
|
|
|
|
|
_write_json({"artifact": artifact.to_dict()})
|
|
|
|
|
elif args.command == "export":
|
|
|
|
|
_write_json(load_infospace(Path(args.root)).to_dict())
|
2026-05-14 14:53:16 +02:00
|
|
|
elif args.command == "validate":
|
|
|
|
|
results = validate_infospace_artifacts(Path(args.root))
|
|
|
|
|
valid = all(result.valid for result in results)
|
|
|
|
|
_write_json(
|
|
|
|
|
{
|
|
|
|
|
"valid": valid,
|
|
|
|
|
"results": [result.to_dict() for result in results],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return 0 if valid else 1
|
2026-05-14 11:32:25 +02:00
|
|
|
else:
|
|
|
|
|
parser.error(f"Unhandled command: {args.command}")
|
|
|
|
|
except InfospaceError as exc:
|
|
|
|
|
print(json.dumps(exc.to_dict(), indent=2), file=sys.stderr)
|
|
|
|
|
return 2
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _write_json(payload: dict) -> None:
|
|
|
|
|
print(json.dumps(payload, indent=2))
|