Skip to content

render json, yaml & toml with go templates, from the command line

License

Notifications You must be signed in to change notification settings

bluebrown/go-template-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Template CLI (tpl)

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.

Synopsis

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

Input Data

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 '{{ . }}'

Templates

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.

Decoders

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.

Functions

Next to the builtin functions, sSprig functions](http://masterminds.github.io/sprig/) and treasure-map functions are available.

Installation

Binary

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

Go

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

Docker

The binary is also available as a docker image.

curl -s curl -s https://jsonplaceholder.typicode.com/todos | docker run -i bluebrown/tpl '{{ table . }}'

From source

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

Example

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> &nbsp; {{ $val }}</li>
        {{- end -}}
        {{ end }}
      </ul>
    </td>
  </tr>
  {{- end -}}
</table>'
Output My Address Nook
Name Email Phone Address
Leanne Graham Sincere@april.biz 1-770-736-8031 x56442
  • city:   Gwenborough
  • street:   Kulas Light
  • suite:   Apt. 556
  • zipcode:   92998-3874
Ervin Howell Shanna@melissa.tv 010-692-6593 x09125
  • city:   Wisokyburgh
  • street:   Victor Plains
  • suite:   Suite 879
  • zipcode:   90566-7771
Clementine Bauch Nathan@yesenia.net 1-463-123-4447
  • city:   McKenziehaven
  • street:   Douglas Extension
  • suite:   Suite 847
  • zipcode:   59590-4157
Patricia Lebsack Julianne.OConner@kory.org 493-170-9623 x156
  • city:   South Elvis
  • street:   Hoeger Mall
  • suite:   Apt. 692
  • zipcode:   53919-4257
Chelsey Dietrich Lucio_Hettinger@annie.ca (254)954-1289
  • city:   Roscoeview
  • street:   Skiles Walks
  • suite:   Suite 351
  • zipcode:   33263
Mrs. Dennis Schulist Karley_Dach@jasper.info 1-477-935-8478 x6430
  • city:   South Christy
  • street:   Norberto Crossing
  • suite:   Apt. 950
  • zipcode:   23505-1337
Kurtis Weissnat Telly.Hoeger@billy.biz 210.067.6132
  • city:   Howemouth
  • street:   Rex Trail
  • suite:   Suite 280
  • zipcode:   58804-1099
Nicholas Runolfsdottir V Sherwood@rosamond.me 586.493.6943 x140
  • city:   Aliyaview
  • street:   Ellsworth Summit
  • suite:   Suite 729
  • zipcode:   45169
Glenna Reichert Chaim_McDermott@dana.io (775)976-6794 x41206
  • city:   Bartholomebury
  • street:   Dayna Park
  • suite:   Suite 449
  • zipcode:   76495-3109
Clementina DuBuque Rey.Padberg@karina.biz 024-648-3804
  • city:   Lebsackbury
  • street:   Kattie Turnpike
  • suite:   Suite 198
  • zipcode:   31428-2261