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

feat(terraform_docs): Add support for custom markers to better support other formats than Markdown #752

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

tarfu
Copy link

@tarfu tarfu commented Jan 14, 2025

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 adds support to pass custom markers.
This helps to improve support for other formats like asciidoc which you can use if you use a config for terraform-doc.

How can we test changes

Add your custom marker to a README and run the hook with the options. You may also want to change the template used by terraform-doc

terraform_docs.sh --hook-config=--path-to-file=README.adoc --hook-config="--custom-marker-begin=// BEGIN_TF_DOCS" --hook-config="--custom-marker-end=// END_TF_DOCS" --args=--config=.terraform-docs.yml ./path/to/file.tf

@tarfu tarfu force-pushed the tfdocs-custom-markers branch from 895c3fe to c6065ae Compare January 14, 2025 11:18
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.

Thanks for your contribution. Please have a look into comments I added.
I'd also think of adding this as a single configuration parameter to avoid use of mix of standard and custom markers. Something like --custom-markers="// BEGIN;// END". This may decrease clarity and readability and separator needs a more thorough think though.

README.md Outdated Show resolved Hide resolved
hooks/terraform_docs.sh Outdated Show resolved Hide resolved
hooks/terraform_docs.sh Outdated Show resolved Hide resolved
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.

Looks good. @MaxymVlasov What do you think?

@MaxymVlasov MaxymVlasov changed the title feat(terraform-doc): Add support for custom markers to better support other formats than markdown feat(terraform_docs): Add support for custom markers to better support other formats than Markdown Jan 14, 2025
@MaxymVlasov MaxymVlasov changed the title feat(terraform_docs): Add support for custom markers to better support other formats than Markdown feat(terraform_docs): Add support for custom markers to better support other formats than Markdown Jan 14, 2025
@MaxymVlasov MaxymVlasov added feature New feature or request hook/terraform_docs Bash hook labels Jan 14, 2025
Copy link

codecov bot commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.38%. Comparing base (2e6b270) to head (fc53d0a).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #752      +/-   ##
===========================================
- Coverage   100.00%   96.38%   -3.62%     
===========================================
  Files            9       10       +1     
  Lines          161      249      +88     
  Branches         5        7       +2     
===========================================
+ Hits           161      240      +79     
- Misses           0        9       +9     
Flag Coverage Δ
CI-GHA 96.38% <ø> (-3.62%) ⬇️
MyPy 91.05% <ø> (?)
OS-Linux 96.38% <ø> (?)
OS-Windows 100.00% <ø> (ø)
OS-macOS 100.00% <ø> (ø)
Py-3.10.11 100.00% <ø> (ø)
Py-3.10.16 100.00% <ø> (?)
Py-3.11.11 100.00% <ø> (?)
Py-3.11.9 100.00% <ø> (ø)
Py-3.12.8 100.00% <ø> (ø)
Py-3.13.1 96.38% <ø> (-3.62%) ⬇️
Py-3.9.13 100.00% <ø> (ø)
Py-3.9.21 100.00% <ø> (ø)
VM-macos-13 100.00% <ø> (ø)
VM-macos-14 100.00% <ø> (ø)
VM-ubuntu-24.04 100.00% <ø> (?)
VM-ubuntu-latest 91.05% <ø> (?)
VM-windows-2025 100.00% <ø> (ø)
pytest 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MaxymVlasov
Copy link
Collaborator

Hmm, you probably had // BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK and // END OF PRE-COMMIT-TERRAFORM DOCS HOOK?

trace: replace_old_markers terraform_docs main main
       terraform_docs.sh:45: sed -i -e 's/^<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/\/\/ BEGIN_TF_DOCS/' README.adoc

trace: replace_old_markers terraform_docs main main
       terraform_docs.sh:46: sed -i -e 's/^<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->$/\/\/ END_TF_DOCS/' README.adoc

@MaxymVlasov
Copy link
Collaborator

Also, I found that

formatter: asciidoc table
output:
  file: README.adoc
  mode: inject
  template: |-
    // BEGIN_TF_DOCS
    {{ .Content }}
    // END_TF_DOCS
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: fc53d0a10b7846fa8571ddd504660cbe5c6110d6
    - id: terraform_docs
      args:
      - --hook-config=--add-to-existing-file=true     # Boolean. true or false
      #- --hook-config=--custom-marker-begin=// BEGIN_TF_DOCS
      #- --hook-config=--custom-marker-end=// END_TF_DOCS

When you have empty README.adoc, works fine without custom-marker-begin and custom-marker-end options.

And it produces identical result as for rev: 1.96.3

Can you please elaborate on what exactly you tried to fix/add by this PR?

@tarfu
Copy link
Author

tarfu commented Jan 15, 2025

If you got a non-empty file, it won't work for me without the custom markers.

= Terraform Module
This is a short description of the module and below this should be the terraform docs

// BEGIN_TF_DOCS

// END_TF_DOCS

With this snippet in an existing file, I won't get any output with the latest release/master.
If you add the standard markers below (which are then shown in the rendered asciidoc as it is not parsing them as comments), you'll get the output.

= Terraform Module
This is a short description of the module and below this should be the terraform docs

// BEGIN_TF_DOCS

// END_TF_DOCS

