Skip to content

Commit 2fa2ba7

Browse files
committed
feat: add template function for returning the path to the pack's directory
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.
1 parent 3c0178a commit 2fa2ba7

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## UNRELEASED
22

3+
IMPROVEMENTS:
4+
* template: Add template function for returning the path to the pack's directory [[GH-574](https://github.com/hashicorp/nomad-pack/pull/574)]
5+
36
BUG FIXES:
47
* cli: Update references to `hello-world` pack in command help text to `hello_world` [[GH-523](https://github.com/hashicorp/nomad-pack/pull/523)]
58
* 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)]

docs/writing-packs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ Custom Nomad-specific and debugging functions are also provided:
189189
- `nomadNamespace` takes a single string parameter of a namespace ID which will be read via `/v1/namespace/:namespace`.
190190
- `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.
191191
- `spewPrintf` dumps the supplied arguments into a string according to the supplied format. This utilises the `spew.Printf` function.
192-
- `fileContents` takes an argument to a file of the local host, reads its contents and provides this as a string.
192+
- `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.
193+
- `packPath` returns the path to the pack being invoked.
193194

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

internal/pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (pm *PackManager) ProcessTemplates(renderAux bool, format bool, ignoreMissi
118118

119119
r := new(renderer.Renderer)
120120
r.Client = pm.client
121+
r.PackPath = pm.cfg.Path
121122
pm.renderer = r
122123

123124
// should auxiliary files be rendered as well?

internal/pkg/renderer/funcs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func funcMap(r *Renderer) template.FuncMap {
5151
f["nomadRegions"] = nomadRegions(r.Client)
5252
}
5353

54+
if r != nil && r.PackPath != "" {
55+
f["packPath"] = func(file string) (string, error) {
56+
return r.PackPath, nil
57+
}
58+
}
59+
5460
// Add additional custom functions.
5561
f["fileContents"] = fileContents
5662
f["toStringList"] = toStringList

0 commit comments

Comments
 (0)