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

Add hook for autoformat of terraform code snippets embedded in the docs

Open antonbabenko opened this issue 2 years ago β€’ 2 comments

There is a utility called terrafmt that can be used to check and fix the formating of Terraform snippets embedded into usage sections in README.

How could pre-commit-terraform help solve your problem?

It would be great to have native support as a pre-commit hook.

antonbabenko avatar Oct 22 '21 08:10 antonbabenko

@MaxymVlasov @antonbabenko: Can I pick the terrafmt hook? I have tried it in my system. Looks like, terrafmt does not extract terraform files and does not do inline code formatting from the README.md file if, we start the Three backquotes after 4 spaces in the README.md file. It only works, if we start the Three backquotes at column 0.

Ex:

  1. Works:

image

terrafmt blocks README.md

####### B1 @ #587
resource "aws_lambda_function" "pass" {
  function_name = "test-env"
  role          = ""
  runtime       = "python3.8"

  environment {
    variables = {
      AWS_DEFAULT_REGION = "us-west-2"
    }
  }
}
  1. Does not work

image

terrafmt blocks README.md

The README.md in the repository has all the inline code starting at 4 spaces. For ex: Hooks usage notes and examples , apart from checkov, all other sections defines the inline code starting at 4 spaces.

rahulmlokurte avatar Dec 30 '21 19:12 rahulmlokurte

Sounds like this is the problem in terrafmt because markdown format allows having an offset.

antonbabenko avatar Jan 03 '22 13:01 antonbabenko