forked from deepmap/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.go
754 lines (634 loc) · 21.5 KB
/
codegen.go
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright 2019 DeepMap, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codegen
import (
"bufio"
"bytes"
"embed"
"fmt"
"io/fs"
"runtime/debug"
"sort"
"strings"
"text/template"
"github.com/getkin/kin-openapi/openapi3"
"golang.org/x/tools/imports"
)
// Embed the templates directory
//go:embed templates
var templates embed.FS
// globalState stores all global state. Please don't put global state anywhere
// else so that we can easily track it.
var globalState struct {
options Configuration
spec *openapi3.T
}
// goImport represents a go package to be imported in the generated code
type goImport struct {
Name string // package name
Path string // package path
}
// String returns a go import statement
func (gi goImport) String() string {
if gi.Name != "" {
return fmt.Sprintf("%s %q", gi.Name, gi.Path)
}
return fmt.Sprintf("%q", gi.Path)
}
// importMap maps external OpenAPI specifications files/urls to external go packages
type importMap map[string]goImport
// GoImports returns a slice of go import statements
func (im importMap) GoImports() []string {
goImports := make([]string, 0, len(im))
for _, v := range im {
goImports = append(goImports, v.String())
}
return goImports
}
var importMapping importMap
func constructImportMapping(importMapping map[string]string) importMap {
var (
pathToName = map[string]string{}
result = importMap{}
)
{
var packagePaths []string
for _, packageName := range importMapping {
packagePaths = append(packagePaths, packageName)
}
sort.Strings(packagePaths)
for _, packagePath := range packagePaths {
if _, ok := pathToName[packagePath]; !ok {
pathToName[packagePath] = fmt.Sprintf("externalRef%d", len(pathToName))
}
}
}
for specPath, packagePath := range importMapping {
result[specPath] = goImport{Name: pathToName[packagePath], Path: packagePath}
}
return result
}
// Generate uses the Go templating engine to generate all of our server wrappers from
// the descriptions we've built up above from the schema objects.
// opts defines
func Generate(spec *openapi3.T, opts Configuration) (string, error) {
// This is global state
globalState.options = opts
globalState.spec = spec
importMapping = constructImportMapping(opts.ImportMapping)
filterOperationsByTag(spec, opts)
if !opts.OutputOptions.SkipPrune {
pruneUnusedComponents(spec)
}
// if we are provided an override for the response type suffix update it
if opts.OutputOptions.ResponseTypeSuffix != "" {
responseTypeSuffix = opts.OutputOptions.ResponseTypeSuffix
}
// This creates the golang templates text package
TemplateFunctions["opts"] = func() Configuration { return globalState.options }
t := template.New("oapi-codegen").Funcs(TemplateFunctions)
// This parses all of our own template files into the template object
// above
err := LoadTemplates(templates, t)
if err != nil {
return "", fmt.Errorf("error parsing oapi-codegen templates: %w", err)
}
// Override built-in templates with user-provided versions
for _, tpl := range t.Templates() {
if _, ok := opts.OutputOptions.UserTemplates[tpl.Name()]; ok {
utpl := t.New(tpl.Name())
if _, err := utpl.Parse(opts.OutputOptions.UserTemplates[tpl.Name()]); err != nil {
return "", fmt.Errorf("error parsing user-provided template %q: %w", tpl.Name(), err)
}
}
}
ops, err := OperationDefinitions(spec)
if err != nil {
return "", fmt.Errorf("error creating operation definitions: %w", err)
}
var typeDefinitions, constantDefinitions string
if opts.Generate.Models {
typeDefinitions, err = GenerateTypeDefinitions(t, spec, ops, opts.OutputOptions.ExcludeSchemas)
if err != nil {
return "", fmt.Errorf("error generating type definitions: %w", err)
}
constantDefinitions, err = GenerateConstants(t, ops)
if err != nil {
return "", fmt.Errorf("error generating constants: %w", err)
}
}
var echoServerOut string
if opts.Generate.EchoServer {
echoServerOut, err = GenerateEchoServer(t, ops)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
}
var chiServerOut string
if opts.Generate.ChiServer {
chiServerOut, err = GenerateChiServer(t, ops)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
}
var ginServerOut string
if opts.Generate.GinServer {
ginServerOut, err = GenerateGinServer(t, ops)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
}
var gorillaServerOut string
if opts.Generate.GorillaServer {
gorillaServerOut, err = GenerateGorillaServer(t, ops)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
}
var clientOut string
if opts.Generate.Client {
clientOut, err = GenerateClient(t, ops)
if err != nil {
return "", fmt.Errorf("error generating client: %w", err)
}
}
var clientWithResponsesOut string
if opts.Generate.Client {
clientWithResponsesOut, err = GenerateClientWithResponses(t, ops)
if err != nil {
return "", fmt.Errorf("error generating client with responses: %w", err)
}
}
var inlinedSpec string
if opts.Generate.EmbeddedSpec {
inlinedSpec, err = GenerateInlinedSpec(t, importMapping, spec)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
externalImports := importMapping.GoImports()
importsOut, err := GenerateImports(t, externalImports, opts.PackageName)
if err != nil {
return "", fmt.Errorf("error generating imports: %w", err)
}
_, err = w.WriteString(importsOut)
if err != nil {
return "", fmt.Errorf("error writing imports: %w", err)
}
_, err = w.WriteString(constantDefinitions)
if err != nil {
return "", fmt.Errorf("error writing constants: %w", err)
}
_, err = w.WriteString(typeDefinitions)
if err != nil {
return "", fmt.Errorf("error writing type definitions: %w", err)
}
if opts.Generate.Client {
_, err = w.WriteString(clientOut)
if err != nil {
return "", fmt.Errorf("error writing client: %w", err)
}
_, err = w.WriteString(clientWithResponsesOut)
if err != nil {
return "", fmt.Errorf("error writing client: %w", err)
}
}
if opts.Generate.EchoServer {
_, err = w.WriteString(echoServerOut)
if err != nil {
return "", fmt.Errorf("error writing server path handlers: %w", err)
}
}
if opts.Generate.ChiServer {
_, err = w.WriteString(chiServerOut)
if err != nil {
return "", fmt.Errorf("error writing server path handlers: %w", err)
}
}
if opts.Generate.GinServer {
_, err = w.WriteString(ginServerOut)
if err != nil {
return "", fmt.Errorf("error writing server path handlers: %w", err)
}
}
if opts.Generate.GorillaServer {
_, err = w.WriteString(gorillaServerOut)
if err != nil {
return "", fmt.Errorf("error writing server path handlers: %w", err)
}
}
if opts.Generate.EmbeddedSpec {
_, err = w.WriteString(inlinedSpec)
if err != nil {
return "", fmt.Errorf("error writing inlined spec: %w", err)
}
}
err = w.Flush()
if err != nil {
return "", fmt.Errorf("error flushing output buffer: %w", err)
}
// remove any byte-order-marks which break Go-Code
goCode := SanitizeCode(buf.String())
// The generation code produces unindented horrors. Use the Go Imports
// to make it all pretty.
if opts.OutputOptions.SkipFmt {
return goCode, nil
}
outBytes, err := imports.Process(opts.PackageName+".go", []byte(goCode), nil)
if err != nil {
return "", fmt.Errorf("error formatting Go code %s: %w", goCode, err)
}
return string(outBytes), nil
}
func GenerateTypeDefinitions(t *template.Template, swagger *openapi3.T, ops []OperationDefinition, excludeSchemas []string) (string, error) {
schemaTypes, err := GenerateTypesForSchemas(t, swagger.Components.Schemas, excludeSchemas)
if err != nil {
return "", fmt.Errorf("error generating Go types for component schemas: %w", err)
}
paramTypes, err := GenerateTypesForParameters(t, swagger.Components.Parameters)
if err != nil {
return "", fmt.Errorf("error generating Go types for component parameters: %w", err)
}
allTypes := append(schemaTypes, paramTypes...)
responseTypes, err := GenerateTypesForResponses(t, swagger.Components.Responses)
if err != nil {
return "", fmt.Errorf("error generating Go types for component responses: %w", err)
}
allTypes = append(allTypes, responseTypes...)
bodyTypes, err := GenerateTypesForRequestBodies(t, swagger.Components.RequestBodies)
if err != nil {
return "", fmt.Errorf("error generating Go types for component request bodies: %w", err)
}
allTypes = append(allTypes, bodyTypes...)
paramTypesOut, err := GenerateTypesForOperations(t, ops)
if err != nil {
return "", fmt.Errorf("error generating Go types for component request bodies: %w", err)
}
enumsOut, err := GenerateEnums(t, allTypes)
if err != nil {
return "", fmt.Errorf("error generating code for type enums: %w", err)
}
typesOut, err := GenerateTypes(t, allTypes)
if err != nil {
return "", fmt.Errorf("error generating code for type definitions: %w", err)
}
allOfBoilerplate, err := GenerateAdditionalPropertyBoilerplate(t, allTypes)
if err != nil {
return "", fmt.Errorf("error generating allOf boilerplate: %w", err)
}
unionBoilerplate, err := GenerateUnionBoilerplate(t, allTypes)
if err != nil {
return "", fmt.Errorf("error generating union boilerplate: %w", err)
}
typeDefinitions := strings.Join([]string{enumsOut, typesOut, paramTypesOut, allOfBoilerplate, unionBoilerplate}, "")
return typeDefinitions, nil
}
// Generates operation ids, context keys, paths, etc. to be exported as constants
func GenerateConstants(t *template.Template, ops []OperationDefinition) (string, error) {
constants := Constants{
SecuritySchemeProviderNames: []string{},
}
providerNameMap := map[string]struct{}{}
for _, op := range ops {
for _, def := range op.SecurityDefinitions {
providerName := SanitizeGoIdentity(def.ProviderName)
providerNameMap[providerName] = struct{}{}
}
}
var providerNames []string
for providerName := range providerNameMap {
providerNames = append(providerNames, providerName)
}
sort.Strings(providerNames)
constants.SecuritySchemeProviderNames = append(constants.SecuritySchemeProviderNames, providerNames...)
return GenerateTemplates([]string{"constants.tmpl"}, t, constants)
}
// GenerateTypesForSchemas generates type definitions for any custom types defined in the
// components/schemas section of the Swagger spec.
func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.SchemaRef, excludeSchemas []string) ([]TypeDefinition, error) {
excludeSchemasMap := make(map[string]bool)
for _, schema := range excludeSchemas {
excludeSchemasMap[schema] = true
}
types := make([]TypeDefinition, 0)
// We're going to define Go types for every object under components/schemas
for _, schemaName := range SortedSchemaKeys(schemas) {
if _, ok := excludeSchemasMap[schemaName]; ok {
continue
}
schemaRef := schemas[schemaName]
goSchema, err := GenerateGoSchema(schemaRef, []string{schemaName})
if err != nil {
return nil, fmt.Errorf("error converting Schema %s to Go type: %w", schemaName, err)
}
goTypeName, err := renameSchema(schemaName, schemaRef)
if err != nil {
return nil, fmt.Errorf("error making name for components/schemas/%s: %w", schemaName, err)
}
types = append(types, TypeDefinition{
JsonName: schemaName,
TypeName: goTypeName,
Schema: goSchema,
})
types = append(types, goSchema.GetAdditionalTypeDefs()...)
}
return types, nil
}
// GenerateTypesForParameters generates type definitions for any custom types defined in the
// components/parameters section of the Swagger spec.
func GenerateTypesForParameters(t *template.Template, params map[string]*openapi3.ParameterRef) ([]TypeDefinition, error) {
var types []TypeDefinition
for _, paramName := range SortedParameterKeys(params) {
paramOrRef := params[paramName]
goType, err := paramToGoType(paramOrRef.Value, nil)
if err != nil {
return nil, fmt.Errorf("error generating Go type for schema in parameter %s: %w", paramName, err)
}
goTypeName, err := renameParameter(paramName, paramOrRef)
if err != nil {
return nil, fmt.Errorf("error making name for components/parameters/%s: %w", paramName, err)
}
typeDef := TypeDefinition{
JsonName: paramName,
Schema: goType,
TypeName: goTypeName,
}
if paramOrRef.Ref != "" {
// Generate a reference type for referenced parameters
refType, err := RefPathToGoType(paramOrRef.Ref)
if err != nil {
return nil, fmt.Errorf("error generating Go type for (%s) in parameter %s: %w", paramOrRef.Ref, paramName, err)
}
typeDef.TypeName = SchemaNameToTypeName(refType)
}
types = append(types, typeDef)
}
return types, nil
}
// GenerateTypesForResponses generates type definitions for any custom types defined in the
// components/responses section of the Swagger spec.
func GenerateTypesForResponses(t *template.Template, responses openapi3.Responses) ([]TypeDefinition, error) {
var types []TypeDefinition
for _, responseName := range SortedResponsesKeys(responses) {
responseOrRef := responses[responseName]
// We have to generate the response object. We're only going to
// handle application/json media types here. Other responses should
// simply be specified as strings or byte arrays.
response := responseOrRef.Value
jsonResponse, found := response.Content["application/json"]
if found {
goType, err := GenerateGoSchema(jsonResponse.Schema, []string{responseName})
if err != nil {
return nil, fmt.Errorf("error generating Go type for schema in response %s: %w", responseName, err)
}
goTypeName, err := renameResponse(responseName, responseOrRef)
if err != nil {
return nil, fmt.Errorf("error making name for components/responses/%s: %w", responseName, err)
}
typeDef := TypeDefinition{
JsonName: responseName,
Schema: goType,
TypeName: goTypeName,
}
if responseOrRef.Ref != "" {
// Generate a reference type for referenced parameters
refType, err := RefPathToGoType(responseOrRef.Ref)
if err != nil {
return nil, fmt.Errorf("error generating Go type for (%s) in parameter %s: %w", responseOrRef.Ref, responseName, err)
}
typeDef.TypeName = SchemaNameToTypeName(refType)
}
types = append(types, typeDef)
}
}
return types, nil
}
// GenerateTypesForRequestBodies generates type definitions for any custom types defined in the
// components/requestBodies section of the Swagger spec.
func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*openapi3.RequestBodyRef) ([]TypeDefinition, error) {
var types []TypeDefinition
for _, requestBodyName := range SortedRequestBodyKeys(bodies) {
requestBodyRef := bodies[requestBodyName]
// As for responses, we will only generate Go code for JSON bodies,
// the other body formats are up to the user.
response := requestBodyRef.Value
jsonBody, found := response.Content["application/json"]
if found {
goType, err := GenerateGoSchema(jsonBody.Schema, []string{requestBodyName})
if err != nil {
return nil, fmt.Errorf("error generating Go type for schema in body %s: %w", requestBodyName, err)
}
goTypeName, err := renameRequestBody(requestBodyName, requestBodyRef)
if err != nil {
return nil, fmt.Errorf("error making name for components/schemas/%s: %w", requestBodyName, err)
}
typeDef := TypeDefinition{
JsonName: requestBodyName,
Schema: goType,
TypeName: goTypeName,
}
if requestBodyRef.Ref != "" {
// Generate a reference type for referenced bodies
refType, err := RefPathToGoType(requestBodyRef.Ref)
if err != nil {
return nil, fmt.Errorf("error generating Go type for (%s) in body %s: %w", requestBodyRef.Ref, requestBodyName, err)
}
typeDef.TypeName = SchemaNameToTypeName(refType)
}
types = append(types, typeDef)
}
}
return types, nil
}
// GenerateTypes passes a bunch of types to the template engine, and buffers
// its output into a string.
func GenerateTypes(t *template.Template, types []TypeDefinition) (string, error) {
m := map[string]bool{}
ts := []TypeDefinition{}
for _, t := range types {
if found := m[t.TypeName]; found {
continue
}
m[t.TypeName] = true
ts = append(ts, t)
}
context := struct {
Types []TypeDefinition
}{
Types: ts,
}
return GenerateTemplates([]string{"typedef.tmpl"}, t, context)
}
func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) {
enums := []EnumDefinition{}
// Keep track of which enums we've generated
m := map[string]bool{}
// These are all types defined globally
for _, tp := range types {
if found := m[tp.TypeName]; found {
continue
}
m[tp.TypeName] = true
if len(tp.Schema.EnumValues) > 0 {
wrapper := ""
if tp.Schema.GoType == "string" {
wrapper = `"`
}
enums = append(enums, EnumDefinition{
Schema: tp.Schema,
TypeName: tp.TypeName,
ValueWrapper: wrapper,
})
}
}
// Now, go through all the enums, and figure out if we have conflicts with
// any others.
for i := range enums {
// Look through all other enums not compared so far. Make sure we don't
// compare against self.
e1 := enums[i]
for j := i + 1; j < len(enums); j++ {
e2 := enums[j]
for e1key := range e1.GetValues() {
_, found := e2.GetValues()[e1key]
if found {
e1.Conflicts = true
e2.Conflicts = true
enums[i] = e1
enums[j] = e2
break
}
}
}
// now see if this enum conflicts with any global type names.
for _, tp := range types {
// Skip over enums, since we've handled those above.
if len(tp.Schema.EnumValues) > 0 {
continue
}
_, found := e1.Schema.EnumValues[tp.TypeName]
if found {
e1.Conflicts = true
enums[i] = e1
}
}
// Another edge case is that an enum value can conflict with its own
// type name.
_, found := e1.GetValues()[e1.TypeName]
if found {
e1.Conflicts = true
enums[i] = e1
}
}
// Now see if enums conflict with any non-enum typenames
return GenerateTemplates([]string{"constants.tmpl"}, t, Constants{EnumDefinitions: enums})
}
// Generate our import statements and package definition.
func GenerateImports(t *template.Template, externalImports []string, packageName string) (string, error) {
// Read build version for incorporating into generated files
// Unit tests have ok=false, so we'll just use "unknown" for the
// version if we can't read this.
modulePath := "unknown module path"
moduleVersion := "unknown version"
if bi, ok := debug.ReadBuildInfo(); ok {
if bi.Main.Path != "" {
modulePath = bi.Main.Path
}
if bi.Main.Version != "" {
moduleVersion = bi.Main.Version
}
}
context := struct {
ExternalImports []string
PackageName string
ModuleName string
Version string
}{
ExternalImports: externalImports,
PackageName: packageName,
ModuleName: modulePath,
Version: moduleVersion,
}
return GenerateTemplates([]string{"imports.tmpl"}, t, context)
}
// Generate all the glue code which provides the API for interacting with
// additional properties and JSON-ification
func GenerateAdditionalPropertyBoilerplate(t *template.Template, typeDefs []TypeDefinition) (string, error) {
var filteredTypes []TypeDefinition
m := map[string]bool{}
for _, t := range typeDefs {
if found := m[t.TypeName]; found {
continue
}
m[t.TypeName] = true
if t.Schema.HasAdditionalProperties {
filteredTypes = append(filteredTypes, t)
}
}
context := struct {
Types []TypeDefinition
}{
Types: filteredTypes,
}
return GenerateTemplates([]string{"additional-properties.tmpl"}, t, context)
}
func GenerateUnionBoilerplate(t *template.Template, typeDefs []TypeDefinition) (string, error) {
var filteredTypes []TypeDefinition
for _, t := range typeDefs {
if len(t.Schema.UnionElements) != 0 {
filteredTypes = append(filteredTypes, t)
}
}
if len(filteredTypes) == 0 {
return "", nil
}
context := struct {
Types []TypeDefinition
}{
Types: filteredTypes,
}
return GenerateTemplates([]string{"union.tmpl"}, t, context)
}
// SanitizeCode runs sanitizers across the generated Go code to ensure the
// generated code will be able to compile.
func SanitizeCode(goCode string) string {
// remove any byte-order-marks which break Go-Code
// See: https://groups.google.com/forum/#!topic/golang-nuts/OToNIPdfkks
return strings.Replace(goCode, "\uFEFF", "", -1)
}
// LoadTemplates loads all of our template files into a text/template. The
// path of template is relative to the templates directory.
func LoadTemplates(src embed.FS, t *template.Template) error {
return fs.WalkDir(src, "templates", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("error walking directory %s: %w", path, err)
}
if d.IsDir() {
return nil
}
buf, err := src.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file '%s': %w", path, err)
}
templateName := strings.TrimPrefix(path, "templates/")
tmpl := t.New(templateName)
_, err = tmpl.Parse(string(buf))
if err != nil {
return fmt.Errorf("parsing template '%s': %w", path, err)
}
return nil
})
}