Render json, yaml, toml & xml with go templates from the command line.
The templates are executed with the text/template package. This means they come with the additional risks and benefits the text templates provide.
Options:
-f, --file stringArray template file path. Can be specified multiple times
-g, --glob stringArray template file glob. Can be specified multiple times
-n, --name string if specified, execute the template with the given name
--options stringArray options to pass to the template engine
-d, --decoder string decoder to use for input data. Supported values: json, yaml, toml, xml (default "json")
--no-newline do not print newline at the end of the output
-h, --help show the help text
-v, --version show the version
--debug enable debug mode
The input data is read from stdin via pipe or redirection. It is actually not required to provide any input data. If no input data is provided, the template is executed with nil data.
# Redirection
tpl '{{ . }}' < path/to/input.json
# Pipe
curl localhost | tpl '{{ . }}'
# nil data
tpl '{{ . }}'
The root templates name is _tpl.root
and positional arguments are parsed into this root template. That means while its possible to specify multiple arguments, they will overwrite each other unless they use the define
keyword to define a named template that can be referenced later when executing the template. If a named templates is specified multiple times, the last one will override the previous ones.
Templates from the flags --file and --glob are parsed in the order they are specified. So the override rules of the text/template package apply. If a file with the same name is specified multiple times, the last one wins. Even if they are in different directories.
The behavior of the cli tries to stay consistent with the actual behavior of the go template engine.
By default the root template is executed if at least one positional argument has been provided. Otherwise the first parsed file name is used to to determine which named template to execute since the root templates body is empty. It is always possible to choose another template to execute by using the --name flag.
tpl '{{ . }}' --file foo.tpl --glob templates/*.tpl
The ability to parse multiple templates makes sense when defining helper snippets and other named templates to reference using the builtin template
keyword or the custom include
function which can be used in pipelines.
By default input data is decoded as json and passed to the template to execute. It is possible to use an alternative decoder. The supported decoders are:
- json
- yaml
- toml
- xml
While json could technically be decoded using the yaml decoder, this is not done by default for performance reasons.
Next to the builtin functions, sSprig functions](http://masterminds.github.io/sprig/) and treasure-map functions are available.
Download the binary from the release page. For example
curl -fsSLO https://github.com/bluebrown/go-template-cli/releases/download/v0.1.0/tpl-amd64-static.tar.gz
tar -xzf tpl-amd64-static.tar.gz
mv tpl-0.1.0-amd64-static /usr/local/bin/tpl
If you have go installed, you can use the go install
command to install the binary.
go install github.com/bluebrown/go-template-cli/cmd/tpl@latest
The binary is also available as a docker image.
curl -s curl -s https://jsonplaceholder.typicode.com/todos | docker run -i bluebrown/tpl '{{ table . }}'
Clone the repo and use the makefile to build the binary. The make install command will move the binary to /usr/local/bin.
git clone https://github.com/bluebrown/go-template-cli
cd tpl && make install
Review the examples directory, for more examples.
curl -s https://jsonplaceholder.typicode.com/users | tpl '<table>
<caption>My Address Nook</caption>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
{{- range . }}
<tr>
<th>{{ .name }}</th>
<td>{{ .email }}</td>
<td>{{ .phone }}</td>
<td>
<ul>
{{- range $key, $val := .address }} {{ if ne $key "geo" }}
<li><strong>{{ $key }}:</strong> {{ $val }}</li>
{{- end -}}
{{ end }}
</ul>
</td>
</tr>
{{- end -}}
</table>'
Output
Name | Phone | Address | |
---|---|---|---|
Leanne Graham | Sincere@april.biz | 1-770-736-8031 x56442 |
|
Ervin Howell | Shanna@melissa.tv | 010-692-6593 x09125 |
|
Clementine Bauch | Nathan@yesenia.net | 1-463-123-4447 |
|
Patricia Lebsack | Julianne.OConner@kory.org | 493-170-9623 x156 |
|
Chelsey Dietrich | Lucio_Hettinger@annie.ca | (254)954-1289 |
|
Mrs. Dennis Schulist | Karley_Dach@jasper.info | 1-477-935-8478 x6430 |
|
Kurtis Weissnat | Telly.Hoeger@billy.biz | 210.067.6132 |
|
Nicholas Runolfsdottir V | Sherwood@rosamond.me | 586.493.6943 x140 |
|
Glenna Reichert | Chaim_McDermott@dana.io | (775)976-6794 x41206 |
|
Clementina DuBuque | Rey.Padberg@karina.biz | 024-648-3804 |
|