diff --git a/bin/codeguard b/bin/codeguard index 94893b5..fb935e3 100755 --- a/bin/codeguard +++ b/bin/codeguard @@ -43,7 +43,7 @@ case "$cmd" in engine) exec python3 "$ROOT/scripts/engine_report.py" "$@" ;; - init|version|discover|doctor|status|report|hooks|impact) + init|version|discover|doctor|status|report|hooks|impact|install|uninstall|upgrade|completion) exec python3 "$ROOT/scripts/guard_cli.py" "$cmd" "$@" ;; *) diff --git a/scripts/check_architecture.py b/scripts/check_architecture.py index e11e685..366d50a 100644 --- a/scripts/check_architecture.py +++ b/scripts/check_architecture.py @@ -22,6 +22,7 @@ "dockerfile_security.py": "entry", "guard_cli.py": "entry", "hooks_cli.py": "entry", + "install_cli.py": "entry", "engine_report.py": "entry", "fix.py": "entry", "gen_language_docs.py": "entry", diff --git a/scripts/guard_cli.py b/scripts/guard_cli.py index 6e8df7f..dde89b6 100644 --- a/scripts/guard_cli.py +++ b/scripts/guard_cli.py @@ -111,6 +111,9 @@ def cmd_impact(args): COMMANDS={"version":cmd_version,"discover":cmd_discover,"doctor":cmd_doctor,"status":cmd_status,"report":cmd_report,"init":cmd_init,"impact":cmd_impact} def main(argv): + if argv and argv[0] in ("install", "uninstall", "upgrade", "completion"): + from install_cli import main as install_main + return install_main(argv) if argv and argv[0] == "hooks": from hooks_cli import main as hooks_main return hooks_main(argv[1:]) diff --git a/scripts/install_cli.py b/scripts/install_cli.py new file mode 100644 index 0000000..d753a05 --- /dev/null +++ b/scripts/install_cli.py @@ -0,0 +1,70 @@ +"""Install the CodeGuard launcher into a user bin directory.""" +from __future__ import annotations + +import json +import os +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SOURCE = ROOT / "bin" / "codeguard" +DEFAULT_BIN = Path.home() / ".local" / "bin" + + +def target(argv): + if "--bin-dir" in argv: + return Path(argv[argv.index("--bin-dir") + 1]).expanduser().resolve() / "codeguard" + return DEFAULT_BIN / "codeguard" + + +def payload(command, decision, **extra): + code = {"allow": 0, "deny": 1, "error": 2, "unverified": 3}.get(decision, 4) + return {"schemaVersion": "partme.guard/v1", "tool": "codeguard", "command": f"codeguard.{command}", + "decision": decision, "exitCode": code, "findings": [], "evidence": {}, **extra} + + +def main(argv): + command = argv[0] if argv else "install" + path = target(argv) + if command == "completion": + shell = argv[1] if len(argv) > 1 else "bash" + if shell not in ("bash", "zsh", "fish"): + print("usage: codeguard completion bash|zsh|fish", file=sys.stderr) + return 2 + commands = "version discover doctor status report init impact check fix cve dockerfile hooks engine completion" + if shell == "fish": + print("complete -c codeguard -f -a '" + commands + "'") + else: + print("# codeguard " + shell + " completion") + print("complete -W '" + commands + "' codeguard" if shell == "bash" + else "compdef '_arguments " + repr(commands) + "' codeguard") + return 0 + if command not in ("install", "uninstall", "upgrade"): + print("usage: codeguard install|uninstall|upgrade [--bin-dir DIR] [--dry-run]", file=sys.stderr) + return 2 + if command == "uninstall": + if not path.is_symlink() or path.resolve() != SOURCE.resolve(): + print(json.dumps(payload("uninstall", "deny", error="not_managed", path=str(path)), ensure_ascii=False)) + return 1 + path.unlink() + print(json.dumps(payload("uninstall", "allow", removed=str(path)), ensure_ascii=False)) + return 0 + if path.exists() and not path.is_symlink(): + print(json.dumps(payload(command, "deny", error="target_exists", path=str(path)), ensure_ascii=False)) + return 1 + if path.is_symlink() and path.resolve() != SOURCE.resolve(): + print(json.dumps(payload(command, "deny", error="target_is_other_symlink", path=str(path)), ensure_ascii=False)) + return 1 + if "--dry-run" in argv: + print(json.dumps(payload(command, "allow", dryRun=True, source=str(SOURCE), target=str(path)), ensure_ascii=False)) + return 0 + path.parent.mkdir(parents=True, exist_ok=True) + if path.is_symlink(): + path.unlink() + os.symlink(SOURCE, path) + print(json.dumps(payload(command, "allow", installed=True, source=str(SOURCE), target=str(path)), ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:]))