Files
proxmox/scripts/update-scripts-to-modules.sh

69 lines
2.3 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Update scripts to use shared modules
# Replaces hardcoded IPs and custom logging with shared modules
# Usage: ./scripts/update-scripts-to-modules.sh [script1.sh] [script2.sh] ...
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$SCRIPT_DIR/lib/logging.sh" 2>/dev/null || true
# If scripts provided, update those; otherwise find scripts needing updates
if [ $# -gt 0 ]; then
SCRIPTS_TO_UPDATE=("$@")
else
# Find scripts with hardcoded IPs that don't use modules
SCRIPTS_TO_UPDATE=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" ! -path "*/lib/*" -exec sh -c 'file="$1"; if grep -q "192\.168\.11\." "$file" 2>/dev/null && ! grep -q "source.*lib/ip-config\|source.*ip-addresses.conf" "$file" 2>/dev/null && head -5 "$file" | grep -q "^#!/"; then echo "$file"; fi' _ {} \; | head -50)
fi
log_header "Updating Scripts to Use Modules"
updated=0
skipped=0
for script in $SCRIPTS_TO_UPDATE; do
if [ ! -f "$script" ]; then
continue
fi
# Skip if already using modules
if grep -q "source.*lib/ip-config\|source.*ip-addresses.conf" "$script" 2>/dev/null; then
skipped=$((skipped + 1))
continue
fi
log_info "Updating: $script"
# Create backup
cp "$script" "${script}.bak"
# Add module loading after set -euo pipefail or after shebang
if grep -q "^set -euo pipefail\|^set -uo pipefail\|^set -eo pipefail" "$script"; then
# Add after set -euo pipefail
sed -i '/^set -[euo]* pipefail/a\
\
# Load shared modules\
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\
source "$SCRIPT_DIR/lib/ip-config.sh" 2>/dev/null || true\
source "$SCRIPT_DIR/lib/logging.sh" 2>/dev/null || true\
' "$script"
elif grep -q "^#!/" "$script"; then
# Add after shebang, before any other code
sed -i '1a\
\
# Load shared modules\
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\
source "$SCRIPT_DIR/lib/ip-config.sh" 2>/dev/null || true\
source "$SCRIPT_DIR/lib/logging.sh" 2>/dev/null || true\
' "$script"
fi
updated=$((updated + 1))
log_success "Updated: $script"
done
log_success "Update complete!"
echo " Updated: $updated"
echo " Skipped: $skipped"