Skip to content
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 issue and prioritize output.file setting from .terraform-docs.yml config over --hook-config=--path-to-file= #698

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,24 @@ function terraform_docs {
if [[ "$args" != *"--config"* ]]; then
local tf_docs_formatter="md"

# Suppress terraform_docs color
else

local config_file=${args#*--config}
config_file=${config_file#*=}
config_file=${config_file% *}

# Prioritize `.terraform-docs.yml` `output.file` over
# `--hook-config=--path-to-file=` if it set
Comment on lines +170 to +171
Copy link
Collaborator

@yermulnik yermulnik Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't seem to understand the reasoning: ain't it better to raise error when both options are used instead of silently ignoring hook config's one? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that if a user provides this option to hook, then the user expects it to use this option. When there's a race condition, we should raise error or indicate incorrect setting by other mean(s) (e.g. by printing warning message that the one from YAML is used and the hook config's one is ignored).
I'm ok with either approach when this is clearly brought to user's attention and knowledge.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasoning was described in #382
I'll add warning msg

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a read and... and didn't get it =( I'd appreciate "tl;dr" =)
It's most common that command line parameters override config file params. Which is sort of comparable with --hook-config.
Either way I'm relying on your opinion in this case 👍🏻

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love removing code. Much more than write new one :D
Because you don't need to maintain code which not exist.

At the end, I'd like to replace as much hand-made settings as possible by 3rd-party, in this case - by terraform-docs settings. So, at the end, I'd like to deprecate and remove every --hook-config which can be replaced by terraform-docs settings, as long as it will not violate main idea of how pre-commit hooks works

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So keeping this in mind, we can clearly see that native terraform-docs settings should have precedence over --hook-config settings, as long as they not break hook execution - in last case we try explicitly disable such settings (or at least mention in docs that they shouldn't be used)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IE
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, now I think I got the point: hook config's parameter is a workaround from when tf-docs wasn't mature enough — this makes sense to me now 👍🏻 Thanks.

local output_file
# Get latest non-commented `output.file` from `.terraform-docs.yml`
output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | grep -v '#' | tail -n 1 || true)
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

if [ "$output_file" ]; then
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Extract filename from `output.file` line
text_file=$(echo "$output_file" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'")
fi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done it this way to avoid introduction of new dependencies (yq)


# Suppress terraform_docs color
local config_file_no_color
config_file_no_color="$config_file$(date +%s).yml"

Expand Down Expand Up @@ -228,7 +239,7 @@ function terraform_docs {

if [[ "$terraform_docs_awk_file" == "0" ]]; then
# shellcheck disable=SC2086
terraform-docs $tf_docs_formatter $args ./ > "$tmp_file"
terraform-docs --output-file="" $tf_docs_formatter $args ./ > "$tmp_file"
else
# Can't append extension for mktemp, so renaming instead
local tmp_file_docs
Expand All @@ -239,7 +250,7 @@ function terraform_docs {

awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
# shellcheck disable=SC2086
terraform-docs $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
terraform-docs --output-file="" $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
rm -f "$tmp_file_docs_tf"
fi

Expand Down
Loading