From f3c92384a1f44ee7776625a6c3cc02a15e23ddb2 Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Wed, 29 Sep 2021 13:55:50 -0400 Subject: [PATCH] Extract leading comments for resources and modules Signed-off-by: Khosrow Moossavi --- docs/reference/json.md | 27 +++++++++++------ docs/reference/toml.md | 9 ++++++ docs/reference/xml.md | 9 ++++++ docs/reference/yaml.md | 9 ++++++ examples/main.tf | 3 ++ format/testdata/json/json-Base.golden | 27 +++++++++++------ .../json/json-EscapeCharacters.golden | 27 +++++++++++------ .../testdata/json/json-OnlyDataSources.golden | 6 ++-- .../testdata/json/json-OnlyModulecalls.golden | 12 +++++--- .../testdata/json/json-OnlyResources.golden | 9 ++++-- format/testdata/toml/toml-Base.golden | 11 ++++++- .../testdata/toml/toml-OnlyDataSources.golden | 4 ++- .../testdata/toml/toml-OnlyModulecalls.golden | 6 +++- .../testdata/toml/toml-OnlyResources.golden | 5 +++- format/testdata/xml/xml-Base.golden | 9 ++++++ .../testdata/xml/xml-OnlyDataSources.golden | 2 ++ .../testdata/xml/xml-OnlyModulecalls.golden | 4 +++ format/testdata/xml/xml-OnlyResources.golden | 3 ++ format/testdata/yaml/yaml-Base.golden | 11 ++++++- .../testdata/yaml/yaml-OnlyDataSources.golden | 4 ++- .../testdata/yaml/yaml-OnlyModulecalls.golden | 4 +++ .../testdata/yaml/yaml-OnlyResources.golden | 5 +++- terraform/load.go | 30 ++++++++++++++----- terraform/load_test.go | 4 ++- terraform/modulecall.go | 10 ++++--- terraform/resource.go | 1 + 26 files changed, 196 insertions(+), 55 deletions(-) diff --git a/docs/reference/json.md b/docs/reference/json.md index 6659c84..fe4583e 100644 --- a/docs/reference/json.md +++ b/docs/reference/json.md @@ -306,22 +306,26 @@ generates the following output: { "name": "bar", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "baz", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foo", "source": "bar", - "version": "1.2.3" + "version": "1.2.3", + "description": "another type of description for module foo" }, { "name": "foobar", "source": "git@github.com:module/path", - "version": "v7.8.9" + "version": "v7.8.9", + "description": null } ], "outputs": [ @@ -394,7 +398,8 @@ generates the following output: "provider": "foo", "source": "https://registry.acme.com/foo", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "resource", @@ -402,7 +407,8 @@ generates the following output: "provider": "null", "source": "hashicorp/null", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "private_key", @@ -410,7 +416,8 @@ generates the following output: "provider": "tls", "source": "hashicorp/tls", "mode": "managed", - "version": "latest" + "version": "latest", + "description": "this description for tls_private_key.baz which can be multiline." }, { "type": "caller_identity", @@ -418,7 +425,8 @@ generates the following output: "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null }, { "type": "caller_identity", @@ -426,7 +434,8 @@ generates the following output: "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null } ] } diff --git a/docs/reference/toml.md b/docs/reference/toml.md index 75c3eb6..a0c649d 100644 --- a/docs/reference/toml.md +++ b/docs/reference/toml.md @@ -290,21 +290,25 @@ generates the following output: name = "bar" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "baz" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "foo" source = "bar" version = "1.2.3" + description = "another type of description for module foo" [[modules]] name = "foobar" source = "git@github.com:module/path" version = "v7.8.9" + description = "" [[outputs]] name = "output-0.12" @@ -370,6 +374,7 @@ generates the following output: source = "https://registry.acme.com/foo" mode = "managed" version = "latest" + description = "" [[resources]] type = "resource" @@ -378,6 +383,7 @@ generates the following output: source = "hashicorp/null" mode = "managed" version = "latest" + description = "" [[resources]] type = "private_key" @@ -386,6 +392,7 @@ generates the following output: source = "hashicorp/tls" mode = "managed" version = "latest" + description = "this description for tls_private_key.baz which can be multiline." [[resources]] type = "caller_identity" @@ -394,6 +401,7 @@ generates the following output: source = "hashicorp/aws" mode = "data" version = "latest" + description = "" [[resources]] type = "caller_identity" @@ -402,5 +410,6 @@ generates the following output: source = "hashicorp/aws" mode = "data" version = "latest" + description = "" [examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples diff --git a/docs/reference/xml.md b/docs/reference/xml.md index 9788b53..633f124 100644 --- a/docs/reference/xml.md +++ b/docs/reference/xml.md @@ -306,21 +306,25 @@ generates the following output: bar baz 4.5.6 + baz baz 4.5.6 + foo bar 1.2.3 + another type of description for module foo foobar git@github.com:module/path v7.8.9 + @@ -394,6 +398,7 @@ generates the following output: https://registry.acme.com/foo managed latest + resource @@ -402,6 +407,7 @@ generates the following output: hashicorp/null managed latest + private_key @@ -410,6 +416,7 @@ generates the following output: hashicorp/tls managed latest + this description for tls_private_key.baz which can be multiline. caller_identity @@ -418,6 +425,7 @@ generates the following output: hashicorp/aws data latest + caller_identity @@ -426,6 +434,7 @@ generates the following output: hashicorp/aws data latest + diff --git a/docs/reference/yaml.md b/docs/reference/yaml.md index 79a1667..577de59 100644 --- a/docs/reference/yaml.md +++ b/docs/reference/yaml.md @@ -284,15 +284,19 @@ generates the following output: - name: bar source: baz version: 4.5.6 + description: null - name: baz source: baz version: 4.5.6 + description: null - name: foo source: bar version: 1.2.3 + description: another type of description for module foo - name: foobar source: git@github.com:module/path version: v7.8.9 + description: null outputs: - name: output-0.12 description: terraform 0.12 only @@ -334,29 +338,34 @@ generates the following output: source: https://registry.acme.com/foo mode: managed version: latest + description: null - type: resource name: foo provider: "null" source: hashicorp/null mode: managed version: latest + description: null - type: private_key name: baz provider: tls source: hashicorp/tls mode: managed version: latest + description: this description for tls_private_key.baz which can be multiline. - type: caller_identity name: current provider: aws source: hashicorp/aws mode: data version: latest + description: null - type: caller_identity name: ident provider: aws source: hashicorp/aws mode: data version: latest + description: null [examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples diff --git a/examples/main.tf b/examples/main.tf index 438092a..8b31f04 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -50,6 +50,8 @@ terraform { } } +// this description for tls_private_key.baz +// which can be multiline. resource "tls_private_key" "baz" {} resource "foo_resource" "baz" {} @@ -68,6 +70,7 @@ module "bar" { version = "4.5.6" } +# another type of description for module foo module "foo" { source = "bar" version = "1.2.3" diff --git a/format/testdata/json/json-Base.golden b/format/testdata/json/json-Base.golden index df5a604..5168994 100644 --- a/format/testdata/json/json-Base.golden +++ b/format/testdata/json/json-Base.golden @@ -249,22 +249,26 @@ { "name": "bar", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foo", "source": "bar", - "version": "1.2.3" + "version": "1.2.3", + "description": "another type of description for module foo" }, { "name": "baz", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foobar", "source": "git@github.com:module/path", - "version": "v7.8.9" + "version": "v7.8.9", + "description": null } ], "outputs": [ @@ -337,7 +341,8 @@ "provider": "foo", "source": "https://registry.acme.com/foo", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "resource", @@ -345,7 +350,8 @@ "provider": "null", "source": "hashicorp/null", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "private_key", @@ -353,7 +359,8 @@ "provider": "tls", "source": "hashicorp/tls", "mode": "managed", - "version": "latest" + "version": "latest", + "description": "this description for tls_private_key.baz which can be multiline." }, { "type": "caller_identity", @@ -361,7 +368,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null }, { "type": "caller_identity", @@ -369,7 +377,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null } ] } \ No newline at end of file diff --git a/format/testdata/json/json-EscapeCharacters.golden b/format/testdata/json/json-EscapeCharacters.golden index c0c6212..99135f9 100644 --- a/format/testdata/json/json-EscapeCharacters.golden +++ b/format/testdata/json/json-EscapeCharacters.golden @@ -249,22 +249,26 @@ { "name": "bar", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foo", "source": "bar", - "version": "1.2.3" + "version": "1.2.3", + "description": "another type of description for module foo" }, { "name": "baz", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foobar", "source": "git@github.com:module/path", - "version": "v7.8.9" + "version": "v7.8.9", + "description": null } ], "outputs": [ @@ -337,7 +341,8 @@ "provider": "foo", "source": "https://registry.acme.com/foo", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "resource", @@ -345,7 +350,8 @@ "provider": "null", "source": "hashicorp/null", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "private_key", @@ -353,7 +359,8 @@ "provider": "tls", "source": "hashicorp/tls", "mode": "managed", - "version": "latest" + "version": "latest", + "description": "this description for tls_private_key.baz which can be multiline." }, { "type": "caller_identity", @@ -361,7 +368,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null }, { "type": "caller_identity", @@ -369,7 +377,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null } ] } \ No newline at end of file diff --git a/format/testdata/json/json-OnlyDataSources.golden b/format/testdata/json/json-OnlyDataSources.golden index 3044e5f..57d9173 100644 --- a/format/testdata/json/json-OnlyDataSources.golden +++ b/format/testdata/json/json-OnlyDataSources.golden @@ -13,7 +13,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null }, { "type": "caller_identity", @@ -21,7 +22,8 @@ "provider": "aws", "source": "hashicorp/aws", "mode": "data", - "version": "latest" + "version": "latest", + "description": null } ] } \ No newline at end of file diff --git a/format/testdata/json/json-OnlyModulecalls.golden b/format/testdata/json/json-OnlyModulecalls.golden index 0cf5df6..6bdcc6a 100644 --- a/format/testdata/json/json-OnlyModulecalls.golden +++ b/format/testdata/json/json-OnlyModulecalls.golden @@ -6,22 +6,26 @@ { "name": "bar", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foo", "source": "bar", - "version": "1.2.3" + "version": "1.2.3", + "description": "another type of description for module foo" }, { "name": "baz", "source": "baz", - "version": "4.5.6" + "version": "4.5.6", + "description": null }, { "name": "foobar", "source": "git@github.com:module/path", - "version": "v7.8.9" + "version": "v7.8.9", + "description": null } ], "outputs": [], diff --git a/format/testdata/json/json-OnlyResources.golden b/format/testdata/json/json-OnlyResources.golden index 90475ed..3e65cdf 100644 --- a/format/testdata/json/json-OnlyResources.golden +++ b/format/testdata/json/json-OnlyResources.golden @@ -13,7 +13,8 @@ "provider": "foo", "source": "https://registry.acme.com/foo", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "resource", @@ -21,7 +22,8 @@ "provider": "null", "source": "hashicorp/null", "mode": "managed", - "version": "latest" + "version": "latest", + "description": null }, { "type": "private_key", @@ -29,7 +31,8 @@ "provider": "tls", "source": "hashicorp/tls", "mode": "managed", - "version": "latest" + "version": "latest", + "description": "this description for tls_private_key.baz which can be multiline." } ] } \ No newline at end of file diff --git a/format/testdata/toml/toml-Base.golden b/format/testdata/toml/toml-Base.golden index 3141860..b5f33c4 100644 --- a/format/testdata/toml/toml-Base.golden +++ b/format/testdata/toml/toml-Base.golden @@ -234,21 +234,25 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b name = "bar" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "foo" source = "bar" version = "1.2.3" + description = "another type of description for module foo" [[modules]] name = "baz" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "foobar" source = "git@github.com:module/path" version = "v7.8.9" + description = "" [[outputs]] name = "unquoted" @@ -314,6 +318,7 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b source = "https://registry.acme.com/foo" mode = "managed" version = "latest" + description = "" [[resources]] type = "resource" @@ -322,6 +327,7 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b source = "hashicorp/null" mode = "managed" version = "latest" + description = "" [[resources]] type = "private_key" @@ -330,6 +336,7 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b source = "hashicorp/tls" mode = "managed" version = "latest" + description = "this description for tls_private_key.baz which can be multiline." [[resources]] type = "caller_identity" @@ -338,6 +345,7 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b source = "hashicorp/aws" mode = "data" version = "latest" + description = "" [[resources]] type = "caller_identity" @@ -345,4 +353,5 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b provider = "aws" source = "hashicorp/aws" mode = "data" - version = "latest" \ No newline at end of file + version = "latest" + description = "" \ No newline at end of file diff --git a/format/testdata/toml/toml-OnlyDataSources.golden b/format/testdata/toml/toml-OnlyDataSources.golden index 637f215..7bbed1a 100644 --- a/format/testdata/toml/toml-OnlyDataSources.golden +++ b/format/testdata/toml/toml-OnlyDataSources.golden @@ -13,6 +13,7 @@ requirements = [] source = "hashicorp/aws" mode = "data" version = "latest" + description = "" [[resources]] type = "caller_identity" @@ -20,4 +21,5 @@ requirements = [] provider = "aws" source = "hashicorp/aws" mode = "data" - version = "latest" \ No newline at end of file + version = "latest" + description = "" \ No newline at end of file diff --git a/format/testdata/toml/toml-OnlyModulecalls.golden b/format/testdata/toml/toml-OnlyModulecalls.golden index df27b2b..fa343a3 100644 --- a/format/testdata/toml/toml-OnlyModulecalls.golden +++ b/format/testdata/toml/toml-OnlyModulecalls.golden @@ -10,18 +10,22 @@ resources = [] name = "bar" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "foo" source = "bar" version = "1.2.3" + description = "another type of description for module foo" [[modules]] name = "baz" source = "baz" version = "4.5.6" + description = "" [[modules]] name = "foobar" source = "git@github.com:module/path" - version = "v7.8.9" \ No newline at end of file + version = "v7.8.9" + description = "" \ No newline at end of file diff --git a/format/testdata/toml/toml-OnlyResources.golden b/format/testdata/toml/toml-OnlyResources.golden index ec2319d..fa6cfa1 100644 --- a/format/testdata/toml/toml-OnlyResources.golden +++ b/format/testdata/toml/toml-OnlyResources.golden @@ -13,6 +13,7 @@ requirements = [] source = "https://registry.acme.com/foo" mode = "managed" version = "latest" + description = "" [[resources]] type = "resource" @@ -21,6 +22,7 @@ requirements = [] source = "hashicorp/null" mode = "managed" version = "latest" + description = "" [[resources]] type = "private_key" @@ -28,4 +30,5 @@ requirements = [] provider = "tls" source = "hashicorp/tls" mode = "managed" - version = "latest" \ No newline at end of file + version = "latest" + description = "this description for tls_private_key.baz which can be multiline." \ No newline at end of file diff --git a/format/testdata/xml/xml-Base.golden b/format/testdata/xml/xml-Base.golden index a61a110..fbc9b51 100644 --- a/format/testdata/xml/xml-Base.golden +++ b/format/testdata/xml/xml-Base.golden @@ -250,21 +250,25 @@ bar baz 4.5.6 + foo bar 1.2.3 + another type of description for module foo baz baz 4.5.6 + foobar git@github.com:module/path v7.8.9 + @@ -338,6 +342,7 @@ https://registry.acme.com/foo managed latest + resource @@ -346,6 +351,7 @@ hashicorp/null managed latest + private_key @@ -354,6 +360,7 @@ hashicorp/tls managed latest + this description for tls_private_key.baz which can be multiline. caller_identity @@ -362,6 +369,7 @@ hashicorp/aws data latest + caller_identity @@ -370,6 +378,7 @@ hashicorp/aws data latest + \ No newline at end of file diff --git a/format/testdata/xml/xml-OnlyDataSources.golden b/format/testdata/xml/xml-OnlyDataSources.golden index d4d0cc4..e5ff7ad 100644 --- a/format/testdata/xml/xml-OnlyDataSources.golden +++ b/format/testdata/xml/xml-OnlyDataSources.golden @@ -14,6 +14,7 @@ hashicorp/aws data latest + caller_identity @@ -22,6 +23,7 @@ hashicorp/aws data latest + \ No newline at end of file diff --git a/format/testdata/xml/xml-OnlyModulecalls.golden b/format/testdata/xml/xml-OnlyModulecalls.golden index a646ec4..8bf5df2 100644 --- a/format/testdata/xml/xml-OnlyModulecalls.golden +++ b/format/testdata/xml/xml-OnlyModulecalls.golden @@ -7,21 +7,25 @@ bar baz 4.5.6 + foo bar 1.2.3 + another type of description for module foo baz baz 4.5.6 + foobar git@github.com:module/path v7.8.9 + diff --git a/format/testdata/xml/xml-OnlyResources.golden b/format/testdata/xml/xml-OnlyResources.golden index 4bdd544..b1029a2 100644 --- a/format/testdata/xml/xml-OnlyResources.golden +++ b/format/testdata/xml/xml-OnlyResources.golden @@ -14,6 +14,7 @@ https://registry.acme.com/foo managed latest + resource @@ -22,6 +23,7 @@ hashicorp/null managed latest + private_key @@ -30,6 +32,7 @@ hashicorp/tls managed latest + this description for tls_private_key.baz which can be multiline. \ No newline at end of file diff --git a/format/testdata/yaml/yaml-Base.golden b/format/testdata/yaml/yaml-Base.golden index af1e057..8bb5334 100644 --- a/format/testdata/yaml/yaml-Base.golden +++ b/format/testdata/yaml/yaml-Base.golden @@ -228,15 +228,19 @@ modules: - name: bar source: baz version: 4.5.6 + description: null - name: foo source: bar version: 1.2.3 + description: another type of description for module foo - name: baz source: baz version: 4.5.6 + description: null - name: foobar source: git@github.com:module/path version: v7.8.9 + description: null outputs: - name: unquoted description: It's unquoted output. @@ -278,27 +282,32 @@ resources: source: https://registry.acme.com/foo mode: managed version: latest + description: null - type: resource name: foo provider: "null" source: hashicorp/null mode: managed version: latest + description: null - type: private_key name: baz provider: tls source: hashicorp/tls mode: managed version: latest + description: this description for tls_private_key.baz which can be multiline. - type: caller_identity name: current provider: aws source: hashicorp/aws mode: data version: latest + description: null - type: caller_identity name: ident provider: aws source: hashicorp/aws mode: data - version: latest \ No newline at end of file + version: latest + description: null \ No newline at end of file diff --git a/format/testdata/yaml/yaml-OnlyDataSources.golden b/format/testdata/yaml/yaml-OnlyDataSources.golden index 6267cda..f9514fd 100644 --- a/format/testdata/yaml/yaml-OnlyDataSources.golden +++ b/format/testdata/yaml/yaml-OnlyDataSources.golden @@ -12,9 +12,11 @@ resources: source: hashicorp/aws mode: data version: latest + description: null - type: caller_identity name: ident provider: aws source: hashicorp/aws mode: data - version: latest \ No newline at end of file + version: latest + description: null \ No newline at end of file diff --git a/format/testdata/yaml/yaml-OnlyModulecalls.golden b/format/testdata/yaml/yaml-OnlyModulecalls.golden index 939bbf4..db6c5ca 100644 --- a/format/testdata/yaml/yaml-OnlyModulecalls.golden +++ b/format/testdata/yaml/yaml-OnlyModulecalls.golden @@ -5,15 +5,19 @@ modules: - name: bar source: baz version: 4.5.6 + description: null - name: foo source: bar version: 1.2.3 + description: another type of description for module foo - name: baz source: baz version: 4.5.6 + description: null - name: foobar source: git@github.com:module/path version: v7.8.9 + description: null outputs: [] providers: [] requirements: [] diff --git a/format/testdata/yaml/yaml-OnlyResources.golden b/format/testdata/yaml/yaml-OnlyResources.golden index 02e2a0d..835f4e2 100644 --- a/format/testdata/yaml/yaml-OnlyResources.golden +++ b/format/testdata/yaml/yaml-OnlyResources.golden @@ -12,15 +12,18 @@ resources: source: https://registry.acme.com/foo mode: managed version: latest + description: null - type: resource name: foo provider: "null" source: hashicorp/null mode: managed version: latest + description: null - type: private_key name: baz provider: tls source: hashicorp/tls mode: managed - version: latest \ No newline at end of file + version: latest + description: this description for tls_private_key.baz which can be multiline. \ No newline at end of file diff --git a/terraform/load.go b/terraform/load.go index 2c36f22..7cfa1f7 100644 --- a/terraform/load.go +++ b/terraform/load.go @@ -66,14 +66,14 @@ func loadModuleItems(tfmodule *tfconfig.Module, config *print.Config) (*Module, } inputs, required, optional := loadInputs(tfmodule, config) - modulecalls := loadModulecalls(tfmodule) + modulecalls := loadModulecalls(tfmodule, config) outputs, err := loadOutputs(tfmodule, config) if err != nil { return nil, err } providers := loadProviders(tfmodule, config) requirements := loadRequirements(tfmodule) - resources := loadResources(tfmodule) + resources := loadResources(tfmodule, config) return &Module{ Header: header, @@ -241,16 +241,23 @@ func formatSource(s, v string) (source, version string) { return source, version } -func loadModulecalls(tfmodule *tfconfig.Module) []*ModuleCall { +func loadModulecalls(tfmodule *tfconfig.Module, config *print.Config) []*ModuleCall { var modules = make([]*ModuleCall, 0) var source, version string for _, m := range tfmodule.ModuleCalls { source, version = formatSource(m.Source, m.Version) + + description := "" + if config.Settings.ReadComments { + description = loadComments(m.Pos.Filename, m.Pos.Line) + } + modules = append(modules, &ModuleCall{ - Name: m.Name, - Source: source, - Version: version, + Name: m.Name, + Source: source, + Version: version, + Description: types.String(description), Position: Position{ Filename: m.Pos.Filename, Line: m.Pos.Line, @@ -275,6 +282,7 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er if description == "" && config.Settings.ReadComments { description = loadComments(o.Pos.Filename, o.Pos.Line) } + output := &Output{ Name: o.Name, Description: types.String(description), @@ -284,6 +292,7 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er }, ShowValue: config.OutputValues.Enabled, } + if config.OutputValues.Enabled { output.Sensitive = values[output.Name].Sensitive if values[output.Name].Sensitive { @@ -401,7 +410,7 @@ func loadRequirements(tfmodule *tfconfig.Module) []*Requirement { return requirements } -func loadResources(tfmodule *tfconfig.Module) []*Resource { +func loadResources(tfmodule *tfconfig.Module, config *print.Config) []*Resource { allResources := []map[string]*tfconfig.Resource{tfmodule.ManagedResources, tfmodule.DataResources} discovered := make(map[string]*Resource) @@ -421,6 +430,12 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource { rType := strings.TrimPrefix(r.Type, r.Provider.Name+"_") key := fmt.Sprintf("%s.%s.%s.%s", r.Provider.Name, r.Mode, rType, r.Name) + + description := "" + if config.Settings.ReadComments { + description = loadComments(r.Pos.Filename, r.Pos.Line) + } + discovered[key] = &Resource{ Type: rType, Name: r.Name, @@ -428,6 +443,7 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource { ProviderName: r.Provider.Name, ProviderSource: source, Version: types.String(version), + Description: types.String(description), Position: Position{ Filename: r.Pos.Filename, Line: r.Pos.Line, diff --git a/terraform/load_test.go b/terraform/load_test.go index e2c1802..984acd5 100644 --- a/terraform/load_test.go +++ b/terraform/load_test.go @@ -535,8 +535,10 @@ func TestLoadModulecalls(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { assert := assert.New(t) + + config := print.NewConfig() module, _ := loadModule(filepath.Join("testdata", tt.path)) - modulecalls := loadModulecalls(module) + modulecalls := loadModulecalls(module, config) assert.Equal(tt.expected, len(modulecalls)) }) diff --git a/terraform/modulecall.go b/terraform/modulecall.go index 36ac195..fa2053f 100644 --- a/terraform/modulecall.go +++ b/terraform/modulecall.go @@ -15,15 +15,17 @@ import ( "sort" terraformsdk "github.com/terraform-docs/plugin-sdk/terraform" + "github.com/terraform-docs/terraform-docs/internal/types" "github.com/terraform-docs/terraform-docs/print" ) // ModuleCall represents a submodule called by Terraform module. type ModuleCall struct { - Name string `json:"name" toml:"name" xml:"name" yaml:"name"` - Source string `json:"source" toml:"source" xml:"source" yaml:"source"` - Version string `json:"version" toml:"version" xml:"version" yaml:"version"` - Position Position `json:"-" toml:"-" xml:"-" yaml:"-"` + Name string `json:"name" toml:"name" xml:"name" yaml:"name"` + Source string `json:"source" toml:"source" xml:"source" yaml:"source"` + Version string `json:"version" toml:"version" xml:"version" yaml:"version"` + Description types.String `json:"description" toml:"description" xml:"description" yaml:"description"` + Position Position `json:"-" toml:"-" xml:"-" yaml:"-"` } // FullName returns full name of the modulecall, with version if available diff --git a/terraform/resource.go b/terraform/resource.go index 20a40b3..a6fa721 100644 --- a/terraform/resource.go +++ b/terraform/resource.go @@ -27,6 +27,7 @@ type Resource struct { ProviderSource string `json:"source" toml:"source" xml:"source" yaml:"source"` Mode string `json:"mode" toml:"mode" xml:"mode" yaml:"mode"` Version types.String `json:"version" toml:"version" xml:"version" yaml:"version"` + Description types.String `json:"description" toml:"description" xml:"description" yaml:"description"` Position Position `json:"-" toml:"-" xml:"-" yaml:"-"` }