<!-- BEGIN_TF_DOCS -->
<!-- END_TF_DOCS -->

This will result in:

= Hub Management

This folder contains the Terraform code for deployment of the Hub resources, including Virtual WAN and Intermediate Hubs.

// BEGIN_TF_DOCS
=== Requirements

[cols="a,a",options="header,autowidth"]
|===
|Name |Version
|[[requirement_terraform]] <<requirement_terraform,terraform>> |~> 1.7
|===

=== Providers

No providers.

=== Modules

No modules.

=== Resources

No resources.

=== Inputs

No inputs.

=== Outputs

No outputs.
// END_TF_DOCS

<!-- BEGIN_TF_DOCS -->
<!-- END_TF_DOCS -->

This also works if you only have a begin marker, as it is the only thing the grep looks for.
That is the reason for the custom marker. You could also try to parse the config YAML and interpret what the markers are, but this would be a bit too much, I think.

@MaxymVlasov
Copy link
Collaborator

I see what you're talking about

Interesting, that specified by you example of

= Terraform Module
This is a short description of the module and below this should be the terraform docs

// BEGIN_TF_DOCS

// END_TF_DOCS

work well with

formatter: asciidoc table
output:
  file: README.adoc
  mode: inject
  template: |-
    // BEGIN_TF_DOCS
    {{ .Content }}
    // END_TF_DOCS
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: v1.96.3
  hooks:
    - id: terraform_docs
      args:
      - --hook-config=--add-to-existing-file=true     # Boolean. true or false
      - --args=--config=.terraform-docs.yml

but not with - --hook-config=--add-to-existing-file=false which is default

and

      args:
      - --hook-config=--create-file-if-not-exist=true # Boolean. true or false
      - --args=--config=.terraform-docs.yml

produce

# dir-name

<!-- BEGIN_TF_DOCS -->
<!-- END_TF_DOCS -->

// BEGIN_TF_DOCS
== Requirements
...
// END_TF_DOCS

So if --hook-config=--add-to-existing-file=true able to use right templating in 1.96.3 from .terraform-docs.yml, I'm curious why default can't do the same without extra configs to hook? There could be a bug somewhere 🤔 => there should be a better way than proposed by this PR.

If you have time, please investigate what happens, using PCT_LOG=trace pre-comit run command and let us know if you'll find anything weird and try to fix it if possible. If not, I'll check later by my own

@tarfu
Copy link
Author

tarfu commented Jan 15, 2025

--hook-config=--add-to-existing-file=true skips the marker check, so it will always run tf-docs if it is set to false, it will check if the marker we expect is present and only if it is we will run tf-docs.
As we don't parse the config and try to extract the marker from there, I think the behavior is expected. If it is desired like that is another question.

You could try to parse the template in the config, grab the first line of it, check if it is not {{ .Content }} and hope everyone is using the YAML string style we expect. Which leads to other issues.

@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented Jan 15, 2025

Am I right that you anyway need to specify template: in .terraform-docs.yml if you not use <!-- BEGIN_TF_DOCS --> markers, which looks like are set by default?
https://terraform-docs.io/user-guide/configuration/

So, we actually need to find all formatter: options that not support HTML comments, and add check that if such formater provided - run terraform-docs same way as for --hook-config=--add-to-existing-file=true

@tarfu
Copy link
Author

tarfu commented Jan 15, 2025

Yeah, it is set up defaulting to Markdown and HTML comments, but there would be other options in MD as well.

If you run it like it was --hook-config=--add-to-existing-file=true you would lose the ability to skip files without the marker. Which is funnily enough the behavior I want/need :D

@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented Jan 15, 2025

You would lose the ability to skip files without the marker

Ah, yes. And parse it from template will be problematic.
Your PR looks perfectly fine then.

Just let make file creation header (#) be customizable too then for - --hook-config=--create-file-if-not-exist=true, in

# Use of insertion markers, where there is no existing README file
{
echo -e "# ${PWD##*/}\n"
echo "$insertion_marker_begin"
echo "$insertion_marker_end"
} >> "$output_file"

as

# dir-name-here

// BEGIN_TF_DOCS
== Requirements
...
// END_TF_DOCS

will be annoying to fix.
There could be sense to make ${PWD##*/} customizable as well, but it out of scope for sure (and maybe not needed at all).

So, let's reiterate on same question but for different use-case - can we just get header data from formatter like doc_header_prefix[${formatter}] or we need to hardcode it as well via --hook-config=--custom-doc-header?

@tarfu
Copy link
Author

tarfu commented Jan 15, 2025

I think --hook-config=--custom-doc-header would be the way to go, as different formatters have multiple options to go with and would be the easiest way to be customized.

Another way could be just, letting terraform-docs create the file would be an option as well, which should work if there is a tf file.
This would enable us to throw away one marker and only test against the beginning marker.

@MaxymVlasov
Copy link
Collaborator

Please add --hook-config=--custom-doc-header then, with default to #
Don't forget docs section as well.

Another way could be just, letting terraform-docs create the file would be an option as well, which should work if there is a tf file. This would enable us to throw away one marker and only test against the beginning marker.

Interesting solution, but let someone comes with feature request, as I don't see who could use it :)

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

Successfully merging this pull request may close these issues.

3 participants