terraform-docs icon indicating copy to clipboard operation
terraform-docs copied to clipboard

Option to Hide Specific Variables

Open Makeshift opened this issue 2 years ago • 2 comments

What problem are you facing?

We use terragrunt and all of our in-house modules require a bunch of variables that are automatically merged into the inputs of the module when Terragrunt does a deployment (based on cloudposse/terraform-null-label). However, this means when terraform-docs runs we end up with a bunch of variables in the inputs that don't actually matter, because only 2-3 of them need defining and the rest are provided automatically.

It would be really nice if we could specify these variables as hidden in our terraform-docs config, so they don't get rendered in the inputs table.

How could terraform-docs help solve your problem?

Include a configuration option to hide a given list of variables from being outut by terraform-docs.

Makeshift avatar Mar 20 '23 16:03 Makeshift

I have a different request with the potentially same solution -- an internal module has been developed with a number of variables included for debugging purposes; although they're documented in the source code, there's no reason for these features to appear in the main documentation.

They have all been given a common prefix (namely __) and I was fiddling with filtering on {{ range .Module.Inputs }} manually, but there does not seem to be a way to invoke the formatter on the updated range.

Therefore, having a way to have the tool pre-filter these based on a config variable, ideally with wildcard/prefix support, would be an ideal solution to my problem.

javajawa avatar Mar 29 '23 18:03 javajawa

I really wanted to do this using the official terraform-docs binary, rather than custom-building #651, so my colleagues could just grab it and it works.

After trawling golang's template documentation, and significant trial and error, I present this working monstrosity. Add this to .terraform-docs.yml and modify the $hideInputs list to your needs:

content: |
    {{ .Header }}
    {{ .Requirements }}
    {{ .Providers }}
    {{ .Modules }}

    ## Inputs
    {{- $hideInputs := list "additional_tag_map" "attributes" "aws_account_id" "aws_region" "context" "default_region" "default_tags" "delimiter" "dotprefix" "enabled" "environment" "id_length_limit" "label_key_case" "label_order" "label_value_case" "name" "namespace" "prefix" "prefix_alphanum" "prefix_dots_to_dashes" "prefix_dots_to_underscores" "regex_replace_chars" "slashprefix" "stage" "stage_long" "tags" "tf_source" "tf_state_file" }}
    {{- $filteredInputs := list -}}
    {{- range .Module.Inputs -}}
        {{- if not (has .Name $hideInputs) -}}
            {{- $filteredInputs = append $filteredInputs . -}}
        {{- end -}}
    {{- end -}}
    {{ if not $filteredInputs }}

      No inputs.
    {{ else }}
      | Name | Description | Type | Default | Required |
      |------|-------------|------|---------|:--------:|
      {{- range $filteredInputs }}
          | {{ anchorNameMarkdown "input" .Name }} | {{ tostring .Description | sanitizeMarkdownTbl }} | {{ printf " " }}<pre lang="json">{{ tostring .Type | sanitizeMarkdownTbl }}</pre> | {{ printf " " }}<pre lang="json">{{ .GetValue | sanitizeMarkdownTbl }}</pre> | {{ printf " " }}{{ ternary .Required "yes" "no" }} |
      {{- end }}
    {{- end }}
    {{ .Outputs }}
    {{/** End of file fixer */}}

@javajawa

Makeshift avatar Jun 16 '23 19:06 Makeshift

This is done in #651 by @kampka originally only supporting variable, but we extend it to all kind of resources. Anything Terraform related (i.e. resource, data, module, variable, and output) now can be ignored using terraform-docs-ignore keyword in the leading comment of that resource.

khos2ow avatar May 30 '24 16:05 khos2ow