Include lines functions

pull/1666/head
Andrew Stuart 8 years ago
parent 6760aa1588
commit 1df13df88d
No known key found for this signature in database
GPG Key ID: D409317C5B5ACD4D

@ -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{}
}

@ -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 {

Loading…
Cancel
Save