Skip to content

Commit cab5014

Browse files
committed
templates: deprecate NewParse()
It it just a chain of `New("sometag").Parse(...)`, and most of our uses don't use a tag for the template, so can call Parse. There's no public users of this function, but deprecating it first just in case. Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 7ab3e7e) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 7c3ec3e commit cab5014

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

cli/command/container/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func buildContainerListOptions(options *psOptions) (*container.ListOptions, erro
8787

8888
// always validate template when `--format` is used, for consistency
8989
if len(options.format) > 0 {
90-
tmpl, err := templates.NewParse("", options.format)
90+
tmpl, err := templates.Parse(options.format)
9191
if err != nil {
9292
return nil, errors.Wrap(err, "failed to parse template")
9393
}

cli/command/system/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func needsServerInfo(template string, info dockerInfo) bool {
168168
}
169169

170170
// A template is provided and has at least one field set.
171-
tmpl, err := templates.NewParse("", template)
171+
tmpl, err := templates.Parse(template)
172172
if err != nil {
173173
// ignore parsing errors here, and let regular code handle them
174174
return true

cli/command/system/version.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ func newVersionTemplate(templateFormat string) (*template.Template, error) {
215215
case formatter.JSONFormatKey:
216216
templateFormat = formatter.JSONFormat
217217
}
218-
tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
219-
tmpl, err := tmpl.Parse(templateFormat)
218+
tmpl, err := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}).Parse(templateFormat)
220219
if err != nil {
221220
return nil, errors.Wrap(err, "template parsing error")
222221
}

templates/templates.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var HeaderFunctions = template.FuncMap{
7171
// Parse creates a new anonymous template with the basic functions
7272
// and parses the given format.
7373
func Parse(format string) (*template.Template, error) {
74-
return NewParse("", format)
74+
return template.New("").Funcs(basicFunctions).Parse(format)
7575
}
7676

7777
// New creates a new empty template with the provided tag and built-in
@@ -82,8 +82,10 @@ func New(tag string) *template.Template {
8282

8383
// NewParse creates a new tagged template with the basic functions
8484
// and parses the given format.
85+
//
86+
// Deprecated: this function is unused and will be removed in the next release. Use [New] if you need to set a tag, or [Parse] instead.
8587
func NewParse(tag, format string) (*template.Template, error) {
86-
return New(tag).Parse(format)
88+
return template.New(tag).Funcs(basicFunctions).Parse(format)
8789
}
8890

8991
// padWithSpace adds whitespace to the input if the input is non-empty

templates/templates_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestParseStringFunctions(t *testing.T) {
3030
}
3131

3232
func TestNewParse(t *testing.T) {
33-
tm, err := NewParse("foo", "this is a {{ . }}")
33+
tm, err := New("foo").Parse("this is a {{ . }}")
3434
assert.NilError(t, err)
3535

3636
var b bytes.Buffer

0 commit comments

Comments
 (0)