-
-
Notifications
You must be signed in to change notification settings - Fork 540
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
Changes from 5 commits
fa5f222
74a133c
cc5d581
aee5dc9
bb402c9
c44f074
a76f03b
7067081
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,16 @@ function main { | |
function replace_old_markers { | ||
local -r file=$1 | ||
|
||
sed -i'' -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" | ||
sed -i'' -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" | ||
# Determine the appropriate sed command based on the operating system (GNU sed or BSD sed) | ||
if sed --version > /dev/null 2>&1; then | ||
SED_CMD=(sed -i'') | ||
else | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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:
With quotes:
Without quotes:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I replace
Which works fine with GNU sed. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 |
||
# shellcheck disable=SC2068 | ||
${SED_CMD[@]} -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" | ||
MaxymVlasov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
####################################################################### | ||
|
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 may also make sense to prepend all
sed
invocations with either\
(\sed
) orcommand
(command sed
) to force executing a binary fromPATH
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
because on Ubuntu it will create
README.md''
file, I don't think that it worksThere 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
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".
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.
While they are still part of an array though. So nevermind me (given Max's tests show everything works as expected w/o quotes) =)