Skip to content

Commit 45bb43a

Browse files
loong10kPartMe AI
andauthored
feat: add codeguard explain command (#113)
Co-authored-by: PartMe AI <[email protected]>
1 parent ef587a5 commit 45bb43a

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎bin/codeguard‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ case "$cmd" in
4646
init|version|discover|doctor|status|report|hooks|impact|install|uninstall|upgrade|completion)
4747
exec python3 "$ROOT/scripts/guard_cli.py" "$cmd" "$@"
4848
;;
49+
explain)
50+
exec python3 "$ROOT/scripts/explain_cli.py" "$@"
51+
;;
4952
*)
5053
echo "codeguard: 未知子命令 '$cmd'" >&2
5154
echo "可用: check | fix | cve | dockerfile | detect | java-plan | engine | init" >&2

‎scripts/check_architecture.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"guard_cli.py": "entry",
2424
"hooks_cli.py": "entry",
2525
"install_cli.py": "entry",
26+
"explain_cli.py": "entry",
2627
"engine_report.py": "entry",
2728
"fix.py": "entry",
2829
"gen_language_docs.py": "entry",

‎scripts/explain_cli.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Explain a CodeGuard language rule or the last check log."""
2+
from __future__ import annotations
3+
4+
import json
5+
import sys
6+
from pathlib import Path
7+
8+
ROOT = Path(__file__).resolve().parents[1]
9+
10+
11+
def result(decision, **extra):
12+
code = {"allow": 0, "deny": 1, "error": 2, "unverified": 3}.get(decision, 4)
13+
return {"schemaVersion": "partme.guard/v1", "tool": "codeguard", "command": "codeguard.explain",
14+
"decision": decision, "exitCode": code, "findings": [], "evidence": {}, **extra}
15+
16+
17+
def main(argv):
18+
languages = json.loads((ROOT / "scripts" / "languages.json").read_text())["languages"]
19+
if "--last" in argv:
20+
log = Path.cwd() / "out" / ".codeguard-last.log"
21+
text = log.read_text(errors="replace")[-4000:] if log.is_file() else ""
22+
payload = result("allow" if text else "unverified", log=str(log), tail=text)
23+
print(json.dumps(payload, ensure_ascii=False, indent=2))
24+
return payload["exitCode"]
25+
subject = next((a for a in argv if not a.startswith("-")), "")
26+
if not subject:
27+
payload = result("allow", subjects=[{"id": x["id"], "name": x["name"], "status": x["status"]} for x in languages])
28+
print(json.dumps(payload, ensure_ascii=False, indent=2))
29+
return 0
30+
item = next((x for x in languages if x["id"] == subject or x["name"].lower() == subject.lower()), None)
31+
if item is None:
32+
payload = result("unverified", findings=[{"code": "UNKNOWN_SUBJECT", "severity": "warning"}], subject=subject)
33+
print(json.dumps(payload, ensure_ascii=False, indent=2))
34+
return 3
35+
payload = result("allow", subject=item["id"], language={
36+
"name": item["name"], "status": item["status"], "lint": item.get("lint"),
37+
"format": item.get("format"), "installHint": item.get("install_hint"),
38+
"markers": item.get("markers", []), "configFiles": item.get("linter_config_files", [])})
39+
print(json.dumps(payload, ensure_ascii=False, indent=2))
40+
return 0
41+
42+
43+
if __name__ == "__main__":
44+
raise SystemExit(main(sys.argv[1:]))

0 commit comments

Comments
 (0)