-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtemplate.go
More file actions
439 lines (328 loc) · 12 KB
/
template.go
File metadata and controls
439 lines (328 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"bytes"
"fmt"
"io"
"path/filepath"
"strings"
"text/template"
"golang.org/x/text/cases"
"golang.org/x/text/language"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-docs/internal/schemamd"
"github.com/hashicorp/terraform-plugin-docs/internal/functionmd"
"github.com/hashicorp/terraform-plugin-docs/internal/mdplain"
"github.com/hashicorp/terraform-plugin-docs/internal/tmplfuncs"
)
const (
schemaComment = "<!-- schema generated by tfplugindocs -->"
signatureComment = "<!-- signature generated by tfplugindocs -->"
argumentComment = "<!-- arguments generated by tfplugindocs -->"
variadicComment = "<!-- variadic argument generated by tfplugindocs -->"
frontmatterComment = "# generated by https://github.com/hashicorp/terraform-plugin-docs"
)
type (
resourceTemplate string
functionTemplate string
providerTemplate string
docTemplate string
)
type ResourceTemplateType struct {
Type string
Name string
Description string
HasExample bool
HasExamples bool
ExampleFile string
ExampleFiles []string
HasImport bool
ImportFile string
HasImportIDConfig bool
ImportIDConfigFile string
HasImportIdentityConfig bool
ImportIdentityConfigFile string
IdentitySchemaMarkdown string
ProviderName string
ProviderShortName string
SchemaMarkdown string
RenderedProviderName string
}
type ProviderTemplateType struct {
Description string
HasExample bool
HasExamples bool
ExampleFile string
ExampleFiles []string
ProviderName string
ProviderShortName string
SchemaMarkdown string
RenderedProviderName string
}
type FunctionTemplateType struct {
Type string
Name string
Description string
Summary string
HasExample bool
HasExamples bool
ExampleFile string
ExampleFiles []string
ProviderName string
ProviderShortName string
FunctionSignatureMarkdown string
FunctionArgumentsMarkdown string
HasVariadic bool
FunctionVariadicArgumentMarkdown string
RenderedProviderName string
}
func newTemplate(providerDir, name, text string) (*template.Template, error) {
tmpl := template.New(name)
titleCaser := cases.Title(language.Und)
tmpl.Funcs(map[string]interface{}{
"codefile": codeFile(providerDir),
"lower": strings.ToLower,
"plainmarkdown": mdplain.PlainMarkdown,
"prefixlines": tmplfuncs.PrefixLines,
"split": strings.Split,
"tffile": terraformCodeFile(providerDir),
"title": titleCaser.String,
"trimspace": strings.TrimSpace,
"upper": strings.ToUpper,
})
var err error
tmpl, err = tmpl.Parse(text)
if err != nil {
return nil, fmt.Errorf("unable to parse template %q: %w", text, err)
}
return tmpl, nil
}
func codeFile(providerDir string) func(string, string) (string, error) {
return func(format string, file string) (string, error) {
if filepath.IsAbs(file) {
return tmplfuncs.CodeFile(format, file)
}
return tmplfuncs.CodeFile(format, filepath.Join(providerDir, file))
}
}
func terraformCodeFile(providerDir string) func(string) (string, error) {
// TODO: omit comment handling
return func(file string) (string, error) {
if filepath.IsAbs(file) {
return tmplfuncs.CodeFile("terraform", file)
}
return tmplfuncs.CodeFile("terraform", filepath.Join(providerDir, file))
}
}
func renderTemplate(providerDir, name string, text string, out io.Writer, data interface{}) error {
tmpl, err := newTemplate(providerDir, name, text)
if err != nil {
return err
}
err = tmpl.Execute(out, data)
if err != nil {
return fmt.Errorf("unable to execute template: %w", err)
}
return nil
}
func renderStringTemplate(providerDir, name, text string, data interface{}) (string, error) {
var buf bytes.Buffer
err := renderTemplate(providerDir, name, text, &buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (t docTemplate) Render(providerDir string, out io.Writer) error {
s := string(t)
if s == "" {
return nil
}
return renderTemplate(providerDir, "docTemplate", s, out, nil)
}
func (t providerTemplate) Render(providerDir, providerName, renderedProviderName, exampleFile string, exampleFiles []string, schema *tfjson.Schema, blocksSection bool) (string, error) {
schemaBuffer := bytes.NewBuffer(nil)
err := schemamd.Render(schema, schemaBuffer, blocksSection)
if err != nil {
return "", fmt.Errorf("unable to render schema: %w", err)
}
s := string(t)
if s == "" {
return "", nil
}
return renderStringTemplate(providerDir, "providerTemplate", s, ProviderTemplateType{
Description: schema.Block.Description,
HasExample: exampleFile != "" && fileExists(exampleFile),
HasExamples: len(exampleFiles) > 0,
ExampleFile: exampleFile,
ExampleFiles: exampleFiles,
ProviderName: providerName,
ProviderShortName: providerShortName(renderedProviderName),
SchemaMarkdown: schemaComment + "\n" + schemaBuffer.String(),
RenderedProviderName: renderedProviderName,
})
}
func (t resourceTemplate) Render(providerDir, name, providerName, renderedProviderName, typeName, exampleFile string, exampleFiles []string, importIDConfigFile, importIdentityConfigFile, importCmdFile string, schema *tfjson.Schema, identitySchema *tfjson.IdentitySchema, blocksSection bool) (string, error) {
schemaBuffer := bytes.NewBuffer(nil)
err := schemamd.Render(schema, schemaBuffer, blocksSection)
if err != nil {
return "", fmt.Errorf("unable to render schema: %w", err)
}
s := string(t)
if s == "" {
return "", nil
}
hasImportIdentityConfig := importIdentityConfigFile != "" && fileExists(importIdentityConfigFile)
identitySchemaBuffer := bytes.NewBuffer(nil)
// Always render the identity schema if we have one, so it can be used in custom templates.
if identitySchema != nil {
_, err := io.WriteString(identitySchemaBuffer, schemaComment+"\n")
if err != nil {
return "", fmt.Errorf("unable to render identity schema comment: %w", err)
}
err = schemamd.RenderIdentitySchema(identitySchema, identitySchemaBuffer)
if err != nil {
return "", fmt.Errorf("unable to render identity schema: %w", err)
}
} else if hasImportIdentityConfig {
// If there is an identity example, but we don't have an identity schema, we should return an error to ensure the example file was intended.
return "", fmt.Errorf("unable to render: an identity import example (%q) was provided for a resource (%q) that does not support resource identity", importIdentityConfigFile, name)
}
return renderStringTemplate(providerDir, "resourceTemplate", s, ResourceTemplateType{
Type: typeName,
Name: name,
Description: schema.Block.Description,
HasExample: exampleFile != "" && fileExists(exampleFile),
HasExamples: len(exampleFiles) > 0,
ExampleFile: exampleFile,
ExampleFiles: exampleFiles,
HasImport: importCmdFile != "" && fileExists(importCmdFile),
ImportFile: importCmdFile,
HasImportIDConfig: importIDConfigFile != "" && fileExists(importIDConfigFile),
ImportIDConfigFile: importIDConfigFile,
HasImportIdentityConfig: hasImportIdentityConfig,
ImportIdentityConfigFile: importIdentityConfigFile,
IdentitySchemaMarkdown: identitySchemaBuffer.String(),
ProviderName: providerName,
ProviderShortName: providerShortName(renderedProviderName),
SchemaMarkdown: schemaComment + "\n" + schemaBuffer.String(),
RenderedProviderName: renderedProviderName,
})
}
func (t functionTemplate) Render(providerDir, name, providerName, renderedProviderName, typeName, exampleFile string, exampleFiles []string, signature *tfjson.FunctionSignature) (string, error) {
funcSig, err := functionmd.RenderSignature(name, signature)
if err != nil {
return "", fmt.Errorf("unable to render function signature: %w", err)
}
funcArgs, err := functionmd.RenderArguments(signature)
if err != nil {
return "", fmt.Errorf("unable to render function arguments: %w", err)
}
funcVarArg, err := functionmd.RenderVariadicArg(signature)
if err != nil {
return "", fmt.Errorf("unable to render variadic argument: %w", err)
}
s := string(t)
if s == "" {
return "", nil
}
return renderStringTemplate(providerDir, "resourceTemplate", s, FunctionTemplateType{
Type: typeName,
Name: name,
Description: signature.Description,
Summary: signature.Summary,
HasExample: exampleFile != "" && fileExists(exampleFile),
HasExamples: len(exampleFiles) > 0,
ExampleFile: exampleFile,
ExampleFiles: exampleFiles,
ProviderName: providerName,
ProviderShortName: providerShortName(renderedProviderName),
FunctionSignatureMarkdown: signatureComment + "\n" + funcSig,
FunctionArgumentsMarkdown: argumentComment + "\n" + funcArgs,
HasVariadic: signature.VariadicParameter != nil,
FunctionVariadicArgumentMarkdown: variadicComment + "\n" + funcVarArg,
RenderedProviderName: renderedProviderName,
})
}
const defaultResourceTemplate resourceTemplate = `---
` + frontmatterComment + `
page_title: "{{.Name}} {{.Type}} - {{.RenderedProviderName}}"
subcategory: ""
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---
# {{.Name}} ({{.Type}})
{{ .Description | trimspace }}
{{ if .HasExamples -}}
## Example Usage
{{- range .ExampleFiles }}
{{ tffile . }}
{{- end }}
{{- end }}
{{ .SchemaMarkdown | trimspace }}
{{- if or .HasImport .HasImportIDConfig .HasImportIdentityConfig }}
## Import
Import is supported using the following syntax:
{{- end }}
{{- if .HasImportIdentityConfig }}
In Terraform v1.12.0 and later, the [` + "`" + `import` + "`" + ` block](https://developer.hashicorp.com/terraform/language/import) can be used with the ` + "`" + `identity` + "`" + ` attribute, for example:
{{tffile .ImportIdentityConfigFile }}
{{ .IdentitySchemaMarkdown | trimspace }}
{{- end }}
{{- if .HasImportIDConfig }}
In Terraform v1.5.0 and later, the [` + "`" + `import` + "`" + ` block](https://developer.hashicorp.com/terraform/language/import) can be used with the ` + "`" + `id` + "`" + ` attribute, for example:
{{tffile .ImportIDConfigFile }}
{{- end }}
{{- if .HasImport }}
The [` + "`" + `terraform import` + "`" + ` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
{{codefile "shell" .ImportFile }}
{{- end }}
`
const defaultFunctionTemplate functionTemplate = `---
` + frontmatterComment + `
page_title: "{{.Name}} {{.Type}} - {{.RenderedProviderName}}"
subcategory: ""
description: |-
{{ .Summary | plainmarkdown | trimspace | prefixlines " " }}
---
# {{.Type}}: {{.Name}}
{{ .Description | trimspace }}
{{ if .HasExamples -}}
## Example Usage
{{- range .ExampleFiles }}
{{ tffile . }}
{{- end }}
{{- end }}
## Signature
{{ .FunctionSignatureMarkdown }}
## Arguments
{{ .FunctionArgumentsMarkdown }}
{{- if .HasVariadic }}
{{ .FunctionVariadicArgumentMarkdown }}
{{- end }}
`
const defaultProviderTemplate providerTemplate = `---
` + frontmatterComment + `
page_title: "{{.ProviderShortName}} Provider"
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---
# {{.ProviderShortName}} Provider
{{ .Description | trimspace }}
{{ if .HasExamples -}}
## Example Usage
{{- range .ExampleFiles }}
{{ tffile . }}
{{- end }}
{{- end }}
{{ .SchemaMarkdown | trimspace }}
`
const migrateProviderTemplateComment string = `
{{/* This template serves as a starting point for documentation generation, and can be customized with hardcoded values and/or doc gen templates.
For example, the {{ .SchemaMarkdown }} template can be used to replace manual schema documentation if descriptions of schema attributes are added in the provider source code. */ -}}
`
const migrateFunctionTemplateComment string = `
{{/* This template serves as a starting point for documentation generation, and can be customized with hardcoded values and/or doc gen templates.
For example, the {{ .FunctionArgumentsMarkdown }} template can be used to replace manual argument documentation if descriptions of function arguments are added in the provider source code. */ -}}
`