updated version of the script, fix bug that was deleted bracket

This commit is contained in:
Lukas Greve
2025-10-22 22:29:20 +02:00
parent 5985b0f353
commit 15dff2b43e

View File

@@ -92,11 +92,9 @@ for file in $MAIN_TF_FILES; do
if [ "$REVERT_MODE" = true ]; then if [ "$REVERT_MODE" = true ]; then
# Revert operation: change file:// back to original https:// URLs # Revert operation: change file:// back to original https:// URLs
# Create a temporary file to avoid read/write race condition
temp_file=$(mktemp) temp_file=$(mktemp)
# Process the file line by line to avoid reading/writing the same file while IFS= read -r line || [[ -n "$line" ]]; do
while IFS= read -r line; do
if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then
# Extract local path from the file:// URL # Extract local path from the file:// URL
local_file_path="${BASH_REMATCH[1]}" local_file_path="${BASH_REMATCH[1]}"
@@ -109,8 +107,9 @@ for file in $MAIN_TF_FILES; do
echo " Found matching file: $local_filename" echo " Found matching file: $local_filename"
if [ "$DRY_RUN" = false ]; then if [ "$DRY_RUN" = false ]; then
# Replace the line in temporary file # Use precise string replacement to avoid corrupting the file
sed -e "s|image_location.*=.*\"file://$local_file_path\"|image_location = \"$original_url\"|" <<< "$line" >> "$temp_file" new_line="${line/\"file:\/\/$local_file_path\"/\"$original_url\"}"
echo "$new_line" >> "$temp_file"
echo " Reverted to original URL: $original_url" echo " Reverted to original URL: $original_url"
else else
echo " Would revert to: $original_url" echo " Would revert to: $original_url"
@@ -121,7 +120,6 @@ for file in $MAIN_TF_FILES; do
fi fi
done done
# If we didn't find a match, still copy the original line
if [ "$found_match" = false ]; then if [ "$found_match" = false ]; then
echo " Warning: No matching original URL found for $local_filename" echo " Warning: No matching original URL found for $local_filename"
echo "$line" >> "$temp_file" echo "$line" >> "$temp_file"
@@ -132,7 +130,6 @@ for file in $MAIN_TF_FILES; do
fi fi
done < "$file" done < "$file"
# If not in dry run mode, replace the original file with the temporary file
if [ "$DRY_RUN" = false ]; then if [ "$DRY_RUN" = false ]; then
mv "$temp_file" "$file" mv "$temp_file" "$file"
else else
@@ -143,8 +140,7 @@ for file in $MAIN_TF_FILES; do
# Normal operation: convert remote URLs to local paths # Normal operation: convert remote URLs to local paths
temp_file=$(mktemp) temp_file=$(mktemp)
# Process the file line by line to avoid reading/writing the same file while IFS= read -r line || [[ -n "$line" ]]; do
while IFS= read -r line; do
if [[ "$line" =~ .*image_location.*=.*\"(https://.*)\".* ]]; then if [[ "$line" =~ .*image_location.*=.*\"(https://.*)\".* ]]; then
remote_url="${BASH_REMATCH[1]}" remote_url="${BASH_REMATCH[1]}"
filename=$(basename "$remote_url") filename=$(basename "$remote_url")
@@ -156,8 +152,9 @@ for file in $MAIN_TF_FILES; do
echo " Found local image: $filename" echo " Found local image: $filename"
if [ "$DRY_RUN" = false ]; then if [ "$DRY_RUN" = false ]; then
# Replace the line in temporary file # Use precise string replacement to avoid corrupting the file
sed -e "s|image_location.*=.*\"$remote_url\"|image_location = \"file://$local_path\"|" <<< "$line" >> "$temp_file" new_line="${line/\"$remote_url\"/\"file://$local_path\"}"
echo "$new_line" >> "$temp_file"
echo " Updated to: file://$local_path" echo " Updated to: file://$local_path"
else else
echo " Would update to: file://$local_path" echo " Would update to: file://$local_path"
@@ -173,18 +170,11 @@ for file in $MAIN_TF_FILES; do
fi fi
done < "$file" done < "$file"
# If not in dry run mode, replace the original file with the temporary file
if [ "$DRY_RUN" = false ]; then if [ "$DRY_RUN" = false ]; then
mv "$temp_file" "$file" mv "$temp_file" "$file"
else else
rm "$temp_file" rm "$temp_file"
fi fi
if [ "$DRY_RUN" = false ] && [ -f "$temp_file" ]; then
echo " Updated $file"
elif [ "$DRY_RUN" = true ]; then
echo " Would update $file (dry run)"
fi
fi fi
done done