-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
executable file
·112 lines (96 loc) · 3.47 KB
/
Copy pathvalidate.py
File metadata and controls
executable file
·112 lines (96 loc) · 3.47 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
"""
Validate Docker Compose stacks against project standards.
Usage:
./scripts/validate.py # Validate all stacks, JSON output
./scripts/validate.py --human # Human-readable output
./scripts/validate.py graylog # Validate specific stack
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))
from lib.discovery import find_project_root, resolve_stack
from lib.compose import parse_compose_file
from lib.validator import validate_stack, validate_all_stacks, get_validation_summary
from lib.report import generate_validation_report, HumanReporter
from lib.models import to_json
def main() -> int:
parser = argparse.ArgumentParser(
description="Validate Docker Compose stacks against project standards",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s Validate all stacks (JSON output)
%(prog)s --human Human-readable output
%(prog)s graylog Validate specific stack
%(prog)s --summary Show only summary
""",
)
parser.add_argument(
"stack",
nargs="?",
help="Stack name to validate (validates all if not specified)",
)
parser.add_argument(
"--human",
action="store_true",
help="Output human-readable format instead of JSON",
)
parser.add_argument(
"--summary",
action="store_true",
help="Output only summary (JSON mode)",
)
parser.add_argument(
"--no-docker",
action="store_true",
help="Skip docker compose config validation (faster, less thorough)",
)
parser.add_argument(
"--root",
type=Path,
help="Project root directory (auto-detected if not specified)",
)
args = parser.parse_args()
# Determine project root
root = args.root or find_project_root()
try:
if args.stack:
# Validate specific stack
resolved = resolve_stack(args.stack, root)
if not resolved:
if args.human:
print(f"Error: Stack not found: {args.stack}", file=sys.stderr)
else:
print(to_json({"error": f"Stack not found: {args.stack}"}))
return 1
stack_name, stack_path = resolved
stack = parse_compose_file(stack_path / "docker-compose.yml")
stack.name = stack_name
validations = [validate_stack(stack, use_docker_validate=not args.no_docker)]
else:
# Validate all stacks
validations = validate_all_stacks(root)
# Generate output
if args.human:
reporter = HumanReporter()
reporter.report_validations(validations)
elif args.summary:
summary = get_validation_summary(validations)
print(to_json(summary))
else:
generate_validation_report(validations, output_json=True, file=sys.stdout)
# Exit code based on errors
total_errors = sum(v.errors for v in validations)
return 1 if total_errors > 0 else 0
except Exception as e:
if args.human:
print(f"Error: {e}", file=sys.stderr)
else:
print(to_json({"error": str(e)}))
return 1
if __name__ == "__main__":
sys.exit(main())