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

Conversation

sworisbreathing
Copy link
Contributor

@sworisbreathing sworisbreathing commented Aug 16, 2024

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

This PR explicitly overrides any output files configured in .terraform-docs.yml to ensure the rendered documentation is written to stdout (which the hook pipes to a file and then injects into the appropriate destination file).

Fixes #697

How can we test changes

setup as per the steps to reproduce in #697 and run the hook.

@sworisbreathing sworisbreathing changed the title fix(terraform_docs): force rendered documentation to be written to st… fix(terraform_docs): Force rendered documentation to be written to stdout (#697) Aug 16, 2024
@MaxymVlasov
Copy link
Collaborator

This actually ignore any setting set in .terraform-docs.yml output.file

Maybe that's a good idea to prioritize .terraform-docs.yml output.file over - --hook-config=--path-to-file= as it make step to resolve #382

@MaxymVlasov MaxymVlasov changed the title fix(terraform_docs): Force rendered documentation to be written to stdout (#697) fix(terraform_docs): fix issue and prioritize .terraform-docs.yml config output.file setting over --hook-config=--path-to-file= Aug 16, 2024
@MaxymVlasov MaxymVlasov changed the title fix(terraform_docs): fix issue and prioritize .terraform-docs.yml config output.file setting over --hook-config=--path-to-file= fix(terraform_docs): fix issue and prioritize output.file setting from .terraform-docs.yml config over --hook-config=--path-to-file= Aug 16, 2024
@MaxymVlasov MaxymVlasov changed the title fix(terraform_docs): fix issue and prioritize output.file setting from .terraform-docs.yml config over --hook-config=--path-to-file= fix(terraform_docs): Fix issue and prioritize output.file setting from .terraform-docs.yml config over --hook-config=--path-to-file= Aug 16, 2024
Comment on lines 168 to 177
# Prioritize `.terraform-docs.yml` `output.file` over
# `--hook-config=--path-to-file=` if it set
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)

if [ "$output_file" ]; then
# 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)

hooks/terraform_docs.sh Outdated Show resolved Hide resolved
hooks/terraform_docs.sh Outdated Show resolved Hide resolved
Comment on lines +168 to +169
# Prioritize `.terraform-docs.yml` `output.file` over
# `--hook-config=--path-to-file=` if it set
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.

@MaxymVlasov MaxymVlasov requested a review from yermulnik August 16, 2024 14:57
Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

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

@MaxymVlasov MaxymVlasov merged commit 9d6a22b into antonbabenko:master Aug 16, 2024
5 checks passed
antonbabenko pushed a commit that referenced this pull request Aug 16, 2024
## [1.92.2](v1.92.1...v1.92.2) (2024-08-16)

### Bug Fixes

* **`terraform_docs`:** Fix issue and prioritize `output.file` setting from `.terraform-docs.yml` config over `--hook-config=--path-to-file=` ([#698](#698)) ([9d6a22b](9d6a22b))
@antonbabenko
Copy link
Owner

This PR is included in version 1.92.2 🎉

@sworisbreathing sworisbreathing deleted the gh-697 branch August 19, 2024 00:07
@sworisbreathing
Copy link
Contributor Author

sworisbreathing commented Aug 19, 2024

Wow thanks for fixing up the PR @MaxymVlasov and the quick review/merge @yermulnik and @antonbabenko. I've just upgraded to the new release and it's working great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform_docs generated output incorrect when output.file is configured
4 participants