From b7945d05c4373576b8274069d3dc3ce9b39e46d1 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 3 Aug 2016 13:01:13 -0600 Subject: [PATCH] ref(templates): change GetString to Get This changes "pkg/chartutil".Files.Get to return a string, removes "pkg/chartutil".Files.GetString, and adds "pkg/chartutil".Files.GetBytes. Closes #1020 --- pkg/chartutil/files.go | 19 ++++++++++++------- pkg/chartutil/files_test.go | 4 ++-- pkg/engine/engine_test.go | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/chartutil/files.go b/pkg/chartutil/files.go index 45598de3a..89120a42f 100644 --- a/pkg/chartutil/files.go +++ b/pkg/chartutil/files.go @@ -32,11 +32,14 @@ func NewFiles(from []*any.Any) Files { return files } -// Get a file by path. +// GetBytes gets a file by path. +// +// The returned data is raw. In a template context, this is identical to calling +// {{index .Files $path}}. // // This is intended to be accessed from within a template, so a missed key returns // an empty []byte. -func (f Files) Get(name string) []byte { +func (f Files) GetBytes(name string) []byte { v, ok := f[name] if !ok { return []byte{} @@ -44,10 +47,12 @@ func (f Files) Get(name string) []byte { return v } -// GetString returns a string representation of the given file. +// Get returns a string representation of the given file. +// +// Fetch the contents of a file as a string. It is designed to be called in a +// template. // -// This is a convenience for the otherwise cumbersome template logic -// for '{{.Files.Get "foo" | printf "%s"}}'. -func (f Files) GetString(name string) string { - return string(f.Get(name)) +// {{.Files.Get "foo"}} +func (f Files) Get(name string) string { + return string(f.GetBytes(name)) } diff --git a/pkg/chartutil/files_test.go b/pkg/chartutil/files_test.go index 97eb4ee05..5e162f19d 100644 --- a/pkg/chartutil/files_test.go +++ b/pkg/chartutil/files_test.go @@ -43,10 +43,10 @@ func TestNewFiles(t *testing.T) { } for i, f := range cases { - if got := string(files.Get(f.path)); got != f.data { + if got := string(files.GetBytes(f.path)); got != f.data { t.Errorf("%d: expected %q, got %q", i, f.data, got) } - if got := files.GetString(f.path); got != f.data { + if got := files.Get(f.path); got != f.data { t.Errorf("%d: expected %q, got %q", i, f.data, got) } } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index f680d2053..ec19f8ded 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -312,7 +312,7 @@ func TestRenderBuiltinValues(t *testing.T) { Metadata: &chart.Metadata{Name: "Latium"}, Templates: []*chart.Template{ {Name: "Lavinia", Data: []byte(`{{.Template.Name}}{{.Chart.Name}}{{.Release.Name}}`)}, - {Name: "From", Data: []byte(`{{.Files.author | printf "%s"}} {{.Files.GetString "book/title.txt"}}`)}, + {Name: "From", Data: []byte(`{{.Files.author | printf "%s"}} {{.Files.Get "book/title.txt"}}`)}, }, Values: &chart.Config{Raw: ``}, Dependencies: []*chart.Chart{},