Skip to content

Commit

Permalink
feat: add template function for reading files from the pack templates…
Browse files Browse the repository at this point in the history
… directory

When distributing a pack, it is common to wish to ship larger config files as part of a pack in separate files. This allows for better readability, as well as ensuring users can fallback on syntax highlighting from their editors when working on those files.
  • Loading branch information
josegonzalez committed Aug 22, 2024
1 parent 3c0178a commit 1abda7d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/writing-packs.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ The `datacenters` value shows slightly more complex Go Template, which allows fo

#### Template Functions

To supplement the standard Go Template set of template functions, the (masterminds/sprig)[https://github.com/Masterminds/sprig] library is used. This adds helpers for various use cases such as string manipulation, cryptographics, and data conversion (for instance to and from JSON).
To supplement the standard Go Template set of template functions, the [masterminds/sprig](https://github.com/Masterminds/sprig) library is used. This adds helpers for various use cases such as string manipulation, cryptographics, and data conversion (for instance to and from JSON).

Custom Nomad-specific and debugging functions are also provided:

Expand All @@ -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.
- `packFileContents` takes an argument to a file on the local host relative to the pack's `templates` directory, reads its contents and provides the output as a string.

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

Expand Down Expand Up @@ -249,7 +250,7 @@ dependency "demo_dep" {
}
```

The dependency name label *must* match the `name` property of the dependant pack, as specified in its `metadata.hcl`.
The dependency name label _must_ match the `name` property of the dependant pack, as specified in its `metadata.hcl`.

This would allow templates of "simple_service" to use "demo_dep"'s helper templates in the following way:

Expand Down Expand Up @@ -283,7 +284,7 @@ If you wish to share your packs, please consider adding them to the
If you don't want to host a pack registry in version control, you can work locally
using the filesystem method.

## Step Six: Deploy your Custom Pack!
## Step Six: Deploy your Custom Pack

Add your custom repository using the `nomad-pack registry add` command.

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
8 changes: 8 additions & 0 deletions internal/pkg/renderer/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package renderer
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"

Expand Down Expand Up @@ -51,6 +52,13 @@ func funcMap(r *Renderer) template.FuncMap {
f["nomadRegions"] = nomadRegions(r.Client)
}

if r != nil && r.PackPath != "" {
f["packFileContents"] = func(file string) (string, error) {
file = filepath.Join(r.PackPath, "templates", file)
return fileContents(file)
}
}

// 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

// Config is the configuration used when running Nomad Pack
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

0 comments on commit 1abda7d

Please sign in to comment.