Skip to content

Commit

Permalink
Update to add new syntax for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Jun 2, 2023
1 parent be23faf commit 505b5b2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 47 deletions.
75 changes: 53 additions & 22 deletions src/pages/docs/resources/functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -237,44 +237,75 @@ resource "template" "mine" {
}
```

## random_number
## template_file

Generates a random number within the given range.
Returns the rendered contents of a template file at the given path with the given input variables.

Templates can leverage the Handlebars templating language, more details on Handlebars
can be found at the following link:

[https://handlebarsjs.com/](https://handlebarsjs.com/)

### Parameters

<Properties>
<Property name="min" type="int" required="true" value="">
Minimum value for the random number.
<Property name="path" type="string" required="true" value="">
Name of the data folder to create, or path to return.
</Property>

<Property name="max" type="int" required="true" value="">
Maximum value for the random number.
<Property name="variables" type="map[string]interface" required="true" value="">
Name of the data folder to create, or path to return.
</Property>
</Properties>

```hcl
resource "template" "mine" {
source = "./mysource.txt"
destination = "./file.txt"
#given a file "./mytemplate.tmpl" with the contents "hello {{name}}"
variables = {
port = random_number(20000,30000)
}
mytype "test" {
// my_file = "foobar"
my_file = template_file("./mytemplate.tmpl", {
name = "world"
})
}
```

## random_creature
### Template Helpers

Generates a random string in the form of a mythical creature.
The template_file function provides helpers that can be used inside your
templates as shown in the example below.

```hcl
resource "template" "mine" {
source = "./mysource.txt"
destination = "./file.txt"
```javascript
resource "template" "consul_config" {

variables = {
name = random_creature() // Lively-Imp, Mellifluous-Unicorn
}
source = <<-EOF

file_content = "{{ file "./myfile.txt" }}"
quote = {{quote something}}
trim = {{quote (trim with_whitespace)}}

EOF

destination = "./consul_config/consul.hcl"
}
```

#### quote [string]

Returns the original string wrapped in quotations, quote can be used with
the Go template pipe modifier.

```go
// given the string abc

quote "abc" // would return the value "abc"
```

#### trim [string]

Removes whitespace such as carrige returns and spaces from the begining and
the end of the string, can be used with the Go template pipe modifier.

```go
// given the string abc

trim " abc " // would return the value "abc"
```
58 changes: 56 additions & 2 deletions src/pages/docs/resources/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MetaProperties from "./shared/meta.mdx"

<Intro>
The `output` resource allows you to define output variables which can be used
with the jumppad `output` and `env` commands.
to return values from modules or with the jumppad `output` and `env` commands.

<b>Note:</b> `output` resources are module scoped, `jumppad output` will only show
the value of `output` resources scoped at the top level.
Expand All @@ -13,7 +13,7 @@ the value of `output` resources scoped at the top level.
## Properties

<Properties>
<Property name="value" type="string" required="true" value="">
<Property name="value" type="interface" required="true" value="">
Value to set to the output, if this value contains an interpolated
property from another resource, the output will be created after the
referenced resource.
Expand All @@ -24,11 +24,65 @@ the value of `output` resources scoped at the top level.

## Examples

### Simple Example

The following example shows how to use an `output` resource to configure the
environment variable `KUBECONFIG`.

```hcl
output "KUBECONFIG" {
value = resource.k8s_cluster.k3s.kubeconfig
}
```

### Lists of Values

The following output sets a list of numbers

```hcl
output "list" {
value = [1,2,3]
}
```

This can be consumed using the following interpolation.

Note: Indexes for lists are `0` based.

```hcl
output "list_value" {
value = output.list.2 // 3
}
```

### Maps of Values

The following output sets a map of values

```hcl
output "map" {
value = {
list = [1,2,3]
string = "hello world"
submap = {
foo = "bar"
}
}
}
```

This can be consumed using the following interpolation.

```hcl
output "map_value_1" {
value = output.map.list.2 // 3
}
output "map_value_2" {
value = output.map.string // hello world
}
output "map_value_3" {
value = output.map.submap.foo // bar
}
```
34 changes: 11 additions & 23 deletions src/pages/docs/resources/template.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import MetaProperties from "./shared/meta.mdx"
# Template `template`

<Intro>
The Remove Exec resource allows the execution of arbitrary commands and scripts.
Execution can either be in a stand alone container or can target an existing
and running container.
The Template resource allows the processing of templates, outputing
the result as a file.

Documentation is work in progress, please see the old documentation at:

https://shipyard.run/docs/resources/exec_remote
Templating uses the Handlebars language which is Mustache template language can be found at the following
location:
[Mustache templating language details](https://mustache.github.io/mustache.5.html)
</Intro>

## Properties
Expand Down Expand Up @@ -49,7 +48,7 @@ https://shipyard.run/docs/resources/exec_remote
following convention.

```go
data_dir = "#{{ .Vars.data_dir }}"
data_dir = "{{data_dir}}"
```
</Property>
</Properties>
Expand All @@ -67,7 +66,7 @@ stanza using HereDoc syntax.
resource "template" "consul_config" {
source = <<-EOF
data_dir = "#{{ .Vars.data_dir }}"
data_dir = "{{data_dir}}"
log_level = "DEBUG"
datacenter = "dc1"
Expand Down Expand Up @@ -147,7 +146,7 @@ container "consul" {
depends_on = ["template.consul_config"]
image {
name = "consul:${var.consul_version}"
name = "consul:${variable.consul_version}"
}
command = ["consul", "agent", "-config-file=/config/consul.hcl"]
Expand Down Expand Up @@ -188,27 +187,16 @@ resource "template" "consul_config" {
source = <<-EOF
file_content = "#{{ file "./myfile.txt" }}"
quote = #{{ .Var.something | quote }}
trim = #{{ .Var.with_whitespace | trim | quote }}
file_content = "{{ file "./myfile.txt" }}"
quote = {{quote something}}
trim = {{quote (trim with_whitespace)}}
EOF
destination = "./consul_config/consul.hcl"
}
```

### file [path]

Reads the contents of a file from the given path


```go
# given a file ./myfile.txt with the contents "foo bar"

file "./myfile.txt" // would return "foo bar"
```

### quote [string]

Returns the original string wrapped in quotations, quote can be used with
Expand Down

0 comments on commit 505b5b2

Please sign in to comment.