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 replace mode for TF 0.12+; Use native saving to file for TF 0.12+. Both requires terraform-docs v0.12.0+ which released in 2021. #705

Merged
merged 6 commits into from
Aug 29, 2024

Conversation

MaxymVlasov
Copy link
Collaborator

@MaxymVlasov MaxymVlasov commented Aug 29, 2024

Important

Should be merged after #706

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

Closes #179

How can we test changes

.terraform-docs.yml:

formatter: "md"
output:
  mode: replace
  template: |-
        {{ .Content }}

.pre-commit-config.yaml:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: ece0c17b91c17b29201aba379f99e79f59813438
    hooks:
      - id: terraform_docs
        args:
          - --args=--config=.terraform-docs.yml
          ## Also CLI options have precedence over YAML config
          ##  that's default behavior of major chunk of tools, so we just preserve it.
          #- --args=--output-mode=inject

@MaxymVlasov MaxymVlasov requested a review from yermulnik as a code owner August 29, 2024 10:47
@MaxymVlasov MaxymVlasov changed the title feat(terraform-docs): Use native save to file for TF 0.12+ (required terraform-docs v0.12.0+ which released in 2021); Add support for replace mode feat(terraform-docs): Use native save to file for TF 0.12+ (required terraform-docs v0.12.0+ which released in 2021); Add support for replace mode Aug 29, 2024
@MaxymVlasov MaxymVlasov changed the title feat(terraform-docs): Use native save to file for TF 0.12+ (required terraform-docs v0.12.0+ which released in 2021); Add support for replace mode feat(terraform-docs): Add support for replace mode for TF 0.12+; Use native saving to file for TF 0.12+. Both requires terraform-docs v0.12.0+ which released in 2021. Aug 29, 2024
@MaxymVlasov MaxymVlasov marked this pull request as draft August 29, 2024 11:10
@@ -202,6 +201,14 @@ function terraform_docs {
text_file=$output_file
fi

# Use `.terraform-docs.yml` `output.mode` if it set
local config_output_mode
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
Copy link
Collaborator

Choose a reason for hiding this comment

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

JFYI: I'm still concerned with picking last key instead of first despite YAML requires keys to be unique — https://yaml.org/spec/1.2-old/spec.html#id2764044

The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In YAML you can do next

foo: &anchor
    key: 111

bar: 
    <<: *anchor
    key: 222

I not sure what words in https://yaml.org/spec/1.2-old/spec.html#id2765878 exactly means, but it actually same to

foo:
    key: 111

bar: 
    key: 111
    key: 222

Copy link
Collaborator

Choose a reason for hiding this comment

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

I might have worded it a bit ambiguous. What I mean is when you do grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+mode:' | tail -n 1, you actually allow the below YAML within 1001 lines starting from the first match of ^output:$:

output:
    mode: 111

output: 
    mode: 222
    mode: 333

The output is what YAML expects to be unique node. Else YAML is incorrect.
From my point of view the first occurrence of output.mode key with value of 111 is what must be rendered as value for output.mode, whilst your implementation means the output.mode key with value 333 is what is taken as correct value.

Copy link
Collaborator

@yermulnik yermulnik Aug 29, 2024

Choose a reason for hiding this comment

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

Given we're trying to employ as less third-party tools as feasible and to keep solution simple, I'd suggest we take first occurrence of output.mode since duplicates are not allowed.

@MaxymVlasov MaxymVlasov merged commit 1a1b4a3 into master Aug 29, 2024
5 checks passed
@MaxymVlasov MaxymVlasov deleted the feat/GH-179 branch August 29, 2024 19:59
antonbabenko pushed a commit that referenced this pull request Aug 29, 2024
# [1.94.0](v1.93.1...v1.94.0) (2024-08-29)

### Features

* **`terraform-docs`:** Add support for `replace` mode  for TF 0.12+; Use native saving to file for TF 0.12+. Both requires `terraform-docs` v0.12.0+ which released in 2021. ([#705](#705)) ([1a1b4a3](1a1b4a3))
@antonbabenko
Copy link
Owner

This PR is included in version 1.94.0 🎉

@MaxymVlasov MaxymVlasov added feature New feature or request hook/terraform_docs Bash hook labels Aug 29, 2024
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.

Use terraform-docs native saving to file
3 participants