diff --git a/cmd/tfvars/hcl/hcl.go b/cmd/tfvars/hcl/hcl.go index 926f5d8..acf2c6b 100644 --- a/cmd/tfvars/hcl/hcl.go +++ b/cmd/tfvars/hcl/hcl.go @@ -26,5 +26,6 @@ func NewCommand(config *cli.Config) *cobra.Command { PreRunE: cli.PreRunEFunc(config), RunE: cli.RunEFunc(config), } + cmd.PersistentFlags().BoolVar(&config.Settings.Description, "description", false, "show Descriptions on variables") return cmd } diff --git a/docs/reference/tfvars-hcl.md b/docs/reference/tfvars-hcl.md index aee4ebc..5b28c4d 100644 --- a/docs/reference/tfvars-hcl.md +++ b/docs/reference/tfvars-hcl.md @@ -19,7 +19,8 @@ terraform-docs tfvars hcl [PATH] [flags] ## Options ```console - -h, --help help for hcl + --description show Descriptions on variables + -h, --help help for hcl ``` ## Inherited Options diff --git a/internal/cli/config.go b/internal/cli/config.go index e9e0699..f0a07d2 100644 --- a/internal/cli/config.go +++ b/internal/cli/config.go @@ -290,26 +290,28 @@ func (s *sort) validate() error { } type settings struct { - Anchor bool `yaml:"anchor"` - Color bool `yaml:"color"` - Default bool `yaml:"default"` - Escape bool `yaml:"escape"` - Indent int `yaml:"indent"` - Required bool `yaml:"required"` - Sensitive bool `yaml:"sensitive"` - Type bool `yaml:"type"` + Anchor bool `yaml:"anchor"` + Color bool `yaml:"color"` + Default bool `yaml:"default"` + Escape bool `yaml:"escape"` + Indent int `yaml:"indent"` + Required bool `yaml:"required"` + Sensitive bool `yaml:"sensitive"` + Type bool `yaml:"type"` + Description bool `yaml:"description"` } func defaultSettings() settings { return settings{ - Anchor: true, - Color: true, - Default: true, - Escape: true, - Indent: 2, - Required: true, - Sensitive: true, - Type: true, + Anchor: true, + Color: true, + Default: true, + Escape: true, + Indent: 2, + Required: true, + Sensitive: true, + Type: true, + Description: false, } } @@ -468,6 +470,7 @@ func (c *Config) extract() (*print.Settings, *terraform.Options) { settings.EscapeCharacters = c.Settings.Escape settings.IndentLevel = c.Settings.Indent settings.ShowAnchor = c.Settings.Anchor + settings.ShowDescription = c.Settings.Description settings.ShowColor = c.Settings.Color settings.ShowDefault = c.Settings.Default settings.ShowRequired = c.Settings.Required diff --git a/internal/format/templates/tfvars_hcl.tmpl b/internal/format/templates/tfvars_hcl.tmpl index 7c35573..35ab310 100644 --- a/internal/format/templates/tfvars_hcl.tmpl +++ b/internal/format/templates/tfvars_hcl.tmpl @@ -1,5 +1,11 @@ {{- if .Module.Inputs -}} {{- range $i, $k := .Module.Inputs -}} + {{ if and $k.Description showDescription -}} + {{ convertToComment $k.Description }} + {{ align $k.Name $i }} = {{ value $k.GetValue }} + + {{ else -}} {{ align $k.Name $i }} = {{ value $k.GetValue }} + {{ end -}} {{ end -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/internal/format/testdata/tfvars/hcl-PrintDescription.golden b/internal/format/testdata/tfvars/hcl-PrintDescription.golden new file mode 100644 index 0000000..941003c --- /dev/null +++ b/internal/format/testdata/tfvars/hcl-PrintDescription.golden @@ -0,0 +1,104 @@ + +# A variable with underscores. +input_with_underscores = "" + +# It's list number two. +list-2 = "" + +# It's map number two. +map-2 = "" + +# It's number number two. +number-2 = "" + +# It's string number two. +string-2 = "" + +string_no_default = "" +unquoted = "" + +# It's bool number one. +bool-1 = true + +# It's bool number two. +bool-2 = false + +bool-3 = true +bool_default_false = false + +# This is a complicated one. We need a newline. +# And an example in a code block +# ``` +# default = [ +# "machine rack01:neptune" +# ] +# ``` +# +input-with-code-block = [ + "name rack:location" +] + +# It includes v1 | v2 | v3 +input-with-pipe = "v1" + +# It's list number one. +list-1 = [ + "a", + "b", + "c" +] + +list-3 = [] +list_default_empty = [] + +# This description is itself markdown. +# +# It spans over multiple lines. +# +long_type = { + "bar": { + "bar": "bar", + "foo": "bar" + }, + "buzz": [ + "fizz", + "buzz" + ], + "fizz": [], + "foo": { + "bar": "foo", + "foo": "foo" + }, + "name": "hello" +} + +# It's map number one. +map-1 = { + "a": 1, + "b": 2, + "c": 3 +} + +map-3 = {} + +# The description contains `something_with_underscore`. Defaults to 'VALUE_WITH_UNDERSCORE'. +no-escape-default-value = "VALUE_WITH_UNDERSCORE" + +# It's number number one. +number-1 = 42 + +number-3 = "19" +number-4 = 15.75 +number_default_zero = 0 +object_default_empty = {} + +# It's string number one. +string-1 = "bar" + +string-3 = "" +string-special-chars = "\\.<>[]{}_-" +string_default_empty = "" +string_default_null = "" + +# The description contains url. https://www.domain.com/foo/bar_baz.html +with-url = "" \ No newline at end of file diff --git a/internal/format/tfvars_hcl.go b/internal/format/tfvars_hcl.go index f2eb10f..2d044d6 100644 --- a/internal/format/tfvars_hcl.go +++ b/internal/format/tfvars_hcl.go @@ -1,6 +1,5 @@ /* Copyright 2021 The terraform-docs Authors. - Licensed under the MIT license (the "License"); you may not use this file except in compliance with the License. @@ -20,6 +19,7 @@ import ( "github.com/terraform-docs/terraform-docs/internal/print" "github.com/terraform-docs/terraform-docs/internal/template" "github.com/terraform-docs/terraform-docs/internal/terraform" + "github.com/terraform-docs/terraform-docs/internal/types" ) //go:embed templates/tfvars_hcl.tmpl @@ -48,6 +48,12 @@ func NewTfvarsHCL(settings *print.Settings) print.Engine { } return s }, + "convertToComment": func(s types.String) string { + return "\n# " + strings.ReplaceAll(string(s), "\n", "\n# ") + }, + "showDescription": func() bool { + return settings.ShowDescription + }, }) return &TfvarsHCL{ template: tt, @@ -56,7 +62,7 @@ func NewTfvarsHCL(settings *print.Settings) print.Engine { // Print a Terraform module as Terraform tfvars HCL. func (h *TfvarsHCL) Print(module *terraform.Module, settings *print.Settings) (string, error) { - alignments(module.Inputs) + alignments(module.Inputs, settings) rendered, err := h.template.Render(module) if err != nil { return "", err @@ -64,15 +70,20 @@ func (h *TfvarsHCL) Print(module *terraform.Module, settings *print.Settings) (s return strings.TrimSuffix(sanitize(rendered), "\n"), nil } -func alignments(inputs []*terraform.Input) { +func isMultilineFormat(input *terraform.Input) bool { + isList := input.Type == "list" || reflect.TypeOf(input.Default).Name() == "List" + isMap := input.Type == "map" || reflect.TypeOf(input.Default).Name() == "Map" + return (isList || isMap) && input.Default.Length() > 0 +} + +func alignments(inputs []*terraform.Input, settings *print.Settings) { padding = make([]int, len(inputs)) maxlen := 0 index := 0 for i, input := range inputs { - isList := input.Type == "list" || reflect.TypeOf(input.Default).Name() == "List" - isMap := input.Type == "map" || reflect.TypeOf(input.Default).Name() == "Map" + isDescribed := settings.ShowDescription && input.Description.Length() > 0 l := len(input.Name) - if (isList || isMap) && input.Default.Length() > 0 { + if isMultilineFormat(input) || isDescribed { for j := index; j < i; j++ { padding[j] = maxlen } diff --git a/internal/format/tfvars_hcl_test.go b/internal/format/tfvars_hcl_test.go index 975951f..156d3f1 100644 --- a/internal/format/tfvars_hcl_test.go +++ b/internal/format/tfvars_hcl_test.go @@ -42,6 +42,19 @@ func TestTfvarsHcl(t *testing.T) { settings: print.Settings{EscapeCharacters: true}, options: terraform.Options{}, }, + "PrintDescription": { + settings: testutil.WithSections( + print.Settings{ + ShowDescription: true, + }, + ), + options: terraform.Options{ + SortBy: &terraform.SortBy{ + Name: true, + Required: true, + }, + }, + }, "SortByName": { settings: testutil.WithSections(), options: terraform.Options{ diff --git a/internal/print/settings.go b/internal/print/settings.go index b6fd9b3..8f533aa 100644 --- a/internal/print/settings.go +++ b/internal/print/settings.go @@ -46,6 +46,12 @@ type Settings struct { // scope: Pretty ShowColor bool + // ShowDescription show "Descriptions on variables" column + // + // default: false + // scope: tfvars hcl + ShowDescription bool + // ShowDefault show "Default" column // // default: true @@ -128,6 +134,7 @@ func DefaultSettings() *Settings { ShowAnchor: true, ShowColor: true, ShowDefault: true, + ShowDescription: false, ShowFooter: false, ShowHeader: true, ShowInputs: true,