diff --git a/implementation/auditing/audit_engine.py b/implementation/auditing/audit_engine.py index d416fde..b2ff25a 100644 --- a/implementation/auditing/audit_engine.py +++ b/implementation/auditing/audit_engine.py @@ -37,7 +37,7 @@ SEVERITY_ENHANCEMENT = "enhancement" # 🟩 def _safe_get(data, *keys, default=None): - """Nested dict access without KeyError.""" + """Nested dict access without KeyError. Last positional arg is default if not a key.""" if not keys: return default cur = data @@ -779,13 +779,23 @@ def to_markdown(findings, summary, contract): def main(): if len(sys.argv) < 2: - print("Usage: audit_engine.py [--json] [--md]") + print("Usage: audit_engine.py [--json] [--md] [--output-dir DIR]") sys.exit(1) filepath = sys.argv[1] output_json = "--json" in sys.argv output_md = "--md" in sys.argv + # --output-dir: write results to directory instead of stdout + output_dir = None + i = 2 + while i < len(sys.argv): + if sys.argv[i] == "--output-dir" and i + 1 < len(sys.argv): + output_dir = sys.argv[i + 1] + i += 2 + continue + i += 1 + if not os.path.exists(filepath): print(f"Error: {filepath} not found") sys.exit(1) @@ -796,28 +806,33 @@ def main(): findings = analyze(contract) summary = summarize(findings, contract) - if output_md: - md = to_markdown(findings, summary, contract) - print(md) - elif output_json: - result = { - "audit_engine": "v1", - "timestamp": datetime.now(timezone.utc).isoformat(), - "business": _safe_get(contract, "name"), - "summary": summary, - "findings": findings, - } - print(json.dumps(result, indent=2)) + result = { + "audit_engine": "v1", + "timestamp": datetime.now(timezone.utc).isoformat(), + "business": _safe_get(contract, "name"), + "summary": summary, + "findings": findings, + } + + if output_dir: + os.makedirs(output_dir, exist_ok=True) + json_path = os.path.join(output_dir, "findings.json") + with open(json_path, "w") as f: + json.dump(result, f, indent=2) + print(f"Findings written to {json_path}", file=sys.stderr) + + if output_md: + md = to_markdown(findings, summary, contract) + md_path = os.path.join(output_dir, "findings.md") + with open(md_path, "w") as f: + f.write(md) + print(f"Report written to {md_path}", file=sys.stderr) else: - # Default: JSON - result = { - "audit_engine": "v1", - "timestamp": datetime.now(timezone.utc).isoformat(), - "business": _safe_get(contract, "name"), - "summary": summary, - "findings": findings, - } - print(json.dumps(result, indent=2)) + if output_md: + md = to_markdown(findings, summary, contract) + print(md) + else: + print(json.dumps(result, indent=2)) if __name__ == "__main__":