-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtftab.sh
More file actions
executable file
·28 lines (23 loc) · 976 Bytes
/
Copy pathtftab.sh
File metadata and controls
executable file
·28 lines (23 loc) · 976 Bytes
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
#!/usr/bin/env bash
file="$1"
temp_file=$(mktemp --suffix=.tf) # Create a temp file with .tf extension
temp_output_file=$(mktemp)
# Copy the content of the original file to the temporary .tf file
cp "$file" "$temp_file"
# Run terraform fmt on the temporary .tf file, capturing output and return code
terraform_output=$(terraform fmt -write=false -list=false "$temp_file")
terraform_return_code=$?
# Check if terraform fmt was successful
if [ $terraform_return_code -eq 0 ]; then
# If successful, process the output and overwrite the original file
echo "$terraform_output" | sed -e'':a'' -e's/^\(\t*\) /\1\t/;ta' >"$temp_output_file" && mv "$temp_output_file" "$file"
else
# If there was an error, print a message and the terraform fmt output, and leave the original file unchanged
echo "Error formatting Terraform file:"
echo "$terraform_output"
rm "$temp_file"
rm "$temp_output_file"
exit $terraform_return_code
fi
# Clean up temporary .tf file
rm "$temp_file"