| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package tfconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"io" |
| 9 |
"strings" |
| 10 |
"text/template" |
| 11 |
) |
| 12 |
|
| 13 |
func RenderMarkdown(w io.Writer, module *Module) error { |
| 14 |
tmpl := template.New("md") |
| 15 |
tmpl.Funcs(template.FuncMap{ |
| 16 |
"tt": func(s string) string { |
| 17 |
return "`" + s + "`" |
| 18 |
}, |
| 19 |
"commas": func(s []string) string { |
| 20 |
return strings.Join(s, ", ") |
| 21 |
}, |
| 22 |
"json": func(v interface{}) (string, error) { |
| 23 |
j, err := json.Marshal(v) |
| 24 |
return string(j), err |
| 25 |
}, |
| 26 |
"severity": func(s DiagSeverity) string { |
| 27 |
switch s { |
| 28 |
case DiagError: |
| 29 |
return "Error: " |
| 30 |
case DiagWarning: |
| 31 |
return "Warning: " |
| 32 |
default: |
| 33 |
return "" |
| 34 |
} |
| 35 |
}, |
| 36 |
}) |
| 37 |
template.Must(tmpl.Parse(markdownTemplate)) |
| 38 |
return tmpl.Execute(w, module) |
| 39 |
} |
| 40 |
|
| 41 |
const markdownTemplate = ` |
| 42 |
# Module {{ tt .Path }} |
| 43 |
|
| 44 |
{{- if .RequiredCore}} |
| 45 |
|
| 46 |
Core Version Constraints: |
| 47 |
{{- range .RequiredCore }} |
| 48 |
* {{ tt . }} |
| 49 |
{{- end}}{{end}} |
| 50 |
|
| 51 |
{{- if .RequiredProviders}} |
| 52 |
|
| 53 |
Provider Requirements: |
| 54 |
{{- range $name, $req := .RequiredProviders }} |
| 55 |
* **{{ $name }}{{ if $req.Source }} ({{ $req.Source | tt }}){{ end }}:** {{ if $req.VersionConstraints }}{{ commas $req.VersionConstraints | tt }}{{ else }}(any version){{ end }} |
| 56 |
{{- end}}{{end}} |
| 57 |
|
| 58 |
{{- if .Variables}} |
| 59 |
|
| 60 |
## Input Variables |
| 61 |
{{- range .Variables }} |
| 62 |
* {{ tt .Name }}{{ if .Required }} (required){{else}} (default {{ json .Default | tt }}){{end}} |
| 63 |
{{- if .Description}}: {{ .Description }}{{ end }} |
| 64 |
{{- end}}{{end}} |
| 65 |
|
| 66 |
{{- if .Outputs}} |
| 67 |
|
| 68 |
## Output Values |
| 69 |
{{- range .Outputs }} |
| 70 |
* {{ tt .Name }}{{ if .Description}}: {{ .Description }}{{ end }} |
| 71 |
{{- end}}{{end}} |
| 72 |
|
| 73 |
{{- if .ManagedResources}} |
| 74 |
|
| 75 |
## Managed Resources |
| 76 |
{{- range .ManagedResources }} |
| 77 |
* {{ printf "%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }} |
| 78 |
{{- end}}{{end}} |
| 79 |
|
| 80 |
{{- if .DataResources}} |
| 81 |
|
| 82 |
## Data Resources |
| 83 |
{{- range .DataResources }} |
| 84 |
* {{ printf "data.%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }} |
| 85 |
{{- end}}{{end}} |
| 86 |
|
| 87 |
{{- if .ModuleCalls}} |
| 88 |
|
| 89 |
## Child Modules |
| 90 |
{{- range .ModuleCalls }} |
| 91 |
* {{ tt .Name }} from {{ tt .Source }}{{ if .Version }} ({{ tt .Version }}){{ end }} |
| 92 |
{{- end}}{{end}} |
| 93 |
|
| 94 |
{{- if .Diagnostics}} |
| 95 |
|
| 96 |
## Problems |
| 97 |
{{- range .Diagnostics }} |
| 98 |
|
| 99 |
## {{ severity .Severity }}{{ .Summary }}{{ if .Pos }} |
| 100 |
|
| 101 |
(at {{ tt .Pos.Filename }} line {{ .Pos.Line }}{{ end }}) |
| 102 |
{{ if .Detail }} |
| 103 |
{{ .Detail }} |
| 104 |
{{- end }} |
| 105 |
|
| 106 |
{{- end}}{{end}} |
| 107 |
|
| 108 |
` |