Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/codeguard
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"
;;
*)
Expand Down
1 change: 1 addition & 0 deletions scripts/check_architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions scripts/guard_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Expand Down
70 changes: 70 additions & 0 deletions scripts/install_cli.py
Original file line number Diff line number Diff line change
@@ -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:]))
Loading