Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template function for returning the path to the pack's directory #574

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]

## 0.2.0 (October 16, 2024)

BREAKING CHANGES:
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() (string, error) {
return r.PackPath, nil
}
}

// Add additional custom functions.
f["fileContents"] = fileContents
f["toStringList"] = toStringList
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Renderer struct {
// set to true error will be used, otherwise zero is used.
Strict bool

// PackPath is the path to the pack being rendered.
PackPath string

// Client is the Nomad API client used when running the Nomad template
// functions. It can potentially be nil, therefore care should be taken
// when accessing it.
Expand Down