From 6760aa15887b404305169fe1472de214a5a82b35 Mon Sep 17 00:00:00 2001 From: Andrew Stuart Date: Mon, 12 Dec 2016 17:19:47 -0700 Subject: [PATCH] Add Lines method, remove path utils (Sprig pr) --- docs/chart_template_guide/accessing_files.md | 12 ++++++++++++ pkg/chartutil/files.go | 10 ++++++++++ pkg/engine/engine.go | 5 ----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/chart_template_guide/accessing_files.md b/docs/chart_template_guide/accessing_files.md index d2ee57ffa..d19bbf5da 100644 --- a/docs/chart_template_guide/accessing_files.md +++ b/docs/chart_template_guide/accessing_files.md @@ -18,6 +18,7 @@ Helm provides access to files through the `.Files` object. Before we get going w - [Glob patterns](#glob-patterns) - [ConfigMap and Secrets utility functions](#configmap-and-secrets-utility-functions) - [Secrets](#secrets) +- [Lines](#lines) @@ -190,6 +191,17 @@ data: bWVzc2FnZSA9IEhlbGxvIGZyb20gY29uZmlnIDEK ``` +## Lines + +Sometimes it is desireable to access each line of a file in your template. We +provide a convenient `Lines` method for this. + +```yaml +data: + some-file.txt: {{ range .Files.Lines "foo/bar.txt" }} + {{ . }}{{ end }} +``` + Currently, there is no way to pass files external to the chart during `helm install`. So if you are asking users to supply data, it must be loaded using `helm install -f` or `helm install --set`. This discussion wraps up our dive into the tools and techniques for writing Helm templates. In the next section we will see how you can use one special file, `templates/NOTES.txt`, to send post-installation instructions to the users of your chart. diff --git a/pkg/chartutil/files.go b/pkg/chartutil/files.go index 8613044c5..47b3bc80e 100644 --- a/pkg/chartutil/files.go +++ b/pkg/chartutil/files.go @@ -18,6 +18,7 @@ package chartutil import ( "encoding/base64" "path" + "strings" yaml "gopkg.in/yaml.v2" @@ -146,6 +147,15 @@ func (f Files) AsSecrets() string { return ToYaml(m) } +// Lines returns +func (f Files) Lines(string path) []string { + if f == nil || f[path] == nil { + return []string{} + } + + return strings.Split(string(f[path]), "\n") +} + // ToYaml takes an interface, marshals it to yaml, and returns a string. It will // always return a string, even on marshal error (empty string). // diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 16be8451d..42db58c63 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -71,11 +71,6 @@ func FuncMap() template.FuncMap { // Add some extra functionality extra := template.FuncMap{ "toYaml": chartutil.ToYaml, - "base": path.Base, - "dir": path.Dir, - "ext": path.Ext, - "isAbs": path.IsAbs, - "clean": path.Clean, // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the