Skip to content

optionally process comments from tf files as description #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2021

Conversation

pbikki
Copy link
Contributor

@pbikki pbikki commented Sep 19, 2021

Description of your changes

Fixes #552

This PR includes changes to optionally process comments from .tf files as description (when description is not provided in terraform inputs, outputs definition)

  • Adds flag read-comments

I have:

How has this code been tested

  • Added test TestReadComments()
  • Tested with make test
  • Tested manually

Manual Test

  • Create sample tf folder and file
mkdir /tmp/test-tf
cat <<EOF >> /tmp/test-tf/sample.tf
# some comment
variable "var1" {
}

# some comment
output "output-var1" {
  value = null
}
EOF
  • Tested with read-comments as false. Expected - description should be n/a (should not use comment (#some comment) as description)
go run main.go --read-comments=false --show=inputs,outputs markdown /tmp/test-tf

Result:

Inputs

Name Description Type Default Required
var1 n/a any n/a yes

Outputs

Name Description
output-var1 n/a
  • Tested with read-comments as true. Expected - 'some comment' is used as description
▶ go run main.go --read-comments=true --show=inputs,outputs markdown /tmp/test-tf  

Result:

Inputs

Name Description Type Default Required
var1 some comment any n/a yes

Outputs

Name Description
output-var1 some comment
  • Cleanup
$ rm -r /tmp/test-tf

TBD

  • Config setting that maps to the new option added Added new config setting read-comments
  • Tests when option is set to false. Currently few tests fail when option is set to false set default to true (for backwards compatibility). Added new test cases to test when set to false and true

@pbikki pbikki marked this pull request as draft September 20, 2021 17:41
@pbikki pbikki marked this pull request as ready for review September 20, 2021 17:41
@pbikki pbikki marked this pull request as draft September 20, 2021 17:41
@pbikki
Copy link
Contributor Author

pbikki commented Sep 20, 2021

@khos2ow based on our discussion, can you please review.

@khos2ow khos2ow changed the title [Draft] Issue 552 - optionally process comments from tf files for var description optionally process comments from tf files as description Sep 20, 2021
@pull-request-size pull-request-size bot added size/L and removed size/M labels Sep 20, 2021
@pbikki
Copy link
Contributor Author

pbikki commented Sep 20, 2021

I run make docs and it resulted in these changes in commit 26b860b3

When default is option is set to true here technically there shouldn't be any changes to the docs right ?

@khos2ow
Copy link
Member

khos2ow commented Sep 22, 2021

When default is option is set to true here technically there shouldn't be any changes to the docs right ?

You are absolutely correct on this, there shouldn't be any changes in docs/reference/ folder. This clearly indicates there's some flaw in the changes in this PR.

@khos2ow
Copy link
Member

khos2ow commented Sep 22, 2021

OK, found the issue. It actually doesn't have anything to do with your implementation, the docs generator is faulty. Can you please add the same property (i.e. VarDescriptionFromComments or whatever you renamed it to) to https://github.com/terraform-docs/terraform-docs/blob/master/scripts/docs/generate.go#L177 ?

@pbikki
Copy link
Contributor Author

pbikki commented Sep 22, 2021

https://github.com/terraform-docs/terraform-docs/blob/master/scripts/docs/generate.go#L177

That's exactly where I was looking. Assumed that for options that are not explicitly set, it'll use default. Got it now. Will update. Thanks!

@pull-request-size pull-request-size bot added size/S and removed size/L labels Sep 22, 2021
@khos2ow
Copy link
Member

khos2ow commented Sep 22, 2021

Assumed that for options that are not explicitly set, it'll use default.

I'm actually in the process of refactoring Options which will solve this. It will be as you mentioned, anything now explicitly set in generate.go will take the default value.

@pull-request-size pull-request-size bot added size/L and removed size/S labels Sep 23, 2021
@pbikki
Copy link
Contributor Author

pbikki commented Sep 23, 2021

@khos2ow, added config ReadComments (cli flag read-comments) for now. Please review. Let me know if the added config setting should be propagated into printSettings and printSDKsettings

We discussed there might be some overlap with the description setting for hcl. But appears description solves a different purpose, so my guess is new flag added shouldn't be an issue. Below is an example when both flags are used.

▶ go run main.go tfvars hcl --description --read-comments=true <sample-tf-module>

# some comment
var1 = ""
▶ go run main.go tfvars hcl --read-comments=true <sample-tf-module>
var1 = ""

@pbikki pbikki marked this pull request as ready for review September 23, 2021 00:36
Copy link
Member

@khos2ow khos2ow left a comment

Choose a reason for hiding this comment

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

Please look at my comments below. Also please update the new config in:

  • README.md
  • docs/user-guide/configuration.md
  • docs/user-guide/configuration/settings.md

@khos2ow
Copy link
Member

khos2ow commented Sep 23, 2021

Let me know if the added config setting should be propagated into printSettings and printSDKsettings

This should be good as is and it's not necessary as this is an entirely internal config to load the module.

@khos2ow
Copy link
Member

khos2ow commented Sep 23, 2021

I realized in no point there's a test with ReadComments: false. It might be a good idea to create a test case, TestReadComments() in module_test.go, and for simplicity you can create internal/terraform/testdata/read-comments/ folder with one variable and one output with comments and test-cases of ReadComments both true and false.

@pbikki
Copy link
Contributor Author

pbikki commented Sep 24, 2021

  • Updated docs (added new config setting)
  • Added TestReadComments() with testcases and testdata to test new config read-comments

@pbikki pbikki force-pushed the issue-552-dev branch 2 times, most recently from 28d28e4 to 51f63d1 Compare September 24, 2021 18:40
Copy link
Member

@khos2ow khos2ow left a comment

Choose a reason for hiding this comment

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

Thank you @poojitha-bikki for all the efforts. lgtm!
would you please now squash your commits into one?

…description

fixes issue terraform-docs#552

- process description from comments

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- fix module tests

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- optionally read comments for output vars description

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- set default to true

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- run make docs

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- change option name

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- add option in doc generator; make docs

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- add config 'ReadComments'

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- Fix alphabetic order for 'ReadComments' setting

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- add read-comments in docs

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- add test for readcomments option

Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>

- update 'read-comments' flag description

Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>
Signed-off-by: Poojitha Bikki <50849668+poojitha-bikki@users.noreply.github.com>
@khos2ow khos2ow merged commit 4f698fb into terraform-docs:master Sep 27, 2021
@pbikki
Copy link
Contributor Author

pbikki commented Sep 27, 2021

Thank you @khos2ow for your guidance.

@khos2ow
Copy link
Member

khos2ow commented Sep 27, 2021

Thank you @poojitha-bikki for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not use comments as variable description
2 participants