forked from Skull-boy/agent-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
81 lines (58 loc) · 1.67 KB
/
Copy pathcli.py
File metadata and controls
81 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
import sys
from pathlib import Path
import yaml
from .validator import validate_contract
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="agent-contracts",
description="Validate Agent Contract files.",
)
subparsers = parser.add_subparsers(
dest="command",
required=True,
)
validate_parser = subparsers.add_parser(
"validate",
help="Validate an Agent Contract.",
)
validate_parser.add_argument(
"contract",
type=Path,
help="Path to contract.yaml",
)
return parser
def run_validate(contract_path: Path) -> int:
try:
result = validate_contract(contract_path)
except FileNotFoundError:
print(f"ERROR {contract_path}")
print(" file not found")
return 2
except PermissionError:
print(f"ERROR {contract_path}")
print(" permission denied")
return 2
except yaml.YAMLError as error:
print(f"FAIL {contract_path}")
print(f" invalid YAML: {error}")
return 1
if result.valid:
print(f"PASS {contract_path}")
return 0
print(f"FAIL {contract_path}")
for error in result.errors:
if error.path:
print(f" {error.path}: {error.message}")
else:
print(f" {error.message}")
return 1
def main() -> int:
parser = build_parser()
args = parser.parse_args()
if args.command == "validate":
return run_validate(args.contract)
parser.error(f"unknown command: {args.command}")
return 2
if __name__ == "__main__":
sys.exit(main())