-
-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(terraform_docs
): Fix non-GNU sed issues, introduced in v1.93.0, as previous fix doesn't work correctly
#708
Conversation
terraform_docs
): Fix non-GNU sed issues, introduced in v1.93.0terraform_docs
): Fix non-GNU sed issues, introduced in v1.93.0, as previous fix doesn't work correctly
hooks/terraform_docs.sh
Outdated
SED_CMD=(sed -i '') | ||
fi | ||
# shellcheck disable=SC2068 | ||
${SED_CMD[@]} -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change: "${SED_CMD[@]}"
and don't disable shellcheck
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, they're both (current and suggested variants) expanded to
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:49: sed -i -e 's/^<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/<!-- BEGIN_TF_DOCS -->/' README.md
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:50: sed -i -e 's/^<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/<!-- END_TF_DOCS -->/' README.md
on Ubuntu. Not to sed -i'' -e
but sed -i -e
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So let's wait till anyone who face original issue confirm that it will work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on as Mac.
I can confirm it works with the quotes, and not without.
Test script:
old_insertion_marker_begin="Some text foo"
insertion_marker_begin="New Text"
set -x
if sed --version > /dev/null 2>&1; then
SED_CMD=(sed -i'')
else
SED_CMD=(sed -i '')
fi
"${SED_CMD[@]}" -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" tt.txt
With quotes:
+ sed --version
+ SED_CMD=(sed -i '')
+ sed -i '' -e 's/^Some text foo$/New Text/' tt.txt
Without quotes:
+ sed --version
+ SED_CMD=(sed -i '')
+ sed -i -e 's/^Some text foo$/New Text/' tt.txt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I replace sed
with gsed
, I get this output:
+ gsed --version
+ SED_CMD=(gsed -i'')
+ gsed -i -e 's/^Some text foo$/New Text/' tt.txt
Which works fine with GNU sed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test this please
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: a76f03bfc4978741f37f79a6b4a86dc72078a78d
hooks:
- id: terraform_docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my suggestion re wrapping array elements in double quotes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, final (I hope) solution
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: 70670818f5d14ba13c268dd366ecfa8374e88f42
hooks:
- id: terraform_docs
README.md
Outdated
@@ -585,8 +585,9 @@ Unlike most other hooks, this hook triggers once if there are any changed files | |||
To migrate everything to `terraform-docs` insertion markers, run in repo root: | |||
|
|||
```bash | |||
grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i'' -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' | |||
grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i'' -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' | |||
if sed --version > /dev/null 2>&1; then SED_CMD=(sed -i''); else SED_CMD=(sed -i ''); fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if sed --version > /dev/null 2>&1; then SED_CMD=(sed -i''); else SED_CMD=(sed -i ''); fi | |
if sed --version &> /dev/null; then SED_CMD=("sed" "-i''"); else SED_CMD=("sed" "-i" "''"); fi |
It may also make sense to prepend all sed
invocations with either \
(\sed
) or command
(command sed
) to force executing a binary from PATH
rather than a function or an alias 🤔
Also maybe replace if/else
with ternary (sed --version &> /dev/null && SED_CMD=("sed" "-i''") || SED_CMD=("sed" "-i" "''")
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also probably for the GNU sed
we can drop the ''
bit: SED_CMD=("sed" "-i")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for Mac will be next
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:47: SED_CMD=("sed" "-i" "''")
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:49: sed -i ''\'''\''' -e 's/^<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/<!-- BEGIN_TF_DOCS -->/' README.md
because on Ubuntu it will create README.md''
file, I don't think that it works
sed -i''\'''\''' -e 's/^<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/<!-- BEGIN_TF_DOCS -->/' README.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to wrap the individual array elements in quotes.
Using SED_CMD=(sed -i)
makes sense for GNU sed as the ''
gets dropped anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When without quotes we get what we need
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:47: SED_CMD=(sed -i '')
trace: replace_old_markers terraform_docs terraform_docs_ main main
terraform_docs.sh:49: sed -i '' -e 's/^<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/<!-- BEGIN_TF_DOCS -->/' README.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to wrap the individual array elements in quotes.
It helps to avoid empty elements to be "dropped".
It is also kinda common to wrap strings in quotes I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It helps to avoid empty elements to be "dropped".
While they are still part of an array though. So nevermind me (given Max's tests show everything works as expected w/o quotes) =)
hooks/terraform_docs.sh
Outdated
SED_CMD=(sed -i '') | ||
fi | ||
# shellcheck disable=SC2068 | ||
${SED_CMD[@]} -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my suggestion re wrapping array elements in double quotes.
## [1.94.1](v1.94.0...v1.94.1) (2024-08-30) ### Bug Fixes * **`terraform_docs`:** Fix non-GNU sed issues, introduced in v1.93.0, as previous fix doesn't work correctly ([#708](#708)) ([c986c5e](c986c5e))
This PR is included in version 1.94.1 🎉 |
Put an
x
into the box if that apply:Description of your changes
Fixes #707
How can we test changes