Fix ReadDir templating function to respect base path (#2058)

Ref https://github.com/roboll/helmfile/pull/1934#issuecomment-1018663764
This commit is contained in:
Nils 2022-01-23 03:20:56 +00:00 committed by GitHub
parent debd3c0eea
commit 1d70130ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 6 deletions

View File

@ -2,9 +2,6 @@ package tmpl
import ( import (
"fmt" "fmt"
"github.com/ghodss/yaml"
"github.com/roboll/helmfile/pkg/helmexec"
"golang.org/x/sync/errgroup"
"io" "io"
"os" "os"
"os/exec" "os/exec"
@ -12,6 +9,10 @@ import (
"reflect" "reflect"
"strings" "strings"
"text/template" "text/template"
"github.com/ghodss/yaml"
"github.com/roboll/helmfile/pkg/helmexec"
"golang.org/x/sync/errgroup"
) )
type Values = map[string]interface{} type Values = map[string]interface{}
@ -20,7 +21,7 @@ func (c *Context) createFuncMap() template.FuncMap {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"exec": c.Exec, "exec": c.Exec,
"readFile": c.ReadFile, "readFile": c.ReadFile,
"readDir": ReadDir, "readDir": c.ReadDir,
"toYaml": ToYaml, "toYaml": ToYaml,
"fromYaml": FromYaml, "fromYaml": FromYaml,
"setValueAtPath": SetValueAtPath, "setValueAtPath": SetValueAtPath,
@ -131,11 +132,16 @@ func (c *Context) ReadFile(filename string) (string, error) {
return string(bytes), nil 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) entries, err := os.ReadDir(path)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("ReadDir %q: %w", path, err)
} }
var filenames []string var filenames []string
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if entry.IsDir() {
@ -143,6 +149,7 @@ func ReadDir(path string) ([]string, error) {
} }
filenames = append(filenames, filepath.Join(path, entry.Name())) filenames = append(filenames, filepath.Join(path, entry.Name()))
} }
return filenames, nil return filenames, nil
} }