Debian Codegraph @

kyaml/fn/framework/parser/template.go

raw symbols SCIP
1 // Copyright 2021 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package parser
5
6 import (
7 "fmt"
8 iofs "io/fs"
9 "os"
10 "path/filepath"
11 "text/template"
12
13 "sigs.k8s.io/kustomize/kyaml/fn/framework"
14 )
15
16 const (
17 // TemplateExtension is the file extension this package requires template files to have
18 TemplateExtension = ".template.yaml"
19 )
20
21 // TemplateStrings returns a TemplateParser that will parse the templates from the given strings.
22 //
23 // This is a helper for use with framework.TemplateProcessor's template subfields. Example:
24 //
25 // processor := framework.TemplateProcessor{
26 // ResourceTemplates: []framework.ResourceTemplate{{
27 // Templates: parser.TemplateStrings(`
28 // apiVersion: apps/v1
29 // kind: Deployment
30 // metadata:
31 // name: foo
32 // namespace: default
33 // annotations:
34 // {{ .Key }}: {{ .Value }}
35 // `)
36 // }},
37 // }
38 func TemplateStrings(data ...string) framework.TemplateParser {
39 return framework.TemplateParserFunc(func() ([]*template.Template, error) {
40 var templates []*template.Template
41 for i := range data {
42 t, err := template.New(fmt.Sprintf("inline%d", i)).Parse(data[i])
43 if err != nil {
44 return nil, err
45 }
46 templates = append(templates, t)
47 }
48 return templates, nil
49 })
50 }
51
52 // TemplateFiles returns a TemplateParser that will parse the templates from the given files or directories.
53 // Only immediate children of any given directories will be parsed.
54 // All files must end in .template.yaml.
55 //
56 // This is a helper for use with framework.TemplateProcessor's template subfields. Example:
57 //
58 // processor := framework.TemplateProcessor{
59 // ResourceTemplates: []framework.ResourceTemplate{{
60 // Templates: parser.TemplateFiles("path/to/templates", "path/to/special.template.yaml")
61 // }},
62 // }
63 func TemplateFiles(paths ...string) TemplateParser {
64 return TemplateParser{parser{paths: paths, extensions: []string{TemplateExtension}}}
65 }
66
67 // TemplateParser is a framework.TemplateParser that can parse files or directories containing Go templated YAML.
68 type TemplateParser struct {
69 parser
70 }
71
72 // Parse implements framework.TemplateParser
73 func (l TemplateParser) Parse() ([]*template.Template, error) {
74 if l.fs == nil {
75 l.fs = os.DirFS(".")
76 }
77
78 var templates []*template.Template
79 err := l.parse(func(content []byte, file string) error {
80 t, err := template.New(filepath.Base(file)).Parse(string(content))
81 if err == nil {
82 templates = append(templates, t)
83 }
84 return err
85 })
86 if err != nil {
87 return nil, err
88 }
89 return templates, nil
90 }
91
92 // FromFS allows you to replace the filesystem in which the parser will look up the given paths.
93 // For example, you can use an embed.FS.
94 func (l TemplateParser) FromFS(fs iofs.FS) TemplateParser {
95 l.parser.fs = fs
96 return l
97 }
98
99 // WithExtensions allows you to replace the extension the parser accept on the input files.
100 func (l TemplateParser) WithExtensions(ext ...string) TemplateParser {
101 l.parser.extensions = ext
102 return l
103 }