From 1d70130ab92a4c9c57fd9b8831b58ffcc0c5123b Mon Sep 17 00:00:00 2001 From: Nils <9465658+nlueb@users.noreply.github.com> Date: Sun, 23 Jan 2022 03:20:56 +0000 Subject: [PATCH] Fix ReadDir templating function to respect base path (#2058) Ref https://github.com/roboll/helmfile/pull/1934#issuecomment-1018663764 --- pkg/tmpl/context_funcs.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/tmpl/context_funcs.go b/pkg/tmpl/context_funcs.go index 85e7e849..05c40bab 100644 --- a/pkg/tmpl/context_funcs.go +++ b/pkg/tmpl/context_funcs.go @@ -2,9 +2,6 @@ package tmpl import ( "fmt" - "github.com/ghodss/yaml" - "github.com/roboll/helmfile/pkg/helmexec" - "golang.org/x/sync/errgroup" "io" "os" "os/exec" @@ -12,6 +9,10 @@ import ( "reflect" "strings" "text/template" + + "github.com/ghodss/yaml" + "github.com/roboll/helmfile/pkg/helmexec" + "golang.org/x/sync/errgroup" ) type Values = map[string]interface{} @@ -20,7 +21,7 @@ func (c *Context) createFuncMap() template.FuncMap { funcMap := template.FuncMap{ "exec": c.Exec, "readFile": c.ReadFile, - "readDir": ReadDir, + "readDir": c.ReadDir, "toYaml": ToYaml, "fromYaml": FromYaml, "setValueAtPath": SetValueAtPath, @@ -131,11 +132,16 @@ func (c *Context) ReadFile(filename string) (string, error) { return string(bytes), nil } -func ReadDir(path string) ([]string, error) { +func (c *Context) ReadDir(path string) ([]string, error) { + if !filepath.IsAbs(path) { + path = filepath.Join(c.basePath, path) + } + entries, err := os.ReadDir(path) if err != nil { - return nil, err + return nil, fmt.Errorf("ReadDir %q: %w", path, err) } + var filenames []string for _, entry := range entries { if entry.IsDir() { @@ -143,6 +149,7 @@ func ReadDir(path string) ([]string, error) { } filenames = append(filenames, filepath.Join(path, entry.Name())) } + return filenames, nil }