-
Notifications
You must be signed in to change notification settings - Fork 18
/
template.go
120 lines (97 loc) · 3.16 KB
/
template.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
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package env
import (
"embed"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
sprigv3 "github.com/Masterminds/sprig/v3"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"github.com/gardener/gardenctl-v2/internal/util"
)
//go:embed templates
var fsys embed.FS
//go:generate mockgen -destination=./mocks/mock_template.go -package=mocks github.com/gardener/gardenctl-v2/pkg/env Template
// Template provides an abstraction for go templates.
type Template interface {
// ParseFiles parses the named files and associates the resulting templates with t.
ParseFiles(filenames ...string) error
// ExecuteTemplate applies the template associated with t for the given shell
// to the specified data object and writes the output to wr.
ExecuteTemplate(wr io.Writer, shell string, data interface{}) error
}
type templateImpl struct {
delegate *template.Template
}
var _ Template = &templateImpl{}
// NewTemplate creates a new Template instance with the given filenames.
func NewTemplate(filenames ...string) Template {
return newTemplateImpl(filenames...)
}
func newTemplateImpl(filenames ...string) *templateImpl {
t := &templateImpl{
delegate: template.
New("base").
Funcs(sprigv3.TxtFuncMap()).
Funcs(template.FuncMap{"shellEscape": util.ShellEscape}),
}
if len(filenames) > 0 {
utilruntime.Must(t.ParseFiles(filenames...))
}
return t
}
// ParseFiles parses the file located at path and associates the resulting templates with t.
func (t *templateImpl) ParseFiles(filenames ...string) error {
for _, filename := range filenames {
if err := parseFile(fsys, t.delegate, filename); err != nil {
return err
}
}
return nil
}
// ExecuteTemplate applies the template associated with t for the given shell
// to the specified data object and writes the output to wr.
func (t *templateImpl) ExecuteTemplate(wr io.Writer, shell string, data interface{}) error {
return t.delegate.ExecuteTemplate(wr, shell, data)
}
// Delegate returns the wrapped go template.
func (t *templateImpl) Delegate() *template.Template {
return t.delegate
}
func parseFile(fsys fs.FS, t *template.Template, filename string) error {
name := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
// For embed.FS the path separator in is a forward slash, even on Windows systems.
// see https://pkg.go.dev/embed#hdr-Directives
embedFilename := "templates/" + name + ".tmpl"
embedExist := false
if _, err := fs.Stat(fsys, embedFilename); err == nil {
embedExist = true
_, err := t.ParseFS(fsys, embedFilename)
if err != nil {
return fmt.Errorf("parsing embedded template %q failed: %w", name, err)
}
}
if filepath.IsAbs(filename) {
_, err := t.ParseFiles(filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if !embedExist {
return fmt.Errorf("template %q does not exist: %w", name, err)
}
} else {
return fmt.Errorf("parsing template %q failed: %w", name, err)
}
}
} else if !embedExist {
return fmt.Errorf("embedded template %q does not exist", name)
}
return nil
}