pre-commit-terraform icon indicating copy to clipboard operation
pre-commit-terraform copied to clipboard

Use terraform-docs native saving to file

Open khos2ow opened this issue 3 years ago β€’ 7 comments

In terraform-docs v0.12.0, we've added ability to insert generated output directly into a target file (https://github.com/terraform-docs/terraform-docs/issues/286)! This can replace all the sed/awk/perl and drastically simplify the process to the point of only executing:

terraform-docs --output-file README.md --output-mode inject .

Check out all the available option to configure this.

We've just cut a v0.12.0-beta.1 for folks to start experimenting. Please feel free to start using and file any issues or feedback.

khos2ow avatar Mar 09 '21 22:03 khos2ow

Thanks a lot for the update!

That would be really nice if we can remove sed/awk/perl hack which was there since pre-0.12 times :)

I don't have time for this work myself but maybe someone from the community can make a PR.

antonbabenko avatar Mar 10 '21 08:03 antonbabenko

Does it make sense to deprecate terraform_docs_without_aggregate_type_defaults and terraform_docs_replace?

Why not just support native terraform-docs usage similar to how tflint is setup? It seems like the other terraform_docs_* options were there because of the lack of features out-of-the-box with terraform-docs at the time, and that's no longer the case.

jonmaestas avatar Jul 08 '21 22:07 jonmaestas

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] avatar Oct 10 '21 00:10 github-actions[bot]

This fix would be very helpful to me! 😸

kevcube avatar Feb 22 '23 20:02 kevcube

Any chance to get this fixed and remove all those crazy dependencies like perl? terraform-docs is already at version 16 (for a long time btw) and with correct .terraform-docs.yml I'm ending up with my README.md corrupted by the hook:

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
README.md updated successfully
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

While when I run terraform-docs from cli README.md is updated as expected. Basically, terraform-docs is currently unusable if we want to preserve possibility to run terraform-docs manually from CLI...

Just for the reference, my .terraform-docs.yml looks like this:

formatter: "markdown table"
output:
  file: README.md
  mode: inject
  template: |-
    <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
    {{ .Content }}
    <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Would be also great to get rid of custom <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> and <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> markers and use default <!-- BEGIN_TF_DOCS --> and <!-- END_TF_DOCS -->...

dzavalkin-scayle avatar Jul 13 '23 14:07 dzavalkin-scayle

Feel free to implement it and send PR, your help will be much appreciated.

MaxymVlasov avatar Jul 13 '23 14:07 MaxymVlasov

Any chance to get this fixed and remove all those crazy dependencies like perl? terraform-docs is already at version 16 (for a long time btw) and with correct .terraform-docs.yml I'm ending up with my README.md corrupted by the hook:

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
README.md updated successfully
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

While when I run terraform-docs from cli README.md is updated as expected. Basically, terraform-docs is currently unusable if we want to preserve possibility to run terraform-docs manually from CLI...

Just for the reference, my .terraform-docs.yml looks like this:

formatter: "markdown table"
output:
  file: README.md
  mode: inject
  template: |-
    <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
    {{ .Content }}
    <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Would be also great to get rid of custom <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> and <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> markers and use default <!-- BEGIN_TF_DOCS --> and <!-- END_TF_DOCS -->...

@dzavalkin-scayle I'm coming across this behaviour too - there's some kind of conflict between terraform-docs which now writes to the README.md natively instead of printing the rendered text to stdout and the current pre-commit hook which takes stdout and does its own replacement into the README.md

I've worked around this by adding an argument into my hook which restores the previous behaviour of making terraform-docs return the parsed output on stdout

        args:
          - --args=--output-file=""

Maybe this helps you too? In the long term I agree that the entire wrapper should just be simplified and require a recent version of terraform-docs to do this natively; but I don't have time for that at the moment so this was a quick band-aid for me.

edit: this may not work with older hooks, I had a repo using 1.71.0 of this repo which created a bunch of files called "". I upgraded to 1.76.0 to match some of my other repos, and this hook works OK.

gchappel avatar Sep 14 '23 12:09 gchappel