forked from zxdxjtu/claudecode-rule2hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-hooks.py
More file actions
executable file
·131 lines (101 loc) · 4.05 KB
/
validate-hooks.py
File metadata and controls
executable file
·131 lines (101 loc) · 4.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""
Validate the generated hooks.json file
"""
import json
import sys
from pathlib import Path
def validate_hooks_file(file_path):
"""Validate hooks.json file"""
print(f"🔍 Validating file: {file_path}")
print("-" * 50)
# Check if file exists
if not file_path.exists():
print("❌ File does not exist")
return False
# Read and parse JSON
try:
with open(file_path, 'r') as f:
config = json.load(f)
print("✅ JSON format is valid")
except json.JSONDecodeError as e:
print(f"❌ JSON parsing error: {e}")
return False
# Validate structure
if not isinstance(config, dict):
print("❌ Root element must be an object")
return False
if "hooks" not in config:
print("❌ Missing 'hooks' key")
return False
hooks = config["hooks"]
if not isinstance(hooks, dict):
print("❌ 'hooks' must be an object")
return False
# Validate each event type
valid_events = {"PreToolUse", "PostToolUse", "Stop", "Notification"}
hook_count = 0
for event, event_hooks in hooks.items():
if event not in valid_events:
print(f"⚠️ Unknown event type: {event}")
if not isinstance(event_hooks, list):
print(f"❌ Value of {event} must be an array")
return False
print(f"\n📌 {event} ({len(event_hooks)} configurations)")
for i, hook_group in enumerate(event_hooks):
if not isinstance(hook_group, dict):
print(f" ❌ Configuration {i+1} must be an object")
continue
# Check matcher (optional)
matcher = hook_group.get("matcher", "")
if matcher:
print(f" Matcher: {matcher}")
# Check hooks array
if "hooks" not in hook_group:
print(f" ❌ Configuration {i+1} missing 'hooks' array")
continue
hook_list = hook_group["hooks"]
if not isinstance(hook_list, list):
print(f" ❌ 'hooks' must be an array")
continue
for j, hook in enumerate(hook_list):
if not isinstance(hook, dict):
print(f" ❌ Hook {j+1} must be an object")
continue
# Validate hook type and command
hook_type = hook.get("type")
command = hook.get("command")
if hook_type != "command":
print(f" ⚠️ Hook {j+1} type: {hook_type} (expected: command)")
if not command:
print(f" ❌ Hook {j+1} missing command")
else:
print(f" ✅ Command: {command[:50]}{'...' if len(command) > 50 else ''}")
hook_count += 1
print(f"\n📊 Total: {hook_count} hooks")
print("✅ Validation passed" if hook_count > 0 else "⚠️ No valid hooks found")
return True
def display_hooks_summary(file_path):
"""Display hooks summary"""
with open(file_path, 'r') as f:
config = json.load(f)
print("\n📋 Hooks Summary")
print("=" * 50)
for event, event_hooks in config.get("hooks", {}).items():
print(f"\n{event}:")
for hook_group in event_hooks:
matcher = hook_group.get("matcher", "All tools")
for hook in hook_group.get("hooks", []):
command = hook.get("command", "")
print(f" [{matcher}] → {command}")
if __name__ == "__main__":
# Default to check user's hooks.json
hooks_file = Path.home() / ".claude" / "hooks.json"
# Can also specify other files
if len(sys.argv) > 1:
hooks_file = Path(sys.argv[1])
if validate_hooks_file(hooks_file):
display_hooks_summary(hooks_file)
else:
print("\n❌ Validation failed")
sys.exit(1)