Files
smom-dbis-138/scripts/automation/add-error-handling.sh

41 lines
1.1 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Add error handling to scripts missing it
# Adds set -euo pipefail to scripts that don't have it
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
count=0
while IFS= read -r -d '' file; do
# Skip if already has error handling
if head -5 "$file" | grep -q "set -"; then
continue
fi
# Skip library files (they may intentionally not have error handling)
if [[ "$file" =~ /lib/ ]]; then
continue
fi
# Find line after shebang
line_num=2
if head -1 "$file" | grep -q "^#!"; then
# Check if line 2 is empty or a comment
if sed -n '2p' "$file" | grep -qE '^[[:space:]]*$|^[[:space:]]*#'; then
# Find first non-empty, non-comment line
line_num=$(awk '/^[^#[:space:]]/ {print NR; exit}' "$file")
fi
fi
# Insert error handling
sed -i "${line_num}i set -euo pipefail" "$file"
((count++)) || true
done < <(find scripts -name "*.sh" -type f -print0)
echo "✅ Added error handling to $count scripts"