From 753de35ee0bf7f86e380f015c135264b5bcd44a7 Mon Sep 17 00:00:00 2001 From: Nils <9465658+nlueb@users.noreply.github.com> Date: Mon, 10 Jan 2022 08:22:24 +0000 Subject: [PATCH] Add readDir as a templating function (#1934) Adds a new templating function called `readDir`. With `readDir` users can read the contents of a specified directory and get a list of all contained files. This is useful when reading a bunch of files from a directory. The following example shows a snippet of a values file for configuring a Logstash deployment. Using only `readFile`, a user must specify each file by hand and adjust this list as the number of files to be read grows. ```yaml logstash: configs: logstash.yml: | {{- tpl (readFile "config/logstash.yml.gotmpl") . | nindent 6 }} jvm.options: | {{- readFile "config/jvm.options" | nindent 6 }} pipelines.yml: | {{- readFile "config/pipelines.yml" | nindent 6 }} pipelines: beats-log4j.conf: | {{- readFile "config/pipelines/beats-log4j.conf" | nindent 6 }} nginx-access.conf: | {{- readFile "config/pipelines/nginx-access.conf" | nindent 6 }} nginx-error.conf: | {{- readFile "config/pipelines/nginx-error.conf" | nindent 6 }} syslog-logs.conf: | {{- readFile "config/pipelines/syslog-logs.conf" | nindent 6 }} tcp-logs.conf: | {{- readFile "config/pipelines/tcp-logs.conf" | nindent 6 }} udp-debug.conf: | {{- readFile "config/pipelines/udp-debug.conf" | nindent 6 }} udp-logs.conf: | {{- readFile "config/pipelines/udp-logs.conf" | nindent 6 }} certificates: ca.crt: | {{- readFile "config/certificates/ca.crt" | nindent 6 }} logstash.crt: | {{- readFile "config/certificates/logstash.crt" | nindent 6 }} logstash.key: | {{- readFile "config/certificates/logstash.key" | nindent 6 }} ``` With `readDir` the above snippet can be rewritten as follows: ```yaml logstash: configs: {{- range readDir "config" }} {{ base . }}: | {{- if hasSuffix "gotmpl" . }} {{- tpl (readFile .) $ | nindent 6 }} {{- else }} {{- readFile . | nindent 6 }} {{- end }} {{- end }} pipelines: {{- range readDir "config/pipelines" }} {{ base . }}: | {{- readFile . | nindent 6 }} {{- end }} certificates: {{- range readDir "config/certificates" }} {{ base . }}: | {{- readFile . | nindent 6 }} {{- end }} ``` --- pkg/tmpl/context_funcs.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/tmpl/context_funcs.go b/pkg/tmpl/context_funcs.go index 7e64aa5d..85e7e849 100644 --- a/pkg/tmpl/context_funcs.go +++ b/pkg/tmpl/context_funcs.go @@ -20,6 +20,7 @@ func (c *Context) createFuncMap() template.FuncMap { funcMap := template.FuncMap{ "exec": c.Exec, "readFile": c.ReadFile, + "readDir": ReadDir, "toYaml": ToYaml, "fromYaml": FromYaml, "setValueAtPath": SetValueAtPath, @@ -130,6 +131,21 @@ func (c *Context) ReadFile(filename string) (string, error) { return string(bytes), nil } +func ReadDir(path string) ([]string, error) { + entries, err := os.ReadDir(path) + if err != nil { + return nil, err + } + var filenames []string + for _, entry := range entries { + if entry.IsDir() { + continue + } + filenames = append(filenames, filepath.Join(path, entry.Name())) + } + return filenames, nil +} + func (c *Context) Tpl(text string, data interface{}) (string, error) { buf, err := c.RenderTemplateToBuffer(text, data) if err != nil {