Skip to content

Commit

Permalink
Add function to retrieve files
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Nov 9, 2023
1 parent 7d75198 commit dacd347
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The task expects a glob pattern pointing to one or more Go template files (given
* `data`. Parses a `.y(a)ml`, `.json` or `.xml` file into a map. Example: `{{$metadata := data "metadata.yaml"}}`
* `content`. Reads the content of the given file. Example: `{{content ".ods/project"}}`
* `contents`. Reads the content of all files matching the given glob pattern. Example: `{{$ods := contents ".ods/*"}}`
* `files`. Returns a slice of files at given path. Example: `{{$images := data ".ods/artifacts/image-digests/*"}}`
* `directories`. Returns a slice of directories at given path. Example: `{{$repos := data ".ods/repos"}}`
* `exists`. Checks whether given path exists. Example: `{{if exists ".ods/artifacts/foo/bar.json"}}artifact exists{{end}}`
* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps. Example: `{{$manifest := fromMultiYAML $helm_status.manifest}}`
Expand Down
18 changes: 18 additions & 0 deletions cmd/render/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func templateFuncs(baseDir string) template.FuncMap {
"data": makeDataFunc(baseDir),
"content": makeContentFunc(baseDir),
"contents": makeContentsFunc(baseDir),
"files": makeFilesFunc(baseDir),
"directories": makeDirectoriesFunc(baseDir),
"exists": makeExistsFunc(baseDir),
"fromMultiYAML": fromMultiYAML,
Expand Down Expand Up @@ -119,6 +120,23 @@ func makeContentsFunc(baseDir string) func(glob string) (map[string]any, error)
}
}

// makeFilesFunc returns a function that returns the files found at given path.
func makeFilesFunc(baseDir string) func(filename string) ([]string, error) {
return func(path string) ([]string, error) {
entries, err := os.ReadDir(filepath.Join(baseDir, path))
if err != nil {
return nil, err
}
var dirs []string
for _, e := range entries {
if !e.IsDir() {
dirs = append(dirs, e.Name())
}
}
return dirs, nil
}
}

// makeDirectoriesFunc returns a function that returns the directories found at given path.
func makeDirectoriesFunc(baseDir string) func(filename string) ([]string, error) {
return func(path string) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The task expects a glob pattern pointing to one or more Go template files (given
* `data`. Parses a `.y(a)ml`, `.json` or `.xml` file into a map. Example: `{{$metadata := data "metadata.yaml"}}`
* `content`. Reads the content of the given file. Example: `{{content ".ods/project"}}`
* `contents`. Reads the content of all files matching the given glob pattern. Example: `{{$ods := contents ".ods/*"}}`
* `files`. Returns a slice of files at given path. Example: `{{$images := data ".ods/artifacts/image-digests/*"}}`
* `directories`. Returns a slice of directories at given path. Example: `{{$repos := data ".ods/repos"}}`
* `exists`. Checks whether given path exists. Example: `{{if exists ".ods/artifacts/foo/bar.json"}}artifact exists{{end}}`
* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps. Example: `{{$manifest := fromMultiYAML $helm_status.manifest}}`
Expand Down

0 comments on commit dacd347

Please sign in to comment.