Skip to content

Commit

Permalink
feat: add template function for returning the path to the pack's dire…
Browse files Browse the repository at this point in the history
…ctory

This allows users to read the fileContents of files within the pack folder, making templating out larger files less inconvenient and allowing users to use syntax highting from their editors when working on said files.
  • Loading branch information
josegonzalez committed Aug 22, 2024
1 parent 3c0178a commit 2fa2ba7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## UNRELEASED

IMPROVEMENTS:
* template: Add template function for returning the path to the pack's directory [[GH-574](https://github.com/hashicorp/nomad-pack/pull/574)]

BUG FIXES:
* cli: Update references to `hello-world` pack in command help text to `hello_world` [[GH-523](https://github.com/hashicorp/nomad-pack/pull/523)]
* docker: Added tzdata to container to fix panics running packs containing periodic jobs with timezones [[GH-564](https://github.com/hashicorp/nomad-pack/pull/564)]
Expand Down
3 changes: 2 additions & 1 deletion docs/writing-packs.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ Custom Nomad-specific and debugging functions are also provided:
- `nomadNamespace` takes a single string parameter of a namespace ID which will be read via `/v1/namespace/:namespace`.
- `spewDump` dumps the entirety of the passed object as a string. The output includes the content types and values. This uses the `spew.SDump` function.
- `spewPrintf` dumps the supplied arguments into a string according to the supplied format. This utilises the `spew.Printf` function.
- `fileContents` takes an argument to a file of the local host, reads its contents and provides this as a string.
- `fileContents` takes an argument to a file on the local host relative to the directory `nomad-pack` is invoked in, reads its contents and provides the output as a string.
- `packPath` returns the path to the pack being invoked.

A custom function within a template is called like any other:

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (pm *PackManager) ProcessTemplates(renderAux bool, format bool, ignoreMissi

r := new(renderer.Renderer)
r.Client = pm.client
r.PackPath = pm.cfg.Path
pm.renderer = r

// should auxiliary files be rendered as well?
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/renderer/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func funcMap(r *Renderer) template.FuncMap {
f["nomadRegions"] = nomadRegions(r.Client)
}

if r != nil && r.PackPath != "" {
f["packPath"] = func(file string) (string, error) {
return r.PackPath, nil
}
}

// Add additional custom functions.
f["fileContents"] = fileContents
f["toStringList"] = toStringList
Expand Down

0 comments on commit 2fa2ba7

Please sign in to comment.