-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfile-logger.sh
More file actions
executable file
·98 lines (82 loc) · 2.67 KB
/
file-logger.sh
File metadata and controls
executable file
·98 lines (82 loc) · 2.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
set -euo pipefail
# Claude Code Hook: File Logger
#
# Purpose: Simple demonstration of hook functionality
# Trigger: PreToolUse for Edit, Write, MultiEdit tools
# Blocking: No - just logs activity
#
# This hook demonstrates basic hook functionality by logging file operations
##################################
# Configuration
##################################
HOOK_NAME="file-logger"
LOG_FILE="$HOME/.claude/logs/file-logger.log"
# Ensure log directory exists with secure permissions
mkdir -p "$(dirname "$LOG_FILE")"
chmod 700 "$(dirname "$LOG_FILE")"
# Create log file with restrictive permissions if it doesn't exist
touch "$LOG_FILE"
chmod 600 "$LOG_FILE"
##################################
# Logging Functions
##################################
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [$HOOK_NAME] $*" | tee -a "$LOG_FILE"
}
##################################
# Dependency Validation
##################################
validate_hook_dependencies() {
local deps=("wc" "file")
local missing=()
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
missing+=("$dep")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
log "ERROR: Missing required dependencies: ${missing[*]}"
echo "Install missing tools and retry"
exit 1
fi
}
##################################
# Main Hook Logic
##################################
main() {
# Validate dependencies first
validate_hook_dependencies
local tool_name="${CLAUDE_TOOL:-unknown}"
local file_path="${CLAUDE_FILE:-unknown}"
log "Hook triggered!"
log "Tool: $tool_name"
log "File: $file_path"
# Only process file modification tools
case "$tool_name" in
"Edit"|"Write"|"MultiEdit")
log "Processing file modification tool: $tool_name"
;;
*)
log "Skipping non-file tool: $tool_name"
exit 0
;;
esac
# Get basic file info if file exists
if [[ -n "$file_path" ]] && [[ "$file_path" != "unknown" ]] && [[ -f "$file_path" ]]; then
local file_size=$(wc -c < "$file_path" 2>/dev/null || echo "0")
local file_lines=$(wc -l < "$file_path" 2>/dev/null || echo "0")
log "File size: $file_size bytes"
log "File lines: $file_lines"
log "File type: $(file -b "$file_path" 2>/dev/null || echo "unknown")"
else
log "File does not exist yet or path unknown"
fi
# Always allow the operation to proceed
log "Operation allowed - no blocking behavior"
exit 0
}
##################################
# Execute Main Function
##################################
main "$@"