Use embedded template files

Embedding files was added in go1.16, so instead of defining required
templates as constant in .go files, they are moved to dedicated .tmpl
files.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2021-03-01 13:58:26 -05:00
parent ded9b9b080
commit 92d7a60d5e
13 changed files with 611 additions and 806 deletions

View File

@@ -16,7 +16,7 @@ insert_final_newline = true
indent_style = tab
indent_size = 4
[*.golden]
[*.{golden,tmpl}]
trim_trailing_whitespace = false
insert_final_newline = false

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
gotemplate "text/template"
"github.com/terraform-docs/terraform-docs/internal/print"
@@ -18,166 +19,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
asciidocDocumentHeaderTpl = `
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
`
asciidocDocumentResourcesTpl = `
{{- if .Settings.ShowResources -}}
{{ indent 0 "=" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
The following resources are used by this module:
{{ range .Module.Resources }}
{{ if eq (len .URL) 0 }}
- {{ .FullType }}
{{- else -}}
- {{ .URL }}[{{ .FullType }}]
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
`
asciidocDocumentRequirementsTpl = `
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "=" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
The following requirements are needed by this module:
{{- range .Module.Requirements }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .Name }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
`
asciidocDocumentProvidersTpl = `
{{- if .Settings.ShowProviders -}}
{{ indent 0 "=" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
The following providers are used by this module:
{{- range .Module.Providers }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .FullName }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
`
asciidocDocumentInputsTpl = `
{{- if .Settings.ShowInputs -}}
{{- if .Settings.ShowRequired -}}
{{ indent 0 "=" }} Required Inputs
{{ if not .Module.RequiredInputs }}
No required inputs.
{{ else }}
The following input variables are required:
{{- range .Module.RequiredInputs }}
{{ template "input" . }}
{{- end }}
{{- end }}
{{ indent 0 "=" }} Optional Inputs
{{ if not .Module.OptionalInputs }}
No optional inputs.
{{ else }}
The following input variables are optional (have default values):
{{- range .Module.OptionalInputs }}
{{ template "input" . }}
{{- end }}
{{ end }}
{{ else -}}
{{ indent 0 "=" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
The following input variables are supported:
{{- range .Module.Inputs }}
{{ template "input" . }}
{{- end }}
{{ end }}
{{- end }}
{{ end -}}
`
asciidocDocumentInputTpl = `
{{ printf "\n" }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
`
asciidocDocumentOutputsTpl = `
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "=" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
The following outputs are exported:
{{- range .Module.Outputs }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
{{ if $.Settings.OutputValues }}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
Value: {{ value $sensitive | sanitizeDoc }}
{{ if $.Settings.ShowSensitivity -}}
Sensitive: {{ ternary (.Sensitive) "yes" "no" }}
{{- end }}
{{ end }}
{{ end }}
{{ end }}
{{ end -}}
`
asciidocDocumentModulecallsTpl = `
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "=" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
The following Modules are called:
{{- range .Module.ModuleCalls }}
{{ indent 1 "=" }} {{ name .Name }}
Source: {{ .Source }}
Version: {{ .Version }}
{{- end }}
{{ end }}
{{ end -}}
`
asciidocDocumentTpl = `
{{- template "header" . -}}
{{- template "requirements" . -}}
{{- template "providers" . -}}
{{- template "modulecalls" . -}}
{{- template "resources" . -}}
{{- template "inputs" . -}}
{{- template "outputs" . -}}
`
)
//go:embed templates/asciidoc_document.tmpl
var asciidocDocumentTpl []byte
// AsciidocDocument represents AsciiDoc Document format.
type AsciidocDocument struct {
@@ -189,31 +32,7 @@ func NewAsciidocDocument(settings *print.Settings) print.Engine {
settings.EscapeCharacters = false
tt := template.New(settings, &template.Item{
Name: "document",
Text: asciidocDocumentTpl,
}, &template.Item{
Name: "header",
Text: asciidocDocumentHeaderTpl,
}, &template.Item{
Name: "requirements",
Text: asciidocDocumentRequirementsTpl,
}, &template.Item{
Name: "providers",
Text: asciidocDocumentProvidersTpl,
}, &template.Item{
Name: "resources",
Text: asciidocDocumentResourcesTpl,
}, &template.Item{
Name: "inputs",
Text: asciidocDocumentInputsTpl,
}, &template.Item{
Name: "input",
Text: asciidocDocumentInputTpl,
}, &template.Item{
Name: "outputs",
Text: asciidocDocumentOutputsTpl,
}, &template.Item{
Name: "modulecalls",
Text: asciidocDocumentModulecallsTpl,
Text: string(asciidocDocumentTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
gotemplate "text/template"
"github.com/terraform-docs/terraform-docs/internal/print"
@@ -18,142 +19,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
asciidocTableHeaderTpl = `
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
`
asciidocTableResourcesTpl = `
{{- if .Settings.ShowResources -}}
{{ indent 0 "=" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
[cols="a",options="header,autowidth"]
|===
|Name
{{- range .Module.Resources }}
{{ if eq (len .URL) 0 }}
|{{ .FullType }}
{{- else -}}
|{{ .URL }}[{{ .FullType }}]
{{- end }}
{{- end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableRequirementsTpl = `
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "=" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
[cols="a,a",options="header,autowidth"]
|===
|Name |Version
{{- range .Module.Requirements }}
|{{ .Name }} |{{ tostring .Version | default "n/a" }}
{{- end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableProvidersTpl = `
{{- if .Settings.ShowProviders -}}
{{ indent 0 "=" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
[cols="a,a",options="header,autowidth"]
|===
|Name |Version
{{- range .Module.Providers }}
|{{ .FullName }} |{{ tostring .Version | default "n/a" }}
{{- end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableInputsTpl = `
{{- if .Settings.ShowInputs -}}
{{ indent 0 "=" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
[cols="a,a,a,a{{ if .Settings.ShowRequired }},a{{ end }}",options="header,autowidth"]
|===
|Name |Description |Type |Default{{ if .Settings.ShowRequired }} |Required{{ end }}
{{- range .Module.Inputs }}
|{{ .Name }}
|{{ tostring .Description | sanitizeAsciidocTbl }}
|{{ tostring .Type | type | sanitizeAsciidocTbl }}
|{{ value .GetValue | sanitizeAsciidocTbl }}
{{ if $.Settings.ShowRequired }}|{{ ternary .Required "yes" "no" }}{{ end }}
{{ end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableOutputsTpl = `
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "=" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
[cols="a,a{{ if .Settings.OutputValues }},a{{ if $.Settings.ShowSensitivity }},a{{ end }}{{ end }}",options="header,autowidth"]
|===
|Name |Description{{ if .Settings.OutputValues }} |Value{{ if $.Settings.ShowSensitivity }} |Sensitive{{ end }}{{ end }}
{{- range .Module.Outputs }}
|{{ .Name }} |{{ tostring .Description | sanitizeAsciidocTbl }}
{{- if $.Settings.OutputValues -}}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
{{ printf " " }}|{{ value $sensitive }}
{{- if $.Settings.ShowSensitivity -}}
{{ printf " " }}|{{ ternary .Sensitive "yes" "no" }}
{{- end -}}
{{- end -}}
{{- end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableModulecallsTpl = `
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "=" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
{{- range .Module.ModuleCalls }}
|{{ .Name }}|{{ .Source }}|{{ .Version }}
{{- end }}
|===
{{ end }}
{{ end -}}
`
asciidocTableTpl = `
{{- template "header" . -}}
{{- template "requirements" . -}}
{{- template "providers" . -}}
{{- template "modulecalls" . -}}
{{- template "resources" . -}}
{{- template "inputs" . -}}
{{- template "outputs" . -}}
`
)
//go:embed templates/asciidoc_table.tmpl
var asciidocTableTpl []byte
// AsciidocTable represents AsciiDoc Table format.
type AsciidocTable struct {
@@ -165,28 +32,7 @@ func NewAsciidocTable(settings *print.Settings) print.Engine {
settings.EscapeCharacters = false
tt := template.New(settings, &template.Item{
Name: "table",
Text: asciidocTableTpl,
}, &template.Item{
Name: "header",
Text: asciidocTableHeaderTpl,
}, &template.Item{
Name: "resources",
Text: asciidocTableResourcesTpl,
}, &template.Item{
Name: "requirements",
Text: asciidocTableRequirementsTpl,
}, &template.Item{
Name: "providers",
Text: asciidocTableProvidersTpl,
}, &template.Item{
Name: "inputs",
Text: asciidocTableInputsTpl,
}, &template.Item{
Name: "outputs",
Text: asciidocTableOutputsTpl,
}, &template.Item{
Name: "modulecalls",
Text: asciidocTableModulecallsTpl,
Text: string(asciidocTableTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
gotemplate "text/template"
"github.com/terraform-docs/terraform-docs/internal/print"
@@ -18,167 +19,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
documentHeaderTpl = `
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
`
documentResourcesTpl = `
{{- if .Settings.ShowResources -}}
{{ indent 0 "#" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
The following resources are used by this module:
{{ range .Module.Resources }}
{{ if eq (len .URL) 0 }}
- {{ .FullType }}
{{- else -}}
- [{{ .FullType }}]({{ .URL }})
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
`
documentRequirementsTpl = `
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "#" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
The following requirements are needed by this module:
{{- range .Module.Requirements }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .Name }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
`
documentProvidersTpl = `
{{- if .Settings.ShowProviders -}}
{{ indent 0 "#" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
The following providers are used by this module:
{{- range .Module.Providers }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .FullName }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
`
documentInputsTpl = `
{{- if .Settings.ShowInputs -}}
{{- if .Settings.ShowRequired -}}
{{ indent 0 "#" }} Required Inputs
{{ if not .Module.RequiredInputs }}
No required inputs.
{{ else }}
The following input variables are required:
{{- range .Module.RequiredInputs }}
{{ template "input" . }}
{{- end }}
{{- end }}
{{ indent 0 "#" }} Optional Inputs
{{ if not .Module.OptionalInputs }}
No optional inputs.
{{ else }}
The following input variables are optional (have default values):
{{- range .Module.OptionalInputs }}
{{ template "input" . }}
{{- end }}
{{ end }}
{{ else -}}
{{ indent 0 "#" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
The following input variables are supported:
{{- range .Module.Inputs }}
{{ template "input" . }}
{{- end }}
{{ end }}
{{- end }}
{{ end -}}
`
documentInputTpl = `
{{ printf "\n" }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
`
documentOutputsTpl = `
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "#" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
The following outputs are exported:
{{- range .Module.Outputs }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
{{ if $.Settings.OutputValues }}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
Value: {{ value $sensitive | sanitizeDoc }}
{{ if $.Settings.ShowSensitivity -}}
Sensitive: {{ ternary (.Sensitive) "yes" "no" }}
{{- end }}
{{ end }}
{{ end }}
{{ end }}
{{ end -}}
`
documentModulecallsTpl = `
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "#" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
The following Modules are called:
{{- range .Module.ModuleCalls }}
{{ indent 1 "#" }} {{ name .Name }}
Source: {{ .Source }}
Version: {{ .Version }}
{{ end }}
{{ end }}
{{ end -}}
`
documentTpl = `
{{- template "header" . -}}
{{- template "requirements" . -}}
{{- template "providers" . -}}
{{- template "modulecalls" . -}}
{{- template "resources" . -}}
{{- template "inputs" . -}}
{{- template "outputs" . -}}
`
)
//go:embed templates/markdown_document.tmpl
var markdownDocumentTpl []byte
// MarkdownDocument represents Markdown Document format.
type MarkdownDocument struct {
@@ -189,31 +31,7 @@ type MarkdownDocument struct {
func NewMarkdownDocument(settings *print.Settings) print.Engine {
tt := template.New(settings, &template.Item{
Name: "document",
Text: documentTpl,
}, &template.Item{
Name: "header",
Text: documentHeaderTpl,
}, &template.Item{
Name: "requirements",
Text: documentRequirementsTpl,
}, &template.Item{
Name: "providers",
Text: documentProvidersTpl,
}, &template.Item{
Name: "resources",
Text: documentResourcesTpl,
}, &template.Item{
Name: "inputs",
Text: documentInputsTpl,
}, &template.Item{
Name: "input",
Text: documentInputTpl,
}, &template.Item{
Name: "outputs",
Text: documentOutputsTpl,
}, &template.Item{
Name: "modulecalls",
Text: documentModulecallsTpl,
Text: string(markdownDocumentTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
gotemplate "text/template"
"github.com/terraform-docs/terraform-docs/internal/print"
@@ -18,129 +19,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
tableHeaderTpl = `
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
`
tableResourcesTpl = `
{{- if .Settings.ShowResources -}}
{{ indent 0 "#" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
| Name |
|------|
{{- range .Module.Resources }}
{{ if eq (len .URL) 0 }}
| {{ .FullType }}
{{- else -}}
| [{{ .FullType }}]({{ .URL }}) |
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
`
tableRequirementsTpl = `
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "#" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
| Name | Version |
|------|---------|
{{- range .Module.Requirements }}
| {{ name .Name }} | {{ tostring .Version | default "n/a" }} |
{{- end }}
{{ end }}
{{ end -}}
`
tableProvidersTpl = `
{{- if .Settings.ShowProviders -}}
{{ indent 0 "#" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
| Name | Version |
|------|---------|
{{- range .Module.Providers }}
| {{ name .FullName }} | {{ tostring .Version | default "n/a" }} |
{{- end }}
{{ end }}
{{ end -}}
`
tableInputsTpl = `
{{- if .Settings.ShowInputs -}}
{{ indent 0 "#" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
| Name | Description | Type | Default |{{ if .Settings.ShowRequired }} Required |{{ end }}
|------|-------------|------|---------|{{ if .Settings.ShowRequired }}:--------:|{{ end }}
{{- range .Module.Inputs }}
| {{ name .Name }} | {{ tostring .Description | sanitizeTbl }} | {{ tostring .Type | type | sanitizeTbl }} | {{ value .GetValue | sanitizeTbl }} |
{{- if $.Settings.ShowRequired -}}
{{ printf " " }}{{ ternary .Required "yes" "no" }} |
{{- end -}}
{{- end }}
{{ end }}
{{ end -}}
`
tableOutputsTpl = `
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "#" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
| Name | Description |{{ if .Settings.OutputValues }} Value |{{ if $.Settings.ShowSensitivity }} Sensitive |{{ end }}{{ end }}
|------|-------------|{{ if .Settings.OutputValues }}-------|{{ if $.Settings.ShowSensitivity }}:---------:|{{ end }}{{ end }}
{{- range .Module.Outputs }}
| {{ name .Name }} | {{ tostring .Description | sanitizeTbl }} |
{{- if $.Settings.OutputValues -}}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
{{ printf " " }}{{ value $sensitive | sanitizeTbl }} |
{{- if $.Settings.ShowSensitivity -}}
{{ printf " " }}{{ ternary .Sensitive "yes" "no" }} |
{{- end -}}
{{- end -}}
{{- end }}
{{ end }}
{{ end -}}
`
tableModulecallsTpl = `
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "#" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
| Name | Source | Version |
|------|--------|---------|
{{- range .Module.ModuleCalls }}
| {{ .Name }} | {{ .Source }} | {{ .Version }} |
{{- end }}
{{ end }}
{{ end -}}
`
tableTpl = `
{{- template "header" . -}}
{{- template "requirements" . -}}
{{- template "providers" . -}}
{{- template "modulecalls" . -}}
{{- template "resources" . -}}
{{- template "inputs" . -}}
{{- template "outputs" . -}}
`
)
//go:embed templates/markdown_table.tmpl
var markdownTableTpl []byte
// MarkdownTable represents Markdown Table format.
type MarkdownTable struct {
@@ -151,28 +31,7 @@ type MarkdownTable struct {
func NewMarkdownTable(settings *print.Settings) print.Engine {
tt := template.New(settings, &template.Item{
Name: "table",
Text: tableTpl,
}, &template.Item{
Name: "header",
Text: tableHeaderTpl,
}, &template.Item{
Name: "requirements",
Text: tableRequirementsTpl,
}, &template.Item{
Name: "providers",
Text: tableProvidersTpl,
}, &template.Item{
Name: "resources",
Text: tableResourcesTpl,
}, &template.Item{
Name: "inputs",
Text: tableInputsTpl,
}, &template.Item{
Name: "outputs",
Text: tableOutputsTpl,
}, &template.Item{
Name: "modulecalls",
Text: tableModulecallsTpl,
Text: string(markdownTableTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
"fmt"
"regexp"
gotemplate "text/template"
@@ -20,105 +21,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
prettyHeaderTpl = `
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ colorize "\033[90m" . }}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
`
prettyResourcesTpl = `
{{- if .Settings.ShowResources -}}
{{- with .Module.Resources }}
{{- range . }}
{{- if eq (len .URL) 0 }}
{{- printf "resource.%s" .FullType | colorize "\033[36m" }}
{{- else -}}
{{- printf "resource.%s" .FullType | colorize "\033[36m" }} ({{ .URL}})
{{- end }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
`
prettyRequirementsTpl = `
{{- if .Settings.ShowRequirements -}}
{{- with .Module.Requirements }}
{{- range . }}
{{- $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
{{- printf "requirement.%s" .Name | colorize "\033[36m" }}{{ $version }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
`
prettyProvidersTpl = `
{{- if .Settings.ShowProviders -}}
{{- with .Module.Providers }}
{{- range . }}
{{- $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
{{- printf "provider.%s" .FullName | colorize "\033[36m" }}{{ $version }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
`
prettyInputsTpl = `
{{- if .Settings.ShowInputs -}}
{{- with .Module.Inputs }}
{{- range . }}
{{- printf "input.%s" .Name | colorize "\033[36m" }} ({{ default "required" .GetValue }})
{{ tostring .Description | trimSuffix "\n" | default "n/a" | colorize "\033[90m" }}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
{{- printf "\n" -}}
{{ end -}}
`
prettyOutputsTpl = `
{{- if .Settings.ShowOutputs -}}
{{- with .Module.Outputs }}
{{- range . }}
{{- printf "output.%s" .Name | colorize "\033[36m" }}
{{- if $.Settings.OutputValues -}}
{{- printf " " -}}
({{ ternary .Sensitive "<sensitive>" .GetValue }})
{{- end }}
{{ tostring .Description | trimSuffix "\n" | default "n/a" | colorize "\033[90m" }}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
{{ end -}}
`
prettyModulecallsTpl = `
{{- if .Settings.ShowModuleCalls -}}
{{- with .Module.ModuleCalls }}
{{- range . }}
{{- printf "modulecall.%s" .Name | colorize "\033[36m" }}{{ printf " (%s)" .FullName }}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
`
prettyTpl = `
{{- template "header" . -}}
{{- template "requirements" . -}}
{{- template "providers" . -}}
{{- template "modulecalls" . -}}
{{- template "resources" . -}}
{{- template "inputs" . -}}
{{- template "outputs" . -}}
`
)
//go:embed templates/pretty.tmpl
var prettyTpl []byte
// Pretty represents colorized pretty format.
type Pretty struct {
@@ -129,28 +33,7 @@ type Pretty struct {
func NewPretty(settings *print.Settings) print.Engine {
tt := template.New(settings, &template.Item{
Name: "pretty",
Text: prettyTpl,
}, &template.Item{
Name: "header",
Text: prettyHeaderTpl,
}, &template.Item{
Name: "requirements",
Text: prettyRequirementsTpl,
}, &template.Item{
Name: "providers",
Text: prettyProvidersTpl,
}, &template.Item{
Name: "resources",
Text: prettyResourcesTpl,
}, &template.Item{
Name: "inputs",
Text: prettyInputsTpl,
}, &template.Item{
Name: "outputs",
Text: prettyOutputsTpl,
}, &template.Item{
Name: "modulecalls",
Text: prettyModulecallsTpl,
Text: string(prettyTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"colorize": func(c string, s string) string {

View File

@@ -0,0 +1,149 @@
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "=" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
The following requirements are needed by this module:
{{- range .Module.Requirements }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .Name }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowProviders -}}
{{ indent 0 "=" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
The following providers are used by this module:
{{- range .Module.Providers }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .FullName }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "=" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
The following Modules are called:
{{- range .Module.ModuleCalls }}
{{ indent 1 "=" }} {{ name .Name }}
Source: {{ .Source }}
Version: {{ .Version }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowResources -}}
{{ indent 0 "=" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
The following resources are used by this module:
{{ range .Module.Resources }}
{{ if eq (len .URL) 0 }}
- {{ .FullType }}
{{- else -}}
- {{ .URL }}[{{ .FullType }}]
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowInputs -}}
{{- if .Settings.ShowRequired -}}
{{ indent 0 "=" }} Required Inputs
{{ if not .Module.RequiredInputs }}
No required inputs.
{{ else }}
The following input variables are required:
{{- range .Module.RequiredInputs }}
{{ printf "\n" }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{- end }}
{{ indent 0 "=" }} Optional Inputs
{{ if not .Module.OptionalInputs }}
No optional inputs.
{{ else }}
The following input variables are optional (have default values):
{{- range .Module.OptionalInputs }}
{{ printf "\n" }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{ end }}
{{ else -}}
{{ indent 0 "=" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
The following input variables are supported:
{{- range .Module.Inputs }}
{{ printf "\n" }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{ end }}
{{- end }}
{{ end -}}
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "=" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
The following outputs are exported:
{{- range .Module.Outputs }}
{{ indent 1 "=" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
{{ if $.Settings.OutputValues }}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
Value: {{ value $sensitive | sanitizeDoc }}
{{ if $.Settings.ShowSensitivity -}}
Sensitive: {{ ternary (.Sensitive) "yes" "no" }}
{{- end }}
{{ end }}
{{ end }}
{{ end }}
{{ end -}}

View File

@@ -0,0 +1,111 @@
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "=" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
[cols="a,a",options="header,autowidth"]
|===
|Name |Version
{{- range .Module.Requirements }}
|{{ .Name }} |{{ tostring .Version | default "n/a" }}
{{- end }}
|===
{{ end }}
{{ end -}}
{{- if .Settings.ShowProviders -}}
{{ indent 0 "=" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
[cols="a,a",options="header,autowidth"]
|===
|Name |Version
{{- range .Module.Providers }}
|{{ .FullName }} |{{ tostring .Version | default "n/a" }}
{{- end }}
|===
{{ end }}
{{ end -}}
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "=" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
{{- range .Module.ModuleCalls }}
|{{ .Name }}|{{ .Source }}|{{ .Version }}
{{- end }}
|===
{{ end }}
{{ end -}}
{{- if .Settings.ShowResources -}}
{{ indent 0 "=" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
[cols="a",options="header,autowidth"]
|===
|Name
{{- range .Module.Resources }}
{{ if eq (len .URL) 0 }}
|{{ .FullType }}
{{- else -}}
|{{ .URL }}[{{ .FullType }}]
{{- end }}
{{- end }}
|===
{{ end }}
{{ end -}}
{{- if .Settings.ShowInputs -}}
{{ indent 0 "=" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
[cols="a,a,a,a{{ if .Settings.ShowRequired }},a{{ end }}",options="header,autowidth"]
|===
|Name |Description |Type |Default{{ if .Settings.ShowRequired }} |Required{{ end }}
{{- range .Module.Inputs }}
|{{ .Name }}
|{{ tostring .Description | sanitizeAsciidocTbl }}
|{{ tostring .Type | type | sanitizeAsciidocTbl }}
|{{ value .GetValue | sanitizeAsciidocTbl }}
{{ if $.Settings.ShowRequired }}|{{ ternary .Required "yes" "no" }}{{ end }}
{{ end }}
|===
{{ end }}
{{ end -}}
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "=" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
[cols="a,a{{ if .Settings.OutputValues }},a{{ if $.Settings.ShowSensitivity }},a{{ end }}{{ end }}",options="header,autowidth"]
|===
|Name |Description{{ if .Settings.OutputValues }} |Value{{ if $.Settings.ShowSensitivity }} |Sensitive{{ end }}{{ end }}
{{- range .Module.Outputs }}
|{{ .Name }} |{{ tostring .Description | sanitizeAsciidocTbl }}
{{- if $.Settings.OutputValues -}}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
{{ printf " " }}|{{ value $sensitive }}
{{- if $.Settings.ShowSensitivity -}}
{{ printf " " }}|{{ ternary .Sensitive "yes" "no" }}
{{- end -}}
{{- end -}}
{{- end }}
|===
{{ end }}
{{ end -}}

View File

@@ -0,0 +1,150 @@
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "#" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
The following requirements are needed by this module:
{{- range .Module.Requirements }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .Name }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowProviders -}}
{{ indent 0 "#" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
The following providers are used by this module:
{{- range .Module.Providers }}
{{ $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
- {{ name .FullName }}{{ $version }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "#" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
The following Modules are called:
{{- range .Module.ModuleCalls }}
{{ indent 1 "#" }} {{ name .Name }}
Source: {{ .Source }}
Version: {{ .Version }}
{{ end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowResources -}}
{{ indent 0 "#" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
The following resources are used by this module:
{{ range .Module.Resources }}
{{ if eq (len .URL) 0 }}
- {{ .FullType }}
{{- else -}}
- [{{ .FullType }}]({{ .URL }})
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowInputs -}}
{{- if .Settings.ShowRequired -}}
{{ indent 0 "#" }} Required Inputs
{{ if not .Module.RequiredInputs }}
No required inputs.
{{ else }}
The following input variables are required:
{{- range .Module.RequiredInputs }}
{{ printf "\n" }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{- end }}
{{ indent 0 "#" }} Optional Inputs
{{ if not .Module.OptionalInputs }}
No optional inputs.
{{ else }}
The following input variables are optional (have default values):
{{- range .Module.OptionalInputs }}
{{ printf "\n" }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{ end }}
{{ else -}}
{{ indent 0 "#" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
The following input variables are supported:
{{- range .Module.Inputs }}
{{ printf "\n" }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
Type: {{ tostring .Type | type }}
{{ if or .HasDefault (not isRequired) }}
Default: {{ default "n/a" .GetValue | value }}
{{- end }}
{{- end }}
{{ end }}
{{- end }}
{{ end -}}
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "#" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
The following outputs are exported:
{{- range .Module.Outputs }}
{{ indent 1 "#" }} {{ name .Name }}
Description: {{ tostring .Description | sanitizeDoc }}
{{ if $.Settings.OutputValues }}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
Value: {{ value $sensitive | sanitizeDoc }}
{{ if $.Settings.ShowSensitivity -}}
Sensitive: {{ ternary (.Sensitive) "yes" "no" }}
{{- end }}
{{ end }}
{{ end }}
{{ end }}
{{ end -}}

View File

@@ -0,0 +1,98 @@
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ sanitizeHeader . }}
{{ printf "\n" }}
{{- end -}}
{{ end -}}
{{- if .Settings.ShowRequirements -}}
{{ indent 0 "#" }} Requirements
{{ if not .Module.Requirements }}
No requirements.
{{ else }}
| Name | Version |
|------|---------|
{{- range .Module.Requirements }}
| {{ name .Name }} | {{ tostring .Version | default "n/a" }} |
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowProviders -}}
{{ indent 0 "#" }} Providers
{{ if not .Module.Providers }}
No providers.
{{ else }}
| Name | Version |
|------|---------|
{{- range .Module.Providers }}
| {{ name .FullName }} | {{ tostring .Version | default "n/a" }} |
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowModuleCalls -}}
{{ indent 0 "#" }} Modules
{{ if not .Module.ModuleCalls }}
No modules.
{{ else }}
| Name | Source | Version |
|------|--------|---------|
{{- range .Module.ModuleCalls }}
| {{ .Name }} | {{ .Source }} | {{ .Version }} |
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowResources -}}
{{ indent 0 "#" }} Resources
{{ if not .Module.Resources }}
No resources.
{{ else }}
| Name |
|------|
{{- range .Module.Resources }}
{{ if eq (len .URL) 0 }}
| {{ .FullType }}
{{- else -}}
| [{{ .FullType }}]({{ .URL }}) |
{{- end }}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowInputs -}}
{{ indent 0 "#" }} Inputs
{{ if not .Module.Inputs }}
No inputs.
{{ else }}
| Name | Description | Type | Default |{{ if .Settings.ShowRequired }} Required |{{ end }}
|------|-------------|------|---------|{{ if .Settings.ShowRequired }}:--------:|{{ end }}
{{- range .Module.Inputs }}
| {{ name .Name }} | {{ tostring .Description | sanitizeTbl }} | {{ tostring .Type | type | sanitizeTbl }} | {{ value .GetValue | sanitizeTbl }} |
{{- if $.Settings.ShowRequired -}}
{{ printf " " }}{{ ternary .Required "yes" "no" }} |
{{- end -}}
{{- end }}
{{ end }}
{{ end -}}
{{- if .Settings.ShowOutputs -}}
{{ indent 0 "#" }} Outputs
{{ if not .Module.Outputs }}
No outputs.
{{ else }}
| Name | Description |{{ if .Settings.OutputValues }} Value |{{ if $.Settings.ShowSensitivity }} Sensitive |{{ end }}{{ end }}
|------|-------------|{{ if .Settings.OutputValues }}-------|{{ if $.Settings.ShowSensitivity }}:---------:|{{ end }}{{ end }}
{{- range .Module.Outputs }}
| {{ name .Name }} | {{ tostring .Description | sanitizeTbl }} |
{{- if $.Settings.OutputValues -}}
{{- $sensitive := ternary .Sensitive "<sensitive>" .GetValue -}}
{{ printf " " }}{{ value $sensitive | sanitizeTbl }} |
{{- if $.Settings.ShowSensitivity -}}
{{ printf " " }}{{ ternary .Sensitive "yes" "no" }} |
{{- end -}}
{{- end -}}
{{- end }}
{{ end }}
{{ end -}}

View File

@@ -0,0 +1,73 @@
{{- if .Settings.ShowHeader -}}
{{- with .Module.Header -}}
{{ colorize "\033[90m" . }}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{- if .Settings.ShowRequirements -}}
{{- with .Module.Requirements }}
{{- range . }}
{{- $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
{{- printf "requirement.%s" .Name | colorize "\033[36m" }}{{ $version }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{- if .Settings.ShowProviders -}}
{{- with .Module.Providers }}
{{- range . }}
{{- $version := ternary (tostring .Version) (printf " (%s)" .Version) "" }}
{{- printf "provider.%s" .FullName | colorize "\033[36m" }}{{ $version }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{- if .Settings.ShowModuleCalls -}}
{{- with .Module.ModuleCalls }}
{{- range . }}
{{- printf "modulecall.%s" .Name | colorize "\033[36m" }}{{ printf " (%s)" .FullName }}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
{{- if .Settings.ShowResources -}}
{{- with .Module.Resources }}
{{- range . }}
{{- if eq (len .URL) 0 }}
{{- printf "resource.%s" .FullType | colorize "\033[36m" }}
{{- else -}}
{{- printf "resource.%s" .FullType | colorize "\033[36m" }} ({{ .URL}})
{{- end }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{- if .Settings.ShowInputs -}}
{{- with .Module.Inputs }}
{{- range . }}
{{- printf "input.%s" .Name | colorize "\033[36m" }} ({{ default "required" .GetValue }})
{{ tostring .Description | trimSuffix "\n" | default "n/a" | colorize "\033[90m" }}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
{{- printf "\n" -}}
{{ end -}}
{{- if .Settings.ShowOutputs -}}
{{- with .Module.Outputs }}
{{- range . }}
{{- printf "output.%s" .Name | colorize "\033[36m" }}
{{- if $.Settings.OutputValues -}}
{{- printf " " -}}
({{ ternary .Sensitive "<sensitive>" .GetValue }})
{{- end }}
{{ tostring .Description | trimSuffix "\n" | default "n/a" | colorize "\033[90m" }}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}
{{ end -}}

View File

@@ -0,0 +1,5 @@
{{- if .Module.Inputs -}}
{{- range $i, $k := .Module.Inputs -}}
{{ align $k.Name $i }} = {{ value $k.GetValue }}
{{ end -}}
{{- end -}}

View File

@@ -11,6 +11,7 @@ the root directory of this source tree.
package format
import (
_ "embed" //nolint
"fmt"
"reflect"
"strings"
@@ -21,15 +22,8 @@ import (
"github.com/terraform-docs/terraform-docs/internal/terraform"
)
const (
tfvarsHCLTpl = `
{{- if .Module.Inputs -}}
{{- range $i, $k := .Module.Inputs -}}
{{ align $k.Name $i }} = {{ value $k.GetValue }}
{{ end -}}
{{- end -}}
`
)
//go:embed templates/tfvars_hcl.tmpl
var tfvarsHCLTpl []byte
// TfvarsHCL represents Terraform tfvars HCL format.
type TfvarsHCL struct {
@@ -42,7 +36,7 @@ var padding []int
func NewTfvarsHCL(settings *print.Settings) print.Engine {
tt := template.New(settings, &template.Item{
Name: "tfvars",
Text: tfvarsHCLTpl,
Text: string(tfvarsHCLTpl),
})
tt.CustomFunc(gotemplate.FuncMap{
"align": func(s string, i int) string {