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

terraform_docs: Check abillity to use native terraform-docs config as replacement for custom --hook-config= #382

Open
MaxymVlasov opened this issue May 11, 2022 · 4 comments
Labels
feature New feature or request hook/terraform_docs Bash hook

Comments

@MaxymVlasov
Copy link
Collaborator

In #380 I discovered that terraform-docs now have a big config, that, theoretically, can be used as a replacement for all or part of custom --hook-config='s

Need to investigate that option, hope we can remove part of the custom logic.

@yermulnik
Copy link
Collaborator

yermulnik commented May 11, 2022

In #380 I discovered that terraform-docs now have a big config

Me too 😂

that, theoretically, can be used as a replacement for all or part of custom --hook-config='s

...which would mean we need to ship pre-commit-terraform along with a terraform-docs config file, which consumers of pre-commit-terraform would need to amend per their preferences and requirements and put into a standard location where terraform-docs expect it to be (the precedence of config vs cmdline options is not obvious from the terraform-docs README file).
So this may suffice for dockerized pre-commit-terraform only, I seem to think (I might be wrong).
On the other hand, terraform-docs README says to use cmdline options when used with pre-commit: https://github.com/terraform-docs/terraform-docs#pre-commit-hook

@drAlberT
Copy link

drAlberT commented Jun 15, 2023

I'm using the following hook configuration and everything related to terraform-doc is managed by the config files. It works like a charm but all the hook-config args (which are necessary to have it work) are totally useless as the same info is already present in the config file.

- repo: https://github.com/antonbabenko/pre-commit-terraform
   rev: v1.72.1
   hooks:
    - id: terraform_docs
       args:
         - --args=--config=infra/.terraform-docs.yml
         - --hook-config=--path-to-file=MODULE-DETAILS.md
         - --hook-config=--add-to-existing-file=false
         - --hook-config=--create-file-if-not-exist=true

@UkklyDukkling
Copy link

Adding my experience here using the .terraform-docs.yml config file with this hook. Generally passing in the config file works, however there is a slight bug that can produce some undesirable effects.

Example setup for various files:

Config file .pre-commit-config.yaml

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.81.0
    hooks:
      - id: terraform_docs
        args:
          - --args=--config=.terraform-docs.yml

Config file .terraform-docs.yml

formatter: "markdown"
version: "0.16.0"

content: |-
  {{ .Requirements }}
  ...
  various other customizations for our documentation
  ...

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

# Truncated: "settings" block left as defaults for each option

Setup of README.md

# Title for repo

General description paragraph

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

The Outcome

generated-result

As you can see, the hook actually ends up writing the console message to the README.md file instead of the expected content.

Two things to note:

  • If I remove the "output" block from my .terraform-docs.yml file, the content is correctly inserted into README.md. So far in my use case, all of the other configuration settings seem to work correctly. For my use case, I definitely want to retain the "output" block so more troubleshooting was necessary.
  • If I keep the .terraform-docs-yml file target as README.md and redirect the pre-commit hook towards a different file (i.e. add --hook-config=--path-to-file=RANDOM.md to the args), then I can get the desired output in README.md and the console output as shown in the image above in RANDOM.md. This leads me to believe there is something happening in the handoff or a double-write operation causing the conflict here.

Final Solution

I realized that I can refactor my README.md and .terraform-docs.yml files to no longer use the custom comment template that pre-commit-terraform expects. Setting different comment strings in these two files means that pre-commit-terraform will no longer write the changes and instead terraform-docs will handle writing the output per it's config file.

In summary, if you are converting from a standard use of this hook and want to now use terraform-docs config file feature, change your comment strings in your target markdown file to anything else (for example, back to the default <!-- BEGIN_TF_DOCS --> and <!-- END_TF_DOCS -->)

# DO NOT USE if you want to specify a config file with an "output" block
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Hope this helps!

@UkklyDukkling
Copy link

As an aside, I think the greatest driver towards my use of a config file instead of cmd line arguments was the ability to fully customize the generated content. I can re-order the standard sections, add static text wherever I want, insert other files' contents, and (best of all) I am adding an auto-generated code block that users will be able to copy/paste into their projects to consume the terraform module for ease-of-use.

That code block will be automagically updated for any terraform file changes instead of having to manually maintain the doc! (I'm going to be building sooo many modules and don't want to maintain that 😄 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request hook/terraform_docs Bash hook
Projects
None yet
Development

No branches or pull requests

4 participants