Commit b7945d05 authored by Matt Butcher's avatar Matt Butcher

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
parent 2c3d45d1
...@@ -32,11 +32,14 @@ func NewFiles(from []*any.Any) Files { ...@@ -32,11 +32,14 @@ func NewFiles(from []*any.Any) Files {
return 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 // This is intended to be accessed from within a template, so a missed key returns
// an empty []byte. // an empty []byte.
func (f Files) Get(name string) []byte { func (f Files) GetBytes(name string) []byte {
v, ok := f[name] v, ok := f[name]
if !ok { if !ok {
return []byte{} return []byte{}
...@@ -44,10 +47,12 @@ func (f Files) Get(name string) []byte { ...@@ -44,10 +47,12 @@ func (f Files) Get(name string) []byte {
return v 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 // {{.Files.Get "foo"}}
// for '{{.Files.Get "foo" | printf "%s"}}'. func (f Files) Get(name string) string {
func (f Files) GetString(name string) string { return string(f.GetBytes(name))
return string(f.Get(name))
} }
...@@ -43,10 +43,10 @@ func TestNewFiles(t *testing.T) { ...@@ -43,10 +43,10 @@ func TestNewFiles(t *testing.T) {
} }
for i, f := range cases { 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) 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) t.Errorf("%d: expected %q, got %q", i, f.data, got)
} }
} }
......
...@@ -312,7 +312,7 @@ func TestRenderBuiltinValues(t *testing.T) { ...@@ -312,7 +312,7 @@ func TestRenderBuiltinValues(t *testing.T) {
Metadata: &chart.Metadata{Name: "Latium"}, Metadata: &chart.Metadata{Name: "Latium"},
Templates: []*chart.Template{ Templates: []*chart.Template{
{Name: "Lavinia", Data: []byte(`{{.Template.Name}}{{.Chart.Name}}{{.Release.Name}}`)}, {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: ``}, Values: &chart.Config{Raw: ``},
Dependencies: []*chart.Chart{}, Dependencies: []*chart.Chart{},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment