Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
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
35 changes: 35 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,38 @@ Render:
```
QUEUE-NAME
```

#### varExists

Tests for the existence of a variable in the variables file and returns true
if it exists, otherwise false. The first argument should be a map containing
the variable in question, the second is a string containing the name of the
variable to test for.

Example:
```
[[ if varExists . "constraint" ]]
constraint {
attribute = "${[[ .constraint.attribute ]]}"
value = "[[ .constraint.value ]]"
}
[[ end ]]
```

Given a vars yaml file containing:
```
---
constraint:
attribute: meta.foo
value: bar
```

Render:
```
constraint {
attribute = "${meta.foo}"
value = "bar"
}
```

If `constraint` is missing the constraint stanza will not be rendered.
11 changes: 11 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ func funcMap(consulClient *consul.Client) template.FuncMap {
"timeNowTimezone": timeNowTimezoneFunc(),
"toLower": toLower,
"toUpper": toUpper,
"varExists": varExists,
}
}

func varExists(i interface{}, name string) (ok bool, err error) {
switch kv := i.(type) {
case map[string]interface{}:
_, ok = kv[name]
default:
err = errors.New("varExists cannot be used on non-maps")
}
return
}

func consulKeyFunc(consulClient *consul.Client) func(string) (string, error) {
return func(s string) (string, error) {

Expand Down