From 15dff2b43edb861a6f7aeadbb21ee2375457b5fa Mon Sep 17 00:00:00 2001 From: Lukas Greve Date: Wed, 22 Oct 2025 22:29:20 +0200 Subject: [PATCH] updated version of the script, fix bug that was deleted bracket --- update_image_locations.sh | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/update_image_locations.sh b/update_image_locations.sh index 903e7d0..95d6670 100755 --- a/update_image_locations.sh +++ b/update_image_locations.sh @@ -92,11 +92,9 @@ for file in $MAIN_TF_FILES; do if [ "$REVERT_MODE" = true ]; then # Revert operation: change file:// back to original https:// URLs - # Create a temporary file to avoid read/write race condition temp_file=$(mktemp) - # Process the file line by line to avoid reading/writing the same file - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then # Extract local path from the file:// URL local_file_path="${BASH_REMATCH[1]}" @@ -109,8 +107,9 @@ for file in $MAIN_TF_FILES; do echo " Found matching file: $local_filename" if [ "$DRY_RUN" = false ]; then - # Replace the line in temporary file - sed -e "s|image_location.*=.*\"file://$local_file_path\"|image_location = \"$original_url\"|" <<< "$line" >> "$temp_file" + # Use precise string replacement to avoid corrupting the file + new_line="${line/\"file:\/\/$local_file_path\"/\"$original_url\"}" + echo "$new_line" >> "$temp_file" echo " Reverted to original URL: $original_url" else echo " Would revert to: $original_url" @@ -121,7 +120,6 @@ for file in $MAIN_TF_FILES; do fi done - # If we didn't find a match, still copy the original line if [ "$found_match" = false ]; then echo " Warning: No matching original URL found for $local_filename" echo "$line" >> "$temp_file" @@ -132,7 +130,6 @@ for file in $MAIN_TF_FILES; do fi done < "$file" - # If not in dry run mode, replace the original file with the temporary file if [ "$DRY_RUN" = false ]; then mv "$temp_file" "$file" else @@ -143,8 +140,7 @@ for file in $MAIN_TF_FILES; do # Normal operation: convert remote URLs to local paths temp_file=$(mktemp) - # Process the file line by line to avoid reading/writing the same file - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$line" =~ .*image_location.*=.*\"(https://.*)\".* ]]; then remote_url="${BASH_REMATCH[1]}" filename=$(basename "$remote_url") @@ -156,8 +152,9 @@ for file in $MAIN_TF_FILES; do echo " Found local image: $filename" if [ "$DRY_RUN" = false ]; then - # Replace the line in temporary file - sed -e "s|image_location.*=.*\"$remote_url\"|image_location = \"file://$local_path\"|" <<< "$line" >> "$temp_file" + # Use precise string replacement to avoid corrupting the file + new_line="${line/\"$remote_url\"/\"file://$local_path\"}" + echo "$new_line" >> "$temp_file" echo " Updated to: file://$local_path" else echo " Would update to: file://$local_path" @@ -173,18 +170,11 @@ for file in $MAIN_TF_FILES; do fi done < "$file" - # If not in dry run mode, replace the original file with the temporary file if [ "$DRY_RUN" = false ]; then mv "$temp_file" "$file" else rm "$temp_file" 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 done