From 1df13df88d80e7e90517d48918489050e24bb5d4 Mon Sep 17 00:00:00 2001 From: Andrew Stuart Date: Mon, 12 Dec 2016 18:53:32 -0700 Subject: [PATCH] Include lines functions --- pkg/chartutil/files.go | 10 ++++++++-- pkg/chartutil/files_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/chartutil/files.go b/pkg/chartutil/files.go index 47b3bc80e..b10842bec 100644 --- a/pkg/chartutil/files.go +++ b/pkg/chartutil/files.go @@ -147,8 +147,14 @@ func (f Files) AsSecrets() string { return ToYaml(m) } -// Lines returns -func (f Files) Lines(string path) []string { +// Lines returns each line of a named file (split by "\n") as a slice, so it can +// be ranged over in your templates. +// +// This is designed to be called from a template. +// +// {{ range .Files.Lines "foo/bar.html" }} +// {{ . }}{{ end }} +func (f Files) Lines(path string) []string { if f == nil || f[path] == nil { return []string{} } diff --git a/pkg/chartutil/files_test.go b/pkg/chartutil/files_test.go index afc8214b2..6e4dd3a57 100644 --- a/pkg/chartutil/files_test.go +++ b/pkg/chartutil/files_test.go @@ -29,6 +29,7 @@ var cases = []struct { {"ship/stowaway.txt", "Legatt"}, {"story/name.txt", "The Secret Sharer"}, {"story/author.txt", "Joseph Conrad"}, + {"multiline/test.txt", "bar\nfoo"}, } func getTestFiles() []*any.Any { @@ -86,6 +87,17 @@ func TestToSecret(t *testing.T) { as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0\n", out) } +func TestLines(t *testing.T) { + as := assert.New(t) + + f := NewFiles(getTestFiles()) + + out := f.Lines("multiline/test.txt") + as.Len(out, 2) + + as.Equal("bar", out[0]) +} + func TestToYaml(t *testing.T) { expect := "foo: bar\n" v := struct {