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: adds terraform-docs as a linter 📄 #946

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ trunk check enable {linter}
| SQL | [sqlfluff], [sqlfmt], [sql-formatter], [squawk] |
| SVG | [svgo] |
| Swift | [stringslint], [swiftlint], [swiftformat] |
| Terraform | [terraform] (validate and fmt), [checkov], [tflint], [tfsec], [terrascan], [tofu] |
| Terraform | [terraform] (validate and fmt), [checkov], [tflint], [tfsec], [terrascan], [tofu], [terraform-docs] |
| Terragrunt | [terragrunt] |
| Textproto | [txtpbfmt] |
| TOML | [taplo] |
Expand Down Expand Up @@ -188,6 +188,7 @@ trunk check enable {linter}
[terraform]: https://developer.hashicorp.com/terraform/cli/code
[tofu]: https://opentofu.org/
[terragrunt]: https://terragrunt.gruntwork.io/docs/getting-started/quick-start/
[terraform-docs]: https://terraform-docs.io/
[tflint]: https://github.com/terraform-linters/tflint#readme
[tfsec]: https://github.com/aquasecurity/tfsec
[trivy]: https://trunk.io/linters/security/trivy
Expand Down
21 changes: 21 additions & 0 deletions linters/terraform-docs/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 0.1
lint:
definitions:
- name: terraform-docs
files: [terraform]
main_tool: terraform-docs
description: Generate terraform + tofu module documentation
commands:
- name: terraform-docs
Copy link
Member

Choose a reason for hiding this comment

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

The command name should more map what the command does than the name of the tool. So in this case given the formatter nature I would set name to "format"

Gowiem marked this conversation as resolved.
Show resolved Hide resolved
output: rewrite
formatter: true
run_from: ${target_directory}
run: terraform-docs markdown --output-file=README.md .
Copy link
Author

Choose a reason for hiding this comment

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

One note here: This is my team and I's (and I would suspect many many others) most common usage when using terraform-docs with terraform child + root modules. However, the tool is fairly configurable and can be run in a lot of different ways. I didn't see an obvious example of how make these run commands configurable, so I figured I'd put up this lightly working version for review + ask for feedback from the trunk team.

Do ya'll have any other configurable plugins that I should check out for inspiration to make sure this doesn't need an immediate update from the 2nd person to use it?

Copy link
Author

Choose a reason for hiding this comment

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

@EliSchleifer any thoughts here or ship it and we can deal with any requests for configurability in the future?

read_output_from: stdout
stdin: true
success_codes: [0]
suggest_if: never
known_good_version: 0.16.0
version_command:
parse_regex: ${semver}
run: terraform-docs --version
3 changes: 3 additions & 0 deletions linters/terraform-docs/terraform-docs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { linterFmtTest } from "tests";

linterFmtTest({ linterName: "terraform-docs" });
4 changes: 4 additions & 0 deletions linters/terraform-docs/test_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a test of the emergency broadcast system

<!-- BEGIN_TF_DOCS -->
<!-- END_TF_DOCS -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter terraform-docs test variables 1`] = `
"README.md updated successfully
"
`;
5 changes: 5 additions & 0 deletions linters/terraform-docs/test_data/variables.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "testing" {
type = string
default = "Hello world"
description = "This is a test of the emergency broadcast system"
}
Loading