Main entry point for PreToolUse hook.
()
| 33 | |
| 34 | |
| 35 | def main(): |
| 36 | """Main entry point for PreToolUse hook.""" |
| 37 | try: |
| 38 | # Read input from stdin |
| 39 | input_data = json.load(sys.stdin) |
| 40 | |
| 41 | # Determine event type for filtering |
| 42 | # For PreToolUse, we use tool_name to determine "bash" vs "file" event |
| 43 | tool_name = input_data.get('tool_name', '') |
| 44 | |
| 45 | event = None |
| 46 | if tool_name == 'Bash': |
| 47 | event = 'bash' |
| 48 | elif tool_name in ['Edit', 'Write', 'MultiEdit']: |
| 49 | event = 'file' |
| 50 | |
| 51 | # Load rules |
| 52 | rules = load_rules(event=event) |
| 53 | |
| 54 | # Evaluate rules |
| 55 | engine = RuleEngine() |
| 56 | result = engine.evaluate_rules(rules, input_data) |
| 57 | |
| 58 | # Always output JSON (even if empty) |
| 59 | print(json.dumps(result), file=sys.stdout) |
| 60 | |
| 61 | except Exception as e: |
| 62 | # On any error, allow the operation and log |
| 63 | error_output = { |
| 64 | "systemMessage": f"Hookify error: {str(e)}" |
| 65 | } |
| 66 | print(json.dumps(error_output), file=sys.stdout) |
| 67 | |
| 68 | finally: |
| 69 | # ALWAYS exit 0 - never block operations due to hook errors |
| 70 | sys.exit(0) |
| 71 | |
| 72 | |
| 73 | if __name__ == '__main__': |
no test coverage detected