mirror of
https://github.com/anthropics/claude-code.git
synced 2026-01-30 04:02:03 +00:00
Script polish
This commit is contained in:
@@ -18,7 +18,7 @@ Make sure to change your path to your actual script.
|
|||||||
"hooks": [
|
"hooks": [
|
||||||
{
|
{
|
||||||
"type": "command",
|
"type": "command",
|
||||||
"command": "python -m ./bash_command_validator_example"
|
"command": "python3 /path/to/claude-code/examples/hooks/bash_command_validator_example.py"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -33,21 +33,21 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
# Define validation rules as a list of (regex pattern, message) tuples
|
# Define validation rules as a list of (regex pattern, message) tuples
|
||||||
VALIDATION_RULES = [
|
_VALIDATION_RULES = [
|
||||||
(
|
(
|
||||||
r"\bgrep\b(?!.*\|)",
|
r"^grep\b(?!.*\|)",
|
||||||
"Use 'rg' (ripgrep) instead of 'grep' for better performance and features",
|
"Use 'rg' (ripgrep) instead of 'grep' for better performance and features",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
r"\bfind\s+\S+\s+-name\b",
|
r"^find\s+\S+\s+-name\b",
|
||||||
"Use 'rg --files | rg pattern' or 'rg --files -g pattern' instead of 'find -name' for better performance",
|
"Use 'rg --files | rg pattern' or 'rg --files -g pattern' instead of 'find -name' for better performance",
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def validate_command(command: str) -> list[str]:
|
def _validate_command(command: str) -> list[str]:
|
||||||
issues = []
|
issues = []
|
||||||
for pattern, message in VALIDATION_RULES:
|
for pattern, message in _VALIDATION_RULES:
|
||||||
if re.search(pattern, command):
|
if re.search(pattern, command):
|
||||||
issues.append(message)
|
issues.append(message)
|
||||||
return issues
|
return issues
|
||||||
@@ -58,6 +58,7 @@ def main():
|
|||||||
input_data = json.load(sys.stdin)
|
input_data = json.load(sys.stdin)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"Error: Invalid JSON input: {e}", file=sys.stderr)
|
print(f"Error: Invalid JSON input: {e}", file=sys.stderr)
|
||||||
|
# Exit code 1 shows stderr to the user but not to Claude
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
tool_name = input_data.get("tool_name", "")
|
tool_name = input_data.get("tool_name", "")
|
||||||
@@ -70,9 +71,7 @@ def main():
|
|||||||
if not command:
|
if not command:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Validate the command
|
issues = _validate_command(command)
|
||||||
issues = validate_command(command)
|
|
||||||
|
|
||||||
if issues:
|
if issues:
|
||||||
for message in issues:
|
for message in issues:
|
||||||
print(f"• {message}", file=sys.stderr)
|
print(f"• {message}", file=sys.stderr)
|
||||||
|
|||||||
Reference in New Issue
Block a user