From c8fc7ab4f2bc6b3f59b9348f0eaab3953810154a Mon Sep 17 00:00:00 2001 From: "Edgar R. Sandi" Date: Mon, 12 Apr 2021 23:49:37 -0300 Subject: [PATCH] add ability to show or hide data-sources Signed-off-by: Edgar R. Sandi --- docs/reference/asciidoc-document.md | 4 +- docs/reference/asciidoc-table.md | 4 +- docs/reference/asciidoc.md | 4 +- docs/reference/json.md | 4 +- docs/reference/markdown-document.md | 4 +- docs/reference/markdown-table.md | 4 +- docs/reference/markdown.md | 4 +- docs/reference/pretty.md | 8 +-- docs/reference/terraform-docs.md | 4 +- docs/reference/tfvars-hcl.md | 4 +- docs/reference/tfvars-json.md | 4 +- docs/reference/tfvars.md | 4 +- docs/reference/toml.md | 4 +- docs/reference/xml.md | 4 +- docs/reference/yaml.md | 4 +- go.mod | 2 +- go.sum | 2 + internal/cli/config.go | 10 +++- internal/cli/run.go | 2 +- internal/format/asciidoc_document_test.go | 4 ++ internal/format/asciidoc_table_test.go | 4 ++ internal/format/json.go | 25 +------- internal/format/json_test.go | 4 ++ internal/format/markdown_document_test.go | 4 ++ internal/format/markdown_table_test.go | 4 ++ internal/format/pretty_test.go | 4 ++ .../format/templates/asciidoc_document.tmpl | 14 +++-- internal/format/templates/asciidoc_table.tmpl | 14 +++-- .../format/templates/markdown_document.tmpl | 14 +++-- internal/format/templates/markdown_table.tmpl | 14 +++-- internal/format/templates/pretty.tmpl | 26 ++++---- .../asciidoc/document-OnlyDataSources.golden | 6 ++ .../asciidoc/document-OnlyResources.golden | 4 +- .../asciidoc/table-OnlyDataSources.golden | 8 +++ .../asciidoc/table-OnlyResources.golden | 2 - .../testdata/json/json-OnlyDataSources.golden | 27 +++++++++ .../testdata/json/json-OnlyResources.golden | 16 ----- .../markdown/document-OnlyDataSources.golden | 6 ++ .../markdown/document-OnlyResources.golden | 4 +- .../markdown/table-OnlyDataSources.golden | 6 ++ .../markdown/table-OnlyResources.golden | 4 +- .../format/testdata/pretty/pretty-Base.golden | 4 +- .../pretty/pretty-OnlyDataSources.golden | 2 + .../pretty/pretty-OnlyResources.golden | 4 +- .../testdata/pretty/pretty-WithColor.golden | 4 +- .../testdata/toml/toml-OnlyDataSources.golden | 23 ++++++++ .../testdata/toml/toml-OnlyResources.golden | 16 ----- .../testdata/xml/xml-OnlyDataSources.golden | 27 +++++++++ .../testdata/xml/xml-OnlyResources.golden | 16 ----- .../testdata/yaml/yaml-OnlyDataSources.golden | 20 +++++++ .../testdata/yaml/yaml-OnlyResources.golden | 12 ---- internal/format/toml.go | 29 +-------- internal/format/toml_test.go | 4 ++ internal/format/xml.go | 25 +------- internal/format/xml_test.go | 4 ++ internal/format/yaml.go | 25 +------- internal/format/yaml_test.go | 4 ++ internal/print/settings.go | 59 +++++++++++++++++-- internal/testutil/settings.go | 1 + 59 files changed, 327 insertions(+), 246 deletions(-) create mode 100644 internal/format/testdata/asciidoc/document-OnlyDataSources.golden create mode 100644 internal/format/testdata/asciidoc/table-OnlyDataSources.golden create mode 100644 internal/format/testdata/json/json-OnlyDataSources.golden create mode 100644 internal/format/testdata/markdown/document-OnlyDataSources.golden create mode 100644 internal/format/testdata/markdown/table-OnlyDataSources.golden create mode 100644 internal/format/testdata/pretty/pretty-OnlyDataSources.golden create mode 100644 internal/format/testdata/toml/toml-OnlyDataSources.golden create mode 100644 internal/format/testdata/xml/xml-OnlyDataSources.golden create mode 100644 internal/format/testdata/yaml/yaml-OnlyDataSources.golden diff --git a/docs/reference/asciidoc-document.md b/docs/reference/asciidoc-document.md index 115f262..1897b48 100644 --- a/docs/reference/asciidoc-document.md +++ b/docs/reference/asciidoc-document.md @@ -30,7 +30,7 @@ terraform-docs asciidoc document [PATH] [flags] --default show Default column or section (default true) --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") @@ -40,7 +40,7 @@ terraform-docs asciidoc document [PATH] [flags] --output-values-from string inject output values from file into outputs (default "") --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/asciidoc-table.md b/docs/reference/asciidoc-table.md index bf66293..2fe4a45 100644 --- a/docs/reference/asciidoc-table.md +++ b/docs/reference/asciidoc-table.md @@ -30,7 +30,7 @@ terraform-docs asciidoc table [PATH] [flags] --default show Default column or section (default true) --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") @@ -40,7 +40,7 @@ terraform-docs asciidoc table [PATH] [flags] --output-values-from string inject output values from file into outputs (default "") --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/asciidoc.md b/docs/reference/asciidoc.md index 0d474c0..95518e0 100644 --- a/docs/reference/asciidoc.md +++ b/docs/reference/asciidoc.md @@ -34,14 +34,14 @@ terraform-docs asciidoc [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/json.md b/docs/reference/json.md index 6f2ba56..4664c9d 100644 --- a/docs/reference/json.md +++ b/docs/reference/json.md @@ -29,14 +29,14 @@ terraform-docs json [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/markdown-document.md b/docs/reference/markdown-document.md index 61077df..7830419 100644 --- a/docs/reference/markdown-document.md +++ b/docs/reference/markdown-document.md @@ -31,7 +31,7 @@ terraform-docs markdown document [PATH] [flags] --escape escape special characters (default true) --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") @@ -41,7 +41,7 @@ terraform-docs markdown document [PATH] [flags] --output-values-from string inject output values from file into outputs (default "") --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/markdown-table.md b/docs/reference/markdown-table.md index 0942b29..fd8fb32 100644 --- a/docs/reference/markdown-table.md +++ b/docs/reference/markdown-table.md @@ -31,7 +31,7 @@ terraform-docs markdown table [PATH] [flags] --escape escape special characters (default true) --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") @@ -41,7 +41,7 @@ terraform-docs markdown table [PATH] [flags] --output-values-from string inject output values from file into outputs (default "") --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/markdown.md b/docs/reference/markdown.md index 37c0745..451ec53 100644 --- a/docs/reference/markdown.md +++ b/docs/reference/markdown.md @@ -35,14 +35,14 @@ terraform-docs markdown [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/pretty.md b/docs/reference/pretty.md index aefd9e0..8cf0474 100644 --- a/docs/reference/pretty.md +++ b/docs/reference/pretty.md @@ -29,14 +29,14 @@ terraform-docs pretty [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") @@ -109,8 +109,8 @@ generates the following output: resource.null_resource.foo (resource) (https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) resource.tls_private_key.baz (resource) (https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) - resource.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) - resource.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) + data.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) + data.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) input.bool-1 (true) diff --git a/docs/reference/terraform-docs.md b/docs/reference/terraform-docs.md index 0a607f6..43f4427 100644 --- a/docs/reference/terraform-docs.md +++ b/docs/reference/terraform-docs.md @@ -23,14 +23,14 @@ terraform-docs [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") -h, --help help for terraform-docs - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/tfvars-hcl.md b/docs/reference/tfvars-hcl.md index 5b28c4d..59b9015 100644 --- a/docs/reference/tfvars-hcl.md +++ b/docs/reference/tfvars-hcl.md @@ -29,14 +29,14 @@ terraform-docs tfvars hcl [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/tfvars-json.md b/docs/reference/tfvars-json.md index 9f16a75..8beed40 100644 --- a/docs/reference/tfvars-json.md +++ b/docs/reference/tfvars-json.md @@ -28,14 +28,14 @@ terraform-docs tfvars json [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/tfvars.md b/docs/reference/tfvars.md index 26fc0c2..4d8e763 100644 --- a/docs/reference/tfvars.md +++ b/docs/reference/tfvars.md @@ -24,14 +24,14 @@ Generate terraform.tfvars of inputs. -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/toml.md b/docs/reference/toml.md index 015edd8..73e76c7 100644 --- a/docs/reference/toml.md +++ b/docs/reference/toml.md @@ -28,14 +28,14 @@ terraform-docs toml [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/xml.md b/docs/reference/xml.md index 8ca1693..bd4faad 100644 --- a/docs/reference/xml.md +++ b/docs/reference/xml.md @@ -28,14 +28,14 @@ terraform-docs xml [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/docs/reference/yaml.md b/docs/reference/yaml.md index 11a4d5f..a36ff53 100644 --- a/docs/reference/yaml.md +++ b/docs/reference/yaml.md @@ -28,14 +28,14 @@ terraform-docs yaml [PATH] [flags] -c, --config string config file name (default ".terraform-docs.yml") --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") - --hide strings hide section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") - --show strings show section [footer, header, inputs, modules, outputs, providers, requirements, resources] + --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") diff --git a/go.mod b/go.mod index 62458c0..743f424 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 - github.com/terraform-docs/plugin-sdk v0.2.0 + github.com/terraform-docs/plugin-sdk v0.2.1-0.20210329203526-90c9fa0bfae9 github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b honnef.co/go/tools v0.1.2 diff --git a/go.sum b/go.sum index da62d05..1fcac00 100644 --- a/go.sum +++ b/go.sum @@ -223,6 +223,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/terraform-docs/plugin-sdk v0.2.0 h1:dOoPRe0TSg42ywdv/DFG6UrCZ2XPwnS/z8uZjEgz7ro= github.com/terraform-docs/plugin-sdk v0.2.0/go.mod h1:3G+0nZTeaMF1c5CZh8cOEYeNq0kUL6+DlQOVcxK7eCQ= +github.com/terraform-docs/plugin-sdk v0.2.1-0.20210329203526-90c9fa0bfae9 h1:S9JjFwxoxMJo99hv5mvBNndN/Hu8DEi8qYPJHzSIk4o= +github.com/terraform-docs/plugin-sdk v0.2.1-0.20210329203526-90c9fa0bfae9/go.mod h1:3G+0nZTeaMF1c5CZh8cOEYeNq0kUL6+DlQOVcxK7eCQ= github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f h1:WXgHENMC8JOyj6aRpOlnAnJkZ3ZAkPBOhqYrJ5GOhDE= github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f/go.mod h1:GtanFwTsRRXScYHOMb5h4K18XQBFeS2tXat9/LrPtPc= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/internal/cli/config.go b/internal/cli/config.go index f0a07d2..b0d98e3 100644 --- a/internal/cli/config.go +++ b/internal/cli/config.go @@ -19,6 +19,7 @@ import ( ) const ( + sectionDataSources = "data-sources" sectionFooter = "footer" sectionHeader = "header" sectionInputs = "inputs" @@ -30,6 +31,7 @@ const ( ) var allSections = []string{ + sectionDataSources, sectionFooter, sectionHeader, sectionInputs, @@ -49,6 +51,7 @@ type sections struct { ShowAll bool `yaml:"show-all"` HideAll bool `yaml:"hide-all"` + dataSources bool `yaml:"-"` header bool `yaml:"-"` footer bool `yaml:"-"` inputs bool `yaml:"-"` @@ -66,6 +69,7 @@ func defaultSections() sections { ShowAll: true, HideAll: false, + dataSources: false, header: false, footer: false, inputs: false, @@ -84,14 +88,14 @@ func (s *sections) validate() error { //nolint:gocyclo for _, item := range s.Show { switch item { - case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7]: + case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7], allSections[8]: default: return fmt.Errorf("'%s' is not a valid section", item) } } for _, item := range s.Hide { switch item { - case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7]: + case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7], allSections[8]: default: return fmt.Errorf("'%s' is not a valid section", item) } @@ -359,6 +363,7 @@ func (c *Config) process() { c.Sections.HideAll = true } + c.Sections.dataSources = c.Sections.visibility("data-sources") c.Sections.header = c.Sections.visibility("header") c.Sections.footer = c.Sections.visibility("footer") c.Sections.inputs = c.Sections.visibility("inputs") @@ -449,6 +454,7 @@ func (c *Config) extract() (*print.Settings, *terraform.Options) { options.FooterFromFile = c.FooterFrom // sections + settings.ShowDataSources = c.Sections.dataSources settings.ShowInputs = c.Sections.inputs settings.ShowModuleCalls = c.Sections.modulecalls settings.ShowOutputs = c.Sections.outputs diff --git a/internal/cli/run.go b/internal/cli/run.go index 8dee2c3..7ffca3e 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -85,7 +85,7 @@ func PreRunEFunc(config *Config) func(*cobra.Command, []string) error { return err } - // set the base moduel directory + // set the base module directory config.BaseDir = args[0] return nil diff --git a/internal/format/asciidoc_document_test.go b/internal/format/asciidoc_document_test.go index a1a03e9..2524daf 100644 --- a/internal/format/asciidoc_document_test.go +++ b/internal/format/asciidoc_document_test.go @@ -121,6 +121,10 @@ func TestAsciidocDocument(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/asciidoc_table_test.go b/internal/format/asciidoc_table_test.go index da9c424..d451a52 100644 --- a/internal/format/asciidoc_table_test.go +++ b/internal/format/asciidoc_table_test.go @@ -121,6 +121,10 @@ func TestAsciidocTable(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/json.go b/internal/format/json.go index b863b61..2d0bd06 100644 --- a/internal/format/json.go +++ b/internal/format/json.go @@ -40,30 +40,7 @@ func (j *JSON) Print(module *terraform.Module, settings *print.Settings) (string Resources: make([]*terraform.Resource, 0), } - if settings.ShowHeader { - copy.Header = module.Header - } - if settings.ShowFooter { - copy.Footer = module.Footer - } - if settings.ShowInputs { - copy.Inputs = module.Inputs - } - if settings.ShowModuleCalls { - copy.ModuleCalls = module.ModuleCalls - } - if settings.ShowOutputs { - copy.Outputs = module.Outputs - } - if settings.ShowProviders { - copy.Providers = module.Providers - } - if settings.ShowRequirements { - copy.Requirements = module.Requirements - } - if settings.ShowResources { - copy.Resources = module.Resources - } + print.CopySections(settings, module, copy) buffer := new(bytes.Buffer) diff --git a/internal/format/json_test.go b/internal/format/json_test.go index 37b5952..74f95bd 100644 --- a/internal/format/json_test.go +++ b/internal/format/json_test.go @@ -72,6 +72,10 @@ func TestJson(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/markdown_document_test.go b/internal/format/markdown_document_test.go index daf5316..c50b4b8 100644 --- a/internal/format/markdown_document_test.go +++ b/internal/format/markdown_document_test.go @@ -132,6 +132,10 @@ func TestMarkdownDocument(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/markdown_table_test.go b/internal/format/markdown_table_test.go index 8903a4a..4eea4cd 100644 --- a/internal/format/markdown_table_test.go +++ b/internal/format/markdown_table_test.go @@ -132,6 +132,10 @@ func TestMarkdownTable(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/pretty_test.go b/internal/format/pretty_test.go index edf0a22..3b67df0 100644 --- a/internal/format/pretty_test.go +++ b/internal/format/pretty_test.go @@ -72,6 +72,10 @@ func TestPretty(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/templates/asciidoc_document.tmpl b/internal/format/templates/asciidoc_document.tmpl index 5fff6d4..54f672d 100644 --- a/internal/format/templates/asciidoc_document.tmpl +++ b/internal/format/templates/asciidoc_document.tmpl @@ -48,17 +48,21 @@ {{ end }} {{ end -}} -{{- if .Settings.ShowResources -}} +{{- if or .Settings.ShowResources .Settings.ShowDataSources -}} {{ indent 0 "=" }} Resources {{ if not .Module.Resources }} No resources. {{ else }} The following resources are used by this module: {{ range .Module.Resources }} - {{ if eq (len .URL) 0 }} - - {{ .Spec }} {{ printf "(%s)" .GetMode -}} - {{- else -}} - - {{ .URL }}[{{ .Spec }}] {{ printf "(%s)" .GetMode -}} + {{- $isResource := and $.Settings.ShowResources ( eq "resource" (printf "%s" .GetMode)) }} + {{- $isDataResource := and $.Settings.ShowDataSources ( eq "data source" (printf "%s" .GetMode)) }} + {{- if or $isResource $isDataResource }} + {{ if eq (len .URL) 0 }} + - {{ .Spec }} {{ printf "(%s)" .GetMode -}} + {{- else -}} + - {{ .URL }}[{{ .Spec }}] {{ printf "(%s)" .GetMode -}} + {{- end }} {{- end }} {{- end }} {{ end }} diff --git a/internal/format/templates/asciidoc_table.tmpl b/internal/format/templates/asciidoc_table.tmpl index 021f47a..c7f4c56 100644 --- a/internal/format/templates/asciidoc_table.tmpl +++ b/internal/format/templates/asciidoc_table.tmpl @@ -50,7 +50,7 @@ {{ end }} {{ end -}} -{{- if .Settings.ShowResources -}} +{{- if or .Settings.ShowResources .Settings.ShowDataSources -}} {{ indent 0 "=" }} Resources {{ if not .Module.Resources }} No resources. @@ -59,10 +59,14 @@ |=== |Name |Type {{- range .Module.Resources }} - {{ if eq (len .URL) 0 }} - |{{ .Spec }} |{{ .GetMode }} - {{- else -}} - |{{ .URL }}[{{ .Spec }}] |{{ .GetMode }} + {{- $isResource := and $.Settings.ShowResources ( eq "resource" (printf "%s" .GetMode)) }} + {{- $isDataResource := and $.Settings.ShowDataSources ( eq "data source" (printf "%s" .GetMode)) }} + {{- if or $isResource $isDataResource }} + {{ if eq (len .URL) 0 }} + |{{ .Spec }} |{{ .GetMode }} + {{- else -}} + |{{ .URL }}[{{ .Spec }}] |{{ .GetMode }} + {{- end }} {{- end }} {{- end }} |=== diff --git a/internal/format/templates/markdown_document.tmpl b/internal/format/templates/markdown_document.tmpl index 71bdb92..915f8c9 100644 --- a/internal/format/templates/markdown_document.tmpl +++ b/internal/format/templates/markdown_document.tmpl @@ -49,17 +49,21 @@ {{ end }} {{ end -}} -{{- if .Settings.ShowResources -}} +{{- if or .Settings.ShowResources .Settings.ShowDataSources -}} {{ indent 0 "#" }} Resources {{ if not .Module.Resources }} No resources. {{ else }} The following resources are used by this module: {{ range .Module.Resources }} - {{ if eq (len .URL) 0 }} - - {{ .Spec }} {{ printf "(%s)" .GetMode -}} - {{- else -}} - - [{{ .Spec }}]({{ .URL }}) {{ printf "(%s)" .GetMode -}} + {{- $isResource := and $.Settings.ShowResources ( eq "resource" (printf "%s" .GetMode)) }} + {{- $isDataResource := and $.Settings.ShowDataSources ( eq "data source" (printf "%s" .GetMode)) }} + {{- if or $isResource $isDataResource }} + {{ if eq (len .URL) 0 }} + - {{ .Spec }} {{ printf "(%s)" .GetMode -}} + {{- else -}} + - [{{ .Spec }}]({{ .URL }}) {{ printf "(%s)" .GetMode -}} + {{- end }} {{- end }} {{- end }} {{ end }} diff --git a/internal/format/templates/markdown_table.tmpl b/internal/format/templates/markdown_table.tmpl index ca42c35..9a628ad 100644 --- a/internal/format/templates/markdown_table.tmpl +++ b/internal/format/templates/markdown_table.tmpl @@ -44,7 +44,7 @@ {{ end }} {{ end -}} -{{- if .Settings.ShowResources -}} +{{- if or .Settings.ShowResources .Settings.ShowDataSources -}} {{ indent 0 "#" }} Resources {{ if not .Module.Resources }} No resources. @@ -52,10 +52,14 @@ | Name | Type | |------|------| {{- range .Module.Resources }} - {{ if eq (len .URL) 0 }} - | {{ .Spec }} | {{ .GetMode }} | - {{- else -}} - | [{{ .Spec }}]({{ .URL }}) | {{ .GetMode }} | + {{- $isResource := and $.Settings.ShowResources ( eq "resource" (printf "%s" .GetMode)) }} + {{- $isDataResource := and $.Settings.ShowDataSources ( eq "data source" (printf "%s" .GetMode)) }} + {{- if or $isResource $isDataResource }} + {{ if eq (len .URL) 0 }} + | {{ .Spec }} | {{ .GetMode }} | + {{- else -}} + | [{{ .Spec }}]({{ .URL }}) | {{ .GetMode }} | + {{- end }} {{- end }} {{- end }} {{ end }} diff --git a/internal/format/templates/pretty.tmpl b/internal/format/templates/pretty.tmpl index 368a478..8e8f199 100644 --- a/internal/format/templates/pretty.tmpl +++ b/internal/format/templates/pretty.tmpl @@ -30,20 +30,26 @@ {{- range . }} {{- printf "module.%s" .Name | colorize "\033[36m" }}{{ printf " (%s)" .FullName }} {{ end -}} - {{- printf "\n\n" -}} - {{ end -}} -{{ end -}} - -{{- if .Settings.ShowResources -}} - {{- with .Module.Resources }} - {{- range . }} - {{- $url := ternary .URL (printf " (%s)" .URL) "" }} - {{- printf "resource.%s (%s)" .Spec .GetMode | colorize "\033[36m" }}{{ $url }} - {{ end -}} {{ end -}} {{- printf "\n\n" -}} {{ end -}} +{{- if or .Settings.ShowResources .Settings.ShowDataSources -}} + {{- with .Module.Resources }} + {{- range . }} + {{- $isResource := and $.Settings.ShowResources ( eq "resource" (printf "%s" .GetMode)) }} + {{- $isDataResource := and $.Settings.ShowDataSources ( eq "data source" (printf "%s" .GetMode)) }} + {{- $url := ternary .URL (printf " (%s)" .URL) "" }} + {{- if $isResource }} + {{- printf "resource.%s (%s)" .Spec .GetMode | colorize "\033[36m" }}{{ $url }} + {{ end -}} + {{- if $isDataResource }} + {{- printf "data.%s (%s)" .Spec .GetMode | colorize "\033[36m" }}{{ $url }} + {{ end -}} + {{- end }} + {{ end }} +{{ end }} + {{- if .Settings.ShowInputs -}} {{- with .Module.Inputs }} {{- range . }} diff --git a/internal/format/testdata/asciidoc/document-OnlyDataSources.golden b/internal/format/testdata/asciidoc/document-OnlyDataSources.golden new file mode 100644 index 0000000..ceb7287 --- /dev/null +++ b/internal/format/testdata/asciidoc/document-OnlyDataSources.golden @@ -0,0 +1,6 @@ +== Resources + +The following resources are used by this module: + +- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.current] (data source) +- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.ident] (data source) \ No newline at end of file diff --git a/internal/format/testdata/asciidoc/document-OnlyResources.golden b/internal/format/testdata/asciidoc/document-OnlyResources.golden index 8ce302b..5072ace 100644 --- a/internal/format/testdata/asciidoc/document-OnlyResources.golden +++ b/internal/format/testdata/asciidoc/document-OnlyResources.golden @@ -3,6 +3,4 @@ The following resources are used by this module: - https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource[null_resource.foo] (resource) -- https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key[tls_private_key.baz] (resource) -- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.current] (data source) -- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.ident] (data source) \ No newline at end of file +- https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key[tls_private_key.baz] (resource) \ No newline at end of file diff --git a/internal/format/testdata/asciidoc/table-OnlyDataSources.golden b/internal/format/testdata/asciidoc/table-OnlyDataSources.golden new file mode 100644 index 0000000..f4bda5d --- /dev/null +++ b/internal/format/testdata/asciidoc/table-OnlyDataSources.golden @@ -0,0 +1,8 @@ +== Resources + +[cols="a,a",options="header,autowidth"] +|=== +|Name |Type +|https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.current] |data source +|https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.ident] |data source +|=== \ No newline at end of file diff --git a/internal/format/testdata/asciidoc/table-OnlyResources.golden b/internal/format/testdata/asciidoc/table-OnlyResources.golden index 915e2ef..cbea1b2 100644 --- a/internal/format/testdata/asciidoc/table-OnlyResources.golden +++ b/internal/format/testdata/asciidoc/table-OnlyResources.golden @@ -5,6 +5,4 @@ |Name |Type |https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource[null_resource.foo] |resource |https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key[tls_private_key.baz] |resource -|https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.current] |data source -|https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity[aws_caller_identity.ident] |data source |=== \ No newline at end of file diff --git a/internal/format/testdata/json/json-OnlyDataSources.golden b/internal/format/testdata/json/json-OnlyDataSources.golden new file mode 100644 index 0000000..3044e5f --- /dev/null +++ b/internal/format/testdata/json/json-OnlyDataSources.golden @@ -0,0 +1,27 @@ +{ + "header": "", + "footer": "", + "inputs": [], + "modules": [], + "outputs": [], + "providers": [], + "requirements": [], + "resources": [ + { + "type": "caller_identity", + "name": "current", + "provider": "aws", + "source": "hashicorp/aws", + "mode": "data", + "version": "latest" + }, + { + "type": "caller_identity", + "name": "ident", + "provider": "aws", + "source": "hashicorp/aws", + "mode": "data", + "version": "latest" + } + ] +} \ No newline at end of file diff --git a/internal/format/testdata/json/json-OnlyResources.golden b/internal/format/testdata/json/json-OnlyResources.golden index af2303a..b89017e 100644 --- a/internal/format/testdata/json/json-OnlyResources.golden +++ b/internal/format/testdata/json/json-OnlyResources.golden @@ -22,22 +22,6 @@ "source": "hashicorp/tls", "mode": "managed", "version": "latest" - }, - { - "type": "caller_identity", - "name": "current", - "provider": "aws", - "source": "hashicorp/aws", - "mode": "data", - "version": "latest" - }, - { - "type": "caller_identity", - "name": "ident", - "provider": "aws", - "source": "hashicorp/aws", - "mode": "data", - "version": "latest" } ] } \ No newline at end of file diff --git a/internal/format/testdata/markdown/document-OnlyDataSources.golden b/internal/format/testdata/markdown/document-OnlyDataSources.golden new file mode 100644 index 0000000..7ec4268 --- /dev/null +++ b/internal/format/testdata/markdown/document-OnlyDataSources.golden @@ -0,0 +1,6 @@ +## Resources + +The following resources are used by this module: + +- [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) (data source) +- [aws_caller_identity.ident](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) (data source) \ No newline at end of file diff --git a/internal/format/testdata/markdown/document-OnlyResources.golden b/internal/format/testdata/markdown/document-OnlyResources.golden index 368dbd3..830f74e 100644 --- a/internal/format/testdata/markdown/document-OnlyResources.golden +++ b/internal/format/testdata/markdown/document-OnlyResources.golden @@ -3,6 +3,4 @@ The following resources are used by this module: - [null_resource.foo](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) (resource) -- [tls_private_key.baz](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) (resource) -- [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) (data source) -- [aws_caller_identity.ident](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) (data source) \ No newline at end of file +- [tls_private_key.baz](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) (resource) \ No newline at end of file diff --git a/internal/format/testdata/markdown/table-OnlyDataSources.golden b/internal/format/testdata/markdown/table-OnlyDataSources.golden new file mode 100644 index 0000000..c648e5b --- /dev/null +++ b/internal/format/testdata/markdown/table-OnlyDataSources.golden @@ -0,0 +1,6 @@ +## Resources + +| Name | Type | +|------|------| +| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | +| [aws_caller_identity.ident](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | \ No newline at end of file diff --git a/internal/format/testdata/markdown/table-OnlyResources.golden b/internal/format/testdata/markdown/table-OnlyResources.golden index a04cd58..e3db822 100644 --- a/internal/format/testdata/markdown/table-OnlyResources.golden +++ b/internal/format/testdata/markdown/table-OnlyResources.golden @@ -3,6 +3,4 @@ | Name | Type | |------|------| | [null_resource.foo](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | -| [tls_private_key.baz](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource | -| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | -| [aws_caller_identity.ident](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | \ No newline at end of file +| [tls_private_key.baz](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource | \ No newline at end of file diff --git a/internal/format/testdata/pretty/pretty-Base.golden b/internal/format/testdata/pretty/pretty-Base.golden index 38e346f..cea3030 100644 --- a/internal/format/testdata/pretty/pretty-Base.golden +++ b/internal/format/testdata/pretty/pretty-Base.golden @@ -55,8 +55,8 @@ module.baz (baz,4.5.6) resource.null_resource.foo (resource) (https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) resource.tls_private_key.baz (resource) (https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) -resource.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) -resource.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) +data.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) +data.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) input.unquoted (required) diff --git a/internal/format/testdata/pretty/pretty-OnlyDataSources.golden b/internal/format/testdata/pretty/pretty-OnlyDataSources.golden new file mode 100644 index 0000000..e5ad756 --- /dev/null +++ b/internal/format/testdata/pretty/pretty-OnlyDataSources.golden @@ -0,0 +1,2 @@ +data.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) +data.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) \ No newline at end of file diff --git a/internal/format/testdata/pretty/pretty-OnlyResources.golden b/internal/format/testdata/pretty/pretty-OnlyResources.golden index 680fc41..53632ca 100644 --- a/internal/format/testdata/pretty/pretty-OnlyResources.golden +++ b/internal/format/testdata/pretty/pretty-OnlyResources.golden @@ -1,4 +1,2 @@ resource.null_resource.foo (resource) (https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) -resource.tls_private_key.baz (resource) (https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) -resource.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) -resource.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) \ No newline at end of file +resource.tls_private_key.baz (resource) (https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) \ No newline at end of file diff --git a/internal/format/testdata/pretty/pretty-WithColor.golden b/internal/format/testdata/pretty/pretty-WithColor.golden index f297405..a449a5f 100644 --- a/internal/format/testdata/pretty/pretty-WithColor.golden +++ b/internal/format/testdata/pretty/pretty-WithColor.golden @@ -55,8 +55,8 @@ followed by another line of text. resource.null_resource.foo (resource) (https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) resource.tls_private_key.baz (resource) (https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) -resource.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) -resource.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) +data.aws_caller_identity.current (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) +data.aws_caller_identity.ident (data source) (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) input.unquoted (required) diff --git a/internal/format/testdata/toml/toml-OnlyDataSources.golden b/internal/format/testdata/toml/toml-OnlyDataSources.golden new file mode 100644 index 0000000..637f215 --- /dev/null +++ b/internal/format/testdata/toml/toml-OnlyDataSources.golden @@ -0,0 +1,23 @@ +header = "" +footer = "" +inputs = [] +modules = [] +outputs = [] +providers = [] +requirements = [] + +[[resources]] + type = "caller_identity" + name = "current" + provider = "aws" + source = "hashicorp/aws" + mode = "data" + version = "latest" + +[[resources]] + type = "caller_identity" + name = "ident" + provider = "aws" + source = "hashicorp/aws" + mode = "data" + version = "latest" \ No newline at end of file diff --git a/internal/format/testdata/toml/toml-OnlyResources.golden b/internal/format/testdata/toml/toml-OnlyResources.golden index 7f7f16e..7dd61aa 100644 --- a/internal/format/testdata/toml/toml-OnlyResources.golden +++ b/internal/format/testdata/toml/toml-OnlyResources.golden @@ -20,20 +20,4 @@ requirements = [] provider = "tls" source = "hashicorp/tls" mode = "managed" - version = "latest" - -[[resources]] - type = "caller_identity" - name = "current" - provider = "aws" - source = "hashicorp/aws" - mode = "data" - version = "latest" - -[[resources]] - type = "caller_identity" - name = "ident" - provider = "aws" - source = "hashicorp/aws" - mode = "data" version = "latest" \ No newline at end of file diff --git a/internal/format/testdata/xml/xml-OnlyDataSources.golden b/internal/format/testdata/xml/xml-OnlyDataSources.golden new file mode 100644 index 0000000..d4d0cc4 --- /dev/null +++ b/internal/format/testdata/xml/xml-OnlyDataSources.golden @@ -0,0 +1,27 @@ + +
+
+ + + + + + + + caller_identity + current + aws + hashicorp/aws + data + latest + + + caller_identity + ident + aws + hashicorp/aws + data + latest + + +
\ No newline at end of file diff --git a/internal/format/testdata/xml/xml-OnlyResources.golden b/internal/format/testdata/xml/xml-OnlyResources.golden index ad10a5e..c8015da 100644 --- a/internal/format/testdata/xml/xml-OnlyResources.golden +++ b/internal/format/testdata/xml/xml-OnlyResources.golden @@ -23,21 +23,5 @@ managed latest - - caller_identity - current - aws - hashicorp/aws - data - latest - - - caller_identity - ident - aws - hashicorp/aws - data - latest - \ No newline at end of file diff --git a/internal/format/testdata/yaml/yaml-OnlyDataSources.golden b/internal/format/testdata/yaml/yaml-OnlyDataSources.golden new file mode 100644 index 0000000..6267cda --- /dev/null +++ b/internal/format/testdata/yaml/yaml-OnlyDataSources.golden @@ -0,0 +1,20 @@ +header: "" +footer: "" +inputs: [] +modules: [] +outputs: [] +providers: [] +requirements: [] +resources: + - type: caller_identity + name: current + provider: aws + source: hashicorp/aws + mode: data + version: latest + - type: caller_identity + name: ident + provider: aws + source: hashicorp/aws + mode: data + version: latest \ No newline at end of file diff --git a/internal/format/testdata/yaml/yaml-OnlyResources.golden b/internal/format/testdata/yaml/yaml-OnlyResources.golden index d000f11..69275ae 100644 --- a/internal/format/testdata/yaml/yaml-OnlyResources.golden +++ b/internal/format/testdata/yaml/yaml-OnlyResources.golden @@ -17,16 +17,4 @@ resources: provider: tls source: hashicorp/tls mode: managed - version: latest - - type: caller_identity - name: current - provider: aws - source: hashicorp/aws - mode: data - version: latest - - type: caller_identity - name: ident - provider: aws - source: hashicorp/aws - mode: data version: latest \ No newline at end of file diff --git a/internal/format/toml.go b/internal/format/toml.go index e92eb3b..638f2c7 100644 --- a/internal/format/toml.go +++ b/internal/format/toml.go @@ -30,41 +30,18 @@ func NewTOML(settings *print.Settings) print.Engine { // Print a Terraform module as toml. func (t *TOML) Print(module *terraform.Module, settings *print.Settings) (string, error) { - copy := terraform.Module{ + copy := &terraform.Module{ Header: "", Footer: "", - Providers: make([]*terraform.Provider, 0), Inputs: make([]*terraform.Input, 0), ModuleCalls: make([]*terraform.ModuleCall, 0), Outputs: make([]*terraform.Output, 0), + Providers: make([]*terraform.Provider, 0), Requirements: make([]*terraform.Requirement, 0), Resources: make([]*terraform.Resource, 0), } - if settings.ShowHeader { - copy.Header = module.Header - } - if settings.ShowFooter { - copy.Footer = module.Footer - } - if settings.ShowInputs { - copy.Inputs = module.Inputs - } - if settings.ShowModuleCalls { - copy.ModuleCalls = module.ModuleCalls - } - if settings.ShowOutputs { - copy.Outputs = module.Outputs - } - if settings.ShowProviders { - copy.Providers = module.Providers - } - if settings.ShowRequirements { - copy.Requirements = module.Requirements - } - if settings.ShowResources { - copy.Resources = module.Resources - } + print.CopySections(settings, module, copy) buffer := new(bytes.Buffer) encoder := toml.NewEncoder(buffer) diff --git a/internal/format/toml_test.go b/internal/format/toml_test.go index 0379543..b35008f 100644 --- a/internal/format/toml_test.go +++ b/internal/format/toml_test.go @@ -61,6 +61,10 @@ func TestToml(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/xml.go b/internal/format/xml.go index 17991ba..77a2db0 100644 --- a/internal/format/xml.go +++ b/internal/format/xml.go @@ -39,30 +39,7 @@ func (x *XML) Print(module *terraform.Module, settings *print.Settings) (string, Resources: make([]*terraform.Resource, 0), } - if settings.ShowHeader { - copy.Header = module.Header - } - if settings.ShowFooter { - copy.Footer = module.Footer - } - if settings.ShowInputs { - copy.Inputs = module.Inputs - } - if settings.ShowModuleCalls { - copy.ModuleCalls = module.ModuleCalls - } - if settings.ShowOutputs { - copy.Outputs = module.Outputs - } - if settings.ShowProviders { - copy.Providers = module.Providers - } - if settings.ShowRequirements { - copy.Requirements = module.Requirements - } - if settings.ShowResources { - copy.Resources = module.Resources - } + print.CopySections(settings, module, copy) out, err := xml.MarshalIndent(copy, "", " ") if err != nil { diff --git a/internal/format/xml_test.go b/internal/format/xml_test.go index 36400ee..71311d7 100644 --- a/internal/format/xml_test.go +++ b/internal/format/xml_test.go @@ -61,6 +61,10 @@ func TestXml(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/format/yaml.go b/internal/format/yaml.go index 7e2773f..8cf608c 100644 --- a/internal/format/yaml.go +++ b/internal/format/yaml.go @@ -41,30 +41,7 @@ func (y *YAML) Print(module *terraform.Module, settings *print.Settings) (string Resources: make([]*terraform.Resource, 0), } - if settings.ShowHeader { - copy.Header = module.Header - } - if settings.ShowFooter { - copy.Footer = module.Footer - } - if settings.ShowInputs { - copy.Inputs = module.Inputs - } - if settings.ShowModuleCalls { - copy.ModuleCalls = module.ModuleCalls - } - if settings.ShowOutputs { - copy.Outputs = module.Outputs - } - if settings.ShowProviders { - copy.Providers = module.Providers - } - if settings.ShowRequirements { - copy.Requirements = module.Requirements - } - if settings.ShowResources { - copy.Resources = module.Resources - } + print.CopySections(settings, module, copy) buffer := new(bytes.Buffer) diff --git a/internal/format/yaml_test.go b/internal/format/yaml_test.go index 1b93485..ddcecd6 100644 --- a/internal/format/yaml_test.go +++ b/internal/format/yaml_test.go @@ -61,6 +61,10 @@ func TestYaml(t *testing.T) { }, // Only section + "OnlyDataSources": { + settings: print.Settings{ShowDataSources: true}, + options: terraform.Options{}, + }, "OnlyHeader": { settings: print.Settings{ShowHeader: true}, options: terraform.Options{}, diff --git a/internal/print/settings.go b/internal/print/settings.go index 8f533aa..7cf0a74 100644 --- a/internal/print/settings.go +++ b/internal/print/settings.go @@ -12,6 +12,7 @@ package print import ( printsdk "github.com/terraform-docs/plugin-sdk/print" + "github.com/terraform-docs/terraform-docs/internal/terraform" ) // Settings represents all settings. @@ -46,11 +47,11 @@ type Settings struct { // scope: Pretty ShowColor bool - // ShowDescription show "Descriptions on variables" column + // ShowDatasources show the data sources on the "Resources" section // - // default: false - // scope: tfvars hcl - ShowDescription bool + // default: true + // scope: Global + ShowDataSources bool // ShowDefault show "Default" column // @@ -58,6 +59,12 @@ type Settings struct { // scope: Asciidoc, Markdown ShowDefault bool + // ShowDescription show "Descriptions on variables" column + // + // default: false + // scope: tfvars hcl + ShowDescription bool + // ShowFooter show "Footer" module information // // default: false @@ -133,6 +140,7 @@ func DefaultSettings() *Settings { OutputValues: false, ShowAnchor: true, ShowColor: true, + ShowDataSources: true, ShowDefault: true, ShowDescription: false, ShowFooter: false, @@ -156,6 +164,7 @@ func (s *Settings) Convert() *printsdk.Settings { IndentLevel: s.IndentLevel, OutputValues: s.OutputValues, ShowColor: s.ShowColor, + ShowDataSources: s.ShowDataSources, ShowDefault: s.ShowDefault, ShowFooter: s.ShowFooter, ShowHeader: s.ShowHeader, @@ -170,3 +179,45 @@ func (s *Settings) Convert() *printsdk.Settings { ShowType: s.ShowType, } } + +// CopySections sets the sections that'll be printed +func CopySections(settings *Settings, src *terraform.Module, dest *terraform.Module) { + if settings.ShowHeader { + dest.Header = src.Header + } + if settings.ShowFooter { + dest.Footer = src.Footer + } + if settings.ShowInputs { + dest.Inputs = src.Inputs + } + if settings.ShowModuleCalls { + dest.ModuleCalls = src.ModuleCalls + } + if settings.ShowOutputs { + dest.Outputs = src.Outputs + } + if settings.ShowProviders { + dest.Providers = src.Providers + } + if settings.ShowRequirements { + dest.Requirements = src.Requirements + } + if settings.ShowResources || settings.ShowDataSources { + dest.Resources = filterResourcesByMode(settings, src.Resources) + } +} + +// filterResourcesByMode returns the managed or data resources defined by the show argument +func filterResourcesByMode(settings *Settings, module []*terraform.Resource) []*terraform.Resource { + resources := make([]*terraform.Resource, 0) + for _, r := range module { + if settings.ShowResources && r.Mode == "managed" { + resources = append(resources, r) + } + if settings.ShowDataSources && r.Mode == "data" { + resources = append(resources, r) + } + } + return resources +} diff --git a/internal/testutil/settings.go b/internal/testutil/settings.go index fe30f77..18e210f 100644 --- a/internal/testutil/settings.go +++ b/internal/testutil/settings.go @@ -19,6 +19,7 @@ import ( // WithSections appends show all sections to provided Settings. func WithSections(override ...print.Settings) print.Settings { base := print.Settings{ + ShowDataSources: true, ShowFooter: true, ShowHeader: true, ShowInputs: true,