mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
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>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -262,26 +262,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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,6 +437,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
|
||||
|
||||
@@ -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 -}}
|
||||
{{- end -}}
|
||||
|
||||
104
internal/format/testdata/tfvars/hcl-PrintDescription.golden
vendored
Normal file
104
internal/format/testdata/tfvars/hcl-PrintDescription.golden
vendored
Normal file
@@ -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 = ""
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user