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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ settings:
html: true
indent: 2
lockfile: true
read-comments: true
required: true
sensitive: true
type: true
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func NewCommand() *cobra.Command {
cmd.PersistentFlags().BoolVar(&config.OutputValues.Enabled, "output-values", false, "inject output values into outputs (default false)")
cmd.PersistentFlags().StringVar(&config.OutputValues.From, "output-values-from", "", "inject output values from file into outputs (default \"\")")

cmd.PersistentFlags().BoolVar(&config.Settings.ReadComments, "read-comments", true, "use comments as description when description is empty")

// formatter subcommands
cmd.AddCommand(asciidoc.NewCommand(runtime, config))
cmd.AddCommand(json.NewCommand(runtime, config))
Expand Down
1 change: 1 addition & 0 deletions docs/reference/asciidoc-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ terraform-docs asciidoc document [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--required show Required column or section (default true)
Expand Down
1 change: 1 addition & 0 deletions docs/reference/asciidoc-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ terraform-docs asciidoc table [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--required show Required column or section (default true)
Expand Down
1 change: 1 addition & 0 deletions docs/reference/asciidoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ terraform-docs asciidoc [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ terraform-docs json [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/markdown-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ terraform-docs markdown document [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--required show Required column or section (default true)
Expand Down
1 change: 1 addition & 0 deletions docs/reference/markdown-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ terraform-docs markdown table [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--required show Required column or section (default true)
Expand Down
1 change: 1 addition & 0 deletions docs/reference/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ terraform-docs markdown [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/pretty.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ terraform-docs pretty [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/terraform-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ terraform-docs [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/tfvars-hcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ terraform-docs tfvars hcl [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/tfvars-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ terraform-docs tfvars json [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/tfvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Generate terraform.tfvars of inputs.
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ terraform-docs toml [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ terraform-docs xml [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/reference/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ terraform-docs yaml [PATH] [flags]
--output-template string output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
--output-values inject output values into outputs (default false)
--output-values-from string inject output values from file into outputs (default "")
--read-comments use comments as description when description is empty (default true)
--recursive update submodules recursively (default false)
--recursive-path string submodules path to recursively update (default "modules")
--show strings show section [all, data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ settings:
html: true
indent: 2
lockfile: true
read-comments: true
required: true
sensitive: true
type: true
Expand Down
8 changes: 8 additions & 0 deletions docs/user-guide/configuration/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ settings:
html: true
indent: 2
lockfile: true
read-comments: true
required: true
sensitive: true
type: true
Expand Down Expand Up @@ -95,6 +96,13 @@ Indentation level of headings [available: 1, 2, 3, 4, 5].

Read `.terraform.lock.hcl` to extract exact version of providers.

### read-comments

> since: `v0.16.0`\
> scope: `global`

Use comments from `tf` files for "Description" column (for inputs and outputs) when description is empty

### required

> since: `v0.10.0`\
Expand Down
72 changes: 39 additions & 33 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ var flagMappings = map[string]string{
"sort-by-required": "required",
"sort-by-type": "type",

"anchor": "settings.anchor",
"color": "settings.color",
"default": "settings.default",
"description": "settings.description",
"escape": "settings.escape",
"indent": "settings.indent",
"required": "settings.required",
"sensitive": "settings.sensitive",
"type": "settings.type",
"anchor": "settings.anchor",
"color": "settings.color",
"default": "settings.default",
"description": "settings.description",
"escape": "settings.escape",
"indent": "settings.indent",
"read-comments": "settings.read-comments",
"required": "settings.required",
"sensitive": "settings.sensitive",
"type": "settings.type",
}

// Config represents all the available config options that can be accessed and passed through CLI
Expand Down Expand Up @@ -376,34 +377,36 @@ func (s *sort) validate() error {
}

type settings struct {
Anchor bool `mapstructure:"anchor"`
Color bool `mapstructure:"color"`
Default bool `mapstructure:"default"`
Description bool `mapstructure:"description"`
Escape bool `mapstructure:"escape"`
HideEmpty bool `mapstructure:"hide-empty"`
HTML bool `mapstructure:"html"`
Indent int `mapstructure:"indent"`
LockFile bool `mapstructure:"lockfile"`
Required bool `mapstructure:"required"`
Sensitive bool `mapstructure:"sensitive"`
Type bool `mapstructure:"type"`
Anchor bool `mapstructure:"anchor"`
Color bool `mapstructure:"color"`
Default bool `mapstructure:"default"`
Description bool `mapstructure:"description"`
Escape bool `mapstructure:"escape"`
HideEmpty bool `mapstructure:"hide-empty"`
HTML bool `mapstructure:"html"`
Indent int `mapstructure:"indent"`
LockFile bool `mapstructure:"lockfile"`
ReadComments bool `mapstructure:"read-comments"`
Required bool `mapstructure:"required"`
Sensitive bool `mapstructure:"sensitive"`
Type bool `mapstructure:"type"`
}

func defaultSettings() settings {
return settings{
Anchor: true,
Color: true,
Default: true,
Description: false,
Escape: true,
HideEmpty: false,
HTML: true,
Indent: 2,
LockFile: true,
Required: true,
Sensitive: true,
Type: true,
Anchor: true,
Color: true,
Default: true,
Description: false,
Escape: true,
HideEmpty: false,
HTML: true,
Indent: 2,
LockFile: true,
ReadComments: true,
Required: true,
Sensitive: true,
Type: true,
}
}

Expand Down Expand Up @@ -511,6 +514,9 @@ func (c *Config) extract() (*print.Settings, *terraform.Options) {
options.SortBy.Required = c.Sort.Enabled && c.Sort.By == sortRequired
options.SortBy.Type = c.Sort.Enabled && c.Sort.By == sortType

// read comments
options.ReadComments = c.Settings.ReadComments

// settings
settings.EscapeCharacters = c.Settings.Escape
settings.IndentLevel = c.Settings.Indent
Expand Down
8 changes: 4 additions & 4 deletions internal/terraform/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func loadModuleItems(tfmodule *tfconfig.Module, options *Options) (*Module, erro
return nil, err
}

inputs, required, optional := loadInputs(tfmodule)
inputs, required, optional := loadInputs(tfmodule, options)
modulecalls := loadModulecalls(tfmodule)
outputs, err := loadOutputs(tfmodule, options)
if err != nil {
Expand Down Expand Up @@ -265,15 +265,15 @@ func loadSection(options *Options, file string, section string) (string, error)
return strings.Join(sectionText, "\n"), nil
}

func loadInputs(tfmodule *tfconfig.Module) ([]*Input, []*Input, []*Input) {
func loadInputs(tfmodule *tfconfig.Module, options *Options) ([]*Input, []*Input, []*Input) {
var inputs = make([]*Input, 0, len(tfmodule.Variables))
var required = make([]*Input, 0, len(tfmodule.Variables))
var optional = make([]*Input, 0, len(tfmodule.Variables))

for _, input := range tfmodule.Variables {
// convert CRLF to LF early on (https://github.com/terraform-docs/terraform-docs/issues/305)
inputDescription := strings.ReplaceAll(input.Description, "\r\n", "\n")
if inputDescription == "" {
if inputDescription == "" && options.ReadComments {
inputDescription = loadComments(input.Pos.Filename, input.Pos.Line)
}

Expand Down Expand Up @@ -355,7 +355,7 @@ func loadOutputs(tfmodule *tfconfig.Module, options *Options) ([]*Output, error)
}
for _, o := range tfmodule.Outputs {
description := o.Description
if description == "" {
if description == "" && options.ReadComments {
description = loadComments(o.Pos.Filename, o.Pos.Line)
}
output := &Output{
Expand Down
49 changes: 47 additions & 2 deletions internal/terraform/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ func TestLoadInputs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
module, _ := loadModule(filepath.Join("testdata", tt.path))
inputs, requireds, optionals := loadInputs(module)
options := NewOptions()
inputs, requireds, optionals := loadInputs(module, options)

assert.Equal(tt.expected.inputs, len(inputs))
assert.Equal(tt.expected.requireds, len(requireds))
Expand Down Expand Up @@ -556,7 +557,8 @@ func TestLoadInputsLineEnding(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
module, _ := loadModule(filepath.Join("testdata", tt.path))
inputs, _, _ := loadInputs(module)
options := NewOptions()
inputs, _, _ := loadInputs(module, options)

assert.Equal(1, len(inputs))
assert.Equal(tt.expected, string(inputs[0].Description))
Expand Down Expand Up @@ -782,6 +784,49 @@ func TestLoadComments(t *testing.T) {
}
}

func TestReadComments(t *testing.T) {
tests := []struct {
name string
path string
fileName string
readComments bool
expected string
}{
{
name: "Validate description when 'ReadComments' is false",
path: "read-comments",
fileName: "variables.tf",
readComments: false,
expected: "",
},
{
name: "Validate description when 'ReadComments' is true",
path: "read-comments",
fileName: "variables.tf",
readComments: true,
expected: "B description",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
options := NewOptions()
options.ReadComments = tt.readComments
module, err := loadModule(filepath.Join("testdata", tt.path))

assert.Nil(err)

inputs, _, _ := loadInputs(module, options)
assert.Equal(1, len(inputs))
assert.Equal(tt.expected, string(inputs[0].Description))

outputs, _ := loadOutputs(module, options)
assert.Equal(1, len(outputs))
assert.Equal(tt.expected, string(outputs[0].Description))
})
}
}

func TestSortItems(t *testing.T) {
type expected struct {
inputs []string
Expand Down
2 changes: 2 additions & 0 deletions internal/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Options struct {
SortBy *SortBy
OutputValues bool
OutputValuesPath string
ReadComments bool
}

// NewOptions returns new instance of Options
Expand All @@ -49,6 +50,7 @@ func NewOptions() *Options {
SortBy: &SortBy{Name: false, Required: false, Type: false},
OutputValues: false,
OutputValuesPath: "",
ReadComments: true,
}
}

Expand Down
Loading