|
| 1 | +# Claude Code management recipes |
| 2 | + |
| 3 | +claude_settings_file := ".claude/settings.local.json" |
| 4 | + |
| 5 | +# Sort Claude Code permissions in canonical order |
| 6 | +[group('Claude Code')] |
| 7 | +claude_permissions_sort: |
| 8 | + #!/usr/bin/env bash |
| 9 | + set -euo pipefail # strict mode without tracing |
| 10 | +
|
| 11 | + BACKUP_FILE=".claude/settings.local.json.backup" |
| 12 | +
|
| 13 | + echo "{{BLUE}}Sorting Claude Code permissions...{{NORMAL}}" |
| 14 | +
|
| 15 | + # Check if file exists |
| 16 | + if [[ ! -f "{{ claude_settings_file }}" ]]; then |
| 17 | + echo "{{RED}}Error: {{ claude_settings_file }} not found{{NORMAL}}" |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | +
|
| 21 | + # Validate input JSON |
| 22 | + if ! jq empty "{{ claude_settings_file }}" 2>/dev/null; then |
| 23 | + echo "{{RED}}Error: {{ claude_settings_file }} is not valid JSON{{NORMAL}}" |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | +
|
| 27 | + # Create backup |
| 28 | + cp "{{ claude_settings_file }}" "$BACKUP_FILE" |
| 29 | + echo "{{YELLOW}}Created backup: $BACKUP_FILE{{NORMAL}}" |
| 30 | +
|
| 31 | + # Sort permissions with grouping strategy |
| 32 | + # Group 1: Bash (most commonly modified) |
| 33 | + # Group 2: WebFetch (domain restrictions) |
| 34 | + # Group 3: WebSearch (standalone) |
| 35 | + # Group 4: Other (future-proof) |
| 36 | + if jq ' |
| 37 | + .permissions.allow |= ( |
| 38 | + map( |
| 39 | + if startswith("Bash(") then {type: "1-Bash", perm: .} |
| 40 | + elif startswith("WebFetch(") then {type: "2-WebFetch", perm: .} |
| 41 | + elif startswith("WebSearch") then {type: "3-WebSearch", perm: .} |
| 42 | + else {type: "9-Other", perm: .} |
| 43 | + end |
| 44 | + ) | |
| 45 | + sort_by([.type, .perm]) | |
| 46 | + map(.perm) |
| 47 | + ) | |
| 48 | + .permissions.deny |= sort | |
| 49 | + .permissions.ask |= sort |
| 50 | + ' "$BACKUP_FILE" > "{{ claude_settings_file }}"; then |
| 51 | +
|
| 52 | + # Validate output JSON |
| 53 | + if ! jq empty "{{ claude_settings_file }}" 2>/dev/null; then |
| 54 | + echo "{{RED}}Error: Sorting produced invalid JSON, restoring from backup{{NORMAL}}" |
| 55 | + cp "$BACKUP_FILE" "{{ claude_settings_file }}" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +
|
| 59 | + # Remove backup on success |
| 60 | + rm "$BACKUP_FILE" |
| 61 | +
|
| 62 | + # Show permission counts |
| 63 | + ALLOW_COUNT=$(jq '.permissions.allow | length' "{{ claude_settings_file }}") |
| 64 | + DENY_COUNT=$(jq '.permissions.deny | length' "{{ claude_settings_file }}") |
| 65 | + ASK_COUNT=$(jq '.permissions.ask | length' "{{ claude_settings_file }}") |
| 66 | +
|
| 67 | + echo "{{GREEN}}Permissions sorted successfully!{{NORMAL}}" |
| 68 | + echo "{{BLUE}}Allow: $ALLOW_COUNT | Deny: $DENY_COUNT | Ask: $ASK_COUNT{{NORMAL}}" |
| 69 | + else |
| 70 | + echo "{{RED}}Error: jq sorting failed, restoring from backup{{NORMAL}}" |
| 71 | + cp "$BACKUP_FILE" "{{ claude_settings_file }}" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +
|
| 75 | +# Check Claude Code permissions structure |
| 76 | +[group('Claude Code')] |
| 77 | +claude_permissions_check: |
| 78 | + #!/usr/bin/env bash |
| 79 | + set -euo pipefail # strict mode without tracing |
| 80 | +
|
| 81 | + echo "{{BLUE}}Checking Claude Code permissions structure...{{NORMAL}}" |
| 82 | +
|
| 83 | + # Check if file exists |
| 84 | + if [[ ! -f "{{ claude_settings_file }}" ]]; then |
| 85 | + echo "{{RED}}Error: {{ claude_settings_file }} not found{{NORMAL}}" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + # Validate JSON |
| 90 | + if ! jq empty "{{ claude_settings_file }}" 2>/dev/null; then |
| 91 | + echo "{{RED}}Error: {{ claude_settings_file }} is not valid JSON{{NORMAL}}" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +
|
| 95 | + # Check for required structure |
| 96 | + if ! jq -e '.permissions' "{{ claude_settings_file }}" >/dev/null 2>&1; then |
| 97 | + echo "{{RED}}Error: Missing .permissions object{{NORMAL}}" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +
|
| 101 | + if ! jq -e '.permissions.allow' "{{ claude_settings_file }}" >/dev/null 2>&1; then |
| 102 | + echo "{{YELLOW}}Warning: Missing .permissions.allow array{{NORMAL}}" |
| 103 | + fi |
| 104 | +
|
| 105 | + if ! jq -e '.permissions.deny' "{{ claude_settings_file }}" >/dev/null 2>&1; then |
| 106 | + echo "{{YELLOW}}Warning: Missing .permissions.deny array{{NORMAL}}" |
| 107 | + fi |
| 108 | +
|
| 109 | + if ! jq -e '.permissions.ask' "{{ claude_settings_file }}" >/dev/null 2>&1; then |
| 110 | + echo "{{YELLOW}}Warning: Missing .permissions.ask array{{NORMAL}}" |
| 111 | + fi |
| 112 | +
|
| 113 | + # Show permission counts |
| 114 | + ALLOW_COUNT=$(jq '.permissions.allow | length' "{{ claude_settings_file }}" 2>/dev/null || echo "0") |
| 115 | + DENY_COUNT=$(jq '.permissions.deny | length' "{{ claude_settings_file }}" 2>/dev/null || echo "0") |
| 116 | + ASK_COUNT=$(jq '.permissions.ask | length' "{{ claude_settings_file }}" 2>/dev/null || echo "0") |
| 117 | +
|
| 118 | + echo "{{GREEN}}Permissions structure is valid{{NORMAL}}" |
| 119 | + echo "{{BLUE}}Allow: $ALLOW_COUNT | Deny: $DENY_COUNT | Ask: $ASK_COUNT{{NORMAL}}" |
| 120 | +
|
| 121 | + # Show grouping breakdown for allow list |
| 122 | + if [[ $ALLOW_COUNT -gt 0 ]]; then |
| 123 | + echo "" |
| 124 | + echo "{{BLUE}}Permission types in allow list:{{NORMAL}}" |
| 125 | + BASH_COUNT=$(jq '[.permissions.allow[] | select(startswith("Bash("))] | length' "{{ claude_settings_file }}") |
| 126 | + WEBFETCH_COUNT=$(jq '[.permissions.allow[] | select(startswith("WebFetch("))] | length' "{{ claude_settings_file }}") |
| 127 | + WEBSEARCH_COUNT=$(jq '[.permissions.allow[] | select(startswith("WebSearch"))] | length' "{{ claude_settings_file }}") |
| 128 | + OTHER_COUNT=$((ALLOW_COUNT - BASH_COUNT - WEBFETCH_COUNT - WEBSEARCH_COUNT)) |
| 129 | +
|
| 130 | + echo " Bash: $BASH_COUNT" |
| 131 | + echo " WebFetch: $WEBFETCH_COUNT" |
| 132 | + echo " WebSearch: $WEBSEARCH_COUNT" |
| 133 | + if [[ $OTHER_COUNT -gt 0 ]]; then |
| 134 | + echo " Other: $OTHER_COUNT" |
| 135 | + fi |
| 136 | + fi |
0 commit comments