Skip to content

Commit d914ca7

Browse files
committed
Add a flag for showing description with hcl formatted tfvars
Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> add: cli flag of show description Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> add: test for tfvars-hcl description flag Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> docs: add tfvars-hcl flag Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> fix: inser new lines around value with description Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> fix: replace with -1 to replaceall for linter Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> fix: use simple format for linter Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com> fix: padding format as terraform-fmt-way Signed-off-by: mikiya771 <22876341+mikiya771@users.noreply.github.com>
1 parent f19fd85 commit d914ca7

File tree

8 files changed

+170
-24
lines changed

8 files changed

+170
-24
lines changed

cmd/tfvars/hcl/hcl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ func NewCommand(config *cli.Config) *cobra.Command {
2626
PreRunE: cli.PreRunEFunc(config),
2727
RunE: cli.RunEFunc(config),
2828
}
29+
cmd.PersistentFlags().BoolVar(&config.Settings.Description, "description", false, "show Descriptions on variables")
2930
return cmd
3031
}

docs/reference/tfvars-hcl.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ terraform-docs tfvars hcl [PATH] [flags]
1919
## Options
2020

2121
```console
22-
-h, --help help for hcl
22+
--description show Descriptions on variables
23+
-h, --help help for hcl
2324
```
2425

2526
## Inherited Options

internal/cli/config.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,26 +262,28 @@ func (s *sort) validate() error {
262262
}
263263

264264
type settings struct {
265-
Anchor bool `yaml:"anchor"`
266-
Color bool `yaml:"color"`
267-
Default bool `yaml:"default"`
268-
Escape bool `yaml:"escape"`
269-
Indent int `yaml:"indent"`
270-
Required bool `yaml:"required"`
271-
Sensitive bool `yaml:"sensitive"`
272-
Type bool `yaml:"type"`
265+
Anchor bool `yaml:"anchor"`
266+
Color bool `yaml:"color"`
267+
Default bool `yaml:"default"`
268+
Escape bool `yaml:"escape"`
269+
Indent int `yaml:"indent"`
270+
Required bool `yaml:"required"`
271+
Sensitive bool `yaml:"sensitive"`
272+
Type bool `yaml:"type"`
273+
Description bool `yaml:"description"`
273274
}
274275

275276
func defaultSettings() settings {
276277
return settings{
277-
Anchor: true,
278-
Color: true,
279-
Default: true,
280-
Escape: true,
281-
Indent: 2,
282-
Required: true,
283-
Sensitive: true,
284-
Type: true,
278+
Anchor: true,
279+
Color: true,
280+
Default: true,
281+
Escape: true,
282+
Indent: 2,
283+
Required: true,
284+
Sensitive: true,
285+
Type: true,
286+
Description: false,
285287
}
286288
}
287289

