updated version of the script, fix bug that was deleted bracket
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user