-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-s3-backend.sh
More file actions
executable file
·80 lines (70 loc) · 2.21 KB
/
remove-s3-backend.sh
File metadata and controls
executable file
·80 lines (70 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
set -euo pipefail
# Remove Terraform backend "s3" blocks from all main.tf files under the repo
script_dir="$(cd "$(dirname "$0")" && pwd)"
repo_root_dir="$script_dir"
# Find all main.tf files and process them
changed=0
while IFS= read -r -d '' tf_file; do
tmp_file="$(mktemp)"
awk '
BEGIN { in_backend = 0; depth = 0; pending_backend_open = 0 }
{
original_line = $0
if (in_backend) {
line_copy = original_line
opens = gsub(/\{/, "", line_copy)
closes = gsub(/\}/, "", line_copy)
depth += (opens - closes)
if (depth <= 0) { in_backend = 0 }
next
}
# If previous line matched backend without an opening brace, wait until we see the opening brace
if (pending_backend_open) {
line_copy = original_line
opens = gsub(/\{/, "", line_copy)
closes = gsub(/\}/, "", line_copy)
if (opens > 0) {
in_backend = 1
pending_backend_open = 0
depth = opens - closes
if (depth <= 0) { in_backend = 0 }
next
} else {
next
}
}
# Detect start of backend "s3" block (brace can be on same line)
if (match(original_line, /^[[:space:]]*backend[[:space:]]*"s3"([[:space:]]*\{)?[[:space:]]*$/)) {
in_backend = 1
depth = 0
line_copy = original_line
opens = gsub(/\{/, "", line_copy)
closes = gsub(/\}/, "", line_copy)
depth += (opens - closes)
if (opens == 0) {
# No opening brace on this line: mark pending and do not print this or following lines until block ends
pending_backend_open = 1
in_backend = 0
} else if (depth <= 0) {
in_backend = 0
}
next
}
print original_line
}
' "$tf_file" > "$tmp_file"
if ! cmp -s "$tf_file" "$tmp_file"; then
cp "$tf_file" "$tf_file.bak"
mv "$tmp_file" "$tf_file"
echo "Updated: $tf_file (backup at $tf_file.bak)"
changed=$((changed+1))
else
rm -f "$tmp_file"
fi
done < <(find "$repo_root_dir" -type f -name 'main.tf' -print0)
if [[ $changed -eq 0 ]]; then
echo "No changes were necessary."
else
echo "Done. Updated $changed file(s)."
fi