@@ -435,6 +437,7 @@ func (c *Config) extract() (*print.Settings, *terraform.Options) {
435437
settings.EscapeCharacters = c.Settings.Escape
436438
settings.IndentLevel = c.Settings.Indent
437439
settings.ShowAnchor = c.Settings.Anchor
440+
settings.ShowDescription = c.Settings.Description
438441
settings.ShowColor = c.Settings.Color
439442
settings.ShowDefault = c.Settings.Default
440443
settings.ShowRequired = c.Settings.Required
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{{- if .Module.Inputs -}}
22
{{- range $i, $k := .Module.Inputs -}}
3+
{{ if and $k.Description showDescription -}}
4+
{{ convertToComment $k.Description }}
5+
{{ align $k.Name $i }} = {{ value $k.GetValue }}
6+
7+
{{ else -}}
38
{{ align $k.Name $i }} = {{ value $k.GetValue }}
9+
{{ end -}}
410
{{ end -}}
5-
{{- end -}}
11+
{{- end -}}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
# A variable with underscores.
3+
input_with_underscores = ""
4+
5+
# It's list number two.
6+
list-2 = ""
7+
8+
# It's map number two.
9+
map-2 = ""
10+
11+
# It's number number two.
12+
number-2 = ""
13+
14+
# It's string number two.
15+
string-2 = ""
16+
17+
string_no_default = ""
18+
unquoted = ""
19+
20+
# It's bool number one.
21+
bool-1 = true
22+
23+
# It's bool number two.
24+
bool-2 = false
25+
26+
bool-3 = true
27+
bool_default_false = false
28+
29+
# This is a complicated one. We need a newline.
30+
# And an example in a code block
31+
# ```
32+
# default = [
33+
# "machine rack01:neptune"
34+
# ]
35+
# ```
36+
#
37+
input-with-code-block = [
38+
"name rack:location"
39+
]
40+
41+
# It includes v1 | v2 | v3
42+
input-with-pipe = "v1"
43+
44+
# It's list number one.
45+
list-1 = [
46+
"a",
47+
"b",
48+
"c"
49+
]
50+
51+
list-3 = []
52+
list_default_empty = []
53+
54+
# This description is itself markdown.
55+
#
56+
# It spans over multiple lines.
57+
#
58+
long_type = {
59+
"bar": {
60+
"bar": "bar",
61+
"foo": "bar"
62+
},
63+
"buzz": [
64+
"fizz",
65+
"buzz"
66+
],
67+
"fizz": [],
68+
"foo": {
69+
"bar": "foo",
70+
"foo": "foo"
71+
},
72+
"name": "hello"
73+
}
74+
75+
# It's map number one.
76+
map-1 = {
77+
"a": 1,
78+
"b": 2,
79+
"c": 3
80+
}
81+
82+
map-3 = {}
83+
84+
# The description contains `something_with_underscore`. Defaults to 'VALUE_WITH_UNDERSCORE'.
85+
no-escape-default-value = "VALUE_WITH_UNDERSCORE"
86+
87+
# It's number number one.
88+
number-1 = 42
89+
90+
number-3 = "19"
91+
number-4 = 15.75
92+
number_default_zero = 0
93+
object_default_empty = {}
94+
95+
# It's string number one.
96+
string-1 = "bar"
97+
98+
string-3 = ""
99+
string-special-chars = "\\.<>[]{}_-"
100+
string_default_empty = ""
101+
string_default_null = ""
102+
103+
# The description contains url. https://www.domain.com/foo/bar_baz.html
104+
with-url = ""

internal/format/tfvars_hcl.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright 2021 The terraform-docs Authors.
3-
43
Licensed under the MIT license (the "License"); you may not
54
use this file except in compliance with the License.
65
@@ -20,6 +19,7 @@ import (
2019
"github.com/terraform-docs/terraform-docs/internal/print"
2120
"github.com/terraform-docs/terraform-docs/internal/template"
2221
"github.com/terraform-docs/terraform-docs/internal/terraform"
22+
"github.com/terraform-docs/terraform-docs/internal/types"
2323
)
2424

2525
//go:embed templates/tfvars_hcl.tmpl
@@ -48,6 +48,12 @@ func NewTfvarsHCL(settings *print.Settings) print.Engine {
4848
}
4949
return s
5050
},
51+
"convertToComment": func(s types.String) string {
52+
return "\n# " + strings.ReplaceAll(string(s), "\n", "\n# ")
53+
},
54+
"showDescription": func() bool {
55+
return settings.ShowDescription
56+
},
5157
})
5258
return &TfvarsHCL{
5359
template: tt,
@@ -56,23 +62,28 @@ func NewTfvarsHCL(settings *print.Settings) print.Engine {
5662

5763
// Print a Terraform module as Terraform tfvars HCL.
5864
func (h *TfvarsHCL) Print(module *terraform.Module, settings *print.Settings) (string, error) {
59-
alignments(module.Inputs)
65+
alignments(module.Inputs, settings)
6066
rendered, err := h.template.Render(module)
6167
if err != nil {
6268
return "", err
6369
}
6470
return strings.TrimSuffix(sanitize(rendered), "\n"), nil
6571
}
6672

67-
func alignments(inputs []*terraform.Input) {
73+
func isMultilineFormat(input *terraform.Input) bool {
74+
isList := input.Type == "list" || reflect.TypeOf(input.Default).Name() == "List"
75+
isMap := input.Type == "map" || reflect.TypeOf(input.Default).Name() == "Map"
76+
return (isList || isMap) && input.Default.Length() > 0
77+
}
78+
79+
func alignments(inputs []*terraform.Input, settings *print.Settings) {
6880
padding = make([]int, len(inputs))
6981
maxlen := 0
7082
index := 0
7183
for i, input := range inputs {
72-
isList := input.Type == "list" || reflect.TypeOf(input.Default).Name() == "List"
73-
isMap := input.Type == "map" || reflect.TypeOf(input.Default).Name() == "Map"
84+
isDescribed := settings.ShowDescription && input.Description.Length() > 0
7485
l := len(input.Name)
75-
if (isList || isMap) && input.Default.Length() > 0 {
86+
if isMultilineFormat(input) || isDescribed {
7687
for j := index; j < i; j++ {
7788
padding[j] = maxlen
7889
}

internal/format/tfvars_hcl_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ func TestTfvarsHcl(t *testing.T) {
4242
settings: print.Settings{EscapeCharacters: true},
4343
options: terraform.Options{},
4444
},
45+
"PrintDescription": {
46+
settings: testutil.WithSections(
47+
print.Settings{
48+
ShowDescription: true,
49+
},
50+
),
51+
options: terraform.Options{
52+
SortBy: &terraform.SortBy{
53+
Name: true,
54+
Required: true,
55+
},
56+
},
57+
},
4558
"SortByName": {
4659
settings: testutil.WithSections(),
4760
options: terraform.Options{

internal/print/settings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ type Settings struct {
4646
// scope: Pretty
4747
ShowColor bool
4848

49+
// ShowDescription show "Descriptions on variables" column
50+
//
51+
// default: false
52+
// scope: tfvars hcl
53+
ShowDescription bool
54+
4955
// ShowDefault show "Default" column
5056
//
5157
// default: true
@@ -128,6 +134,7 @@ func DefaultSettings() *Settings {
128134
ShowAnchor: true,
129135
ShowColor: true,
130136
ShowDefault: true,
137+
ShowDescription: false,
131138
ShowFooter: false,
132139
ShowHeader: true,
133140
ShowInputs: true,

0 commit comments

Comments
 (0)