tpl

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2022 License: 0BSD Imports: 8 Imported by: 0

README

tpl

Render yaml and json 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. Additionally, Sprig functions are available.

Usage

The input data is read from stdin via pipe or redirection.

# Redirection
tpl < path/to/input.json
# Pipe
curl localhost | tpl

The template is either read from the first positional argument or from a path specified via --template or -t flag.

# Positional argument
echo '{"place": "bar"}' | tpl 'lets go to the {{.place}}!'
# File
echo '{"place": "bar"}' | tpl --template path/to/template

Flags
-n
-no-newline
      do not print a new line at the end
-t string
-template string
      alternative way to specify template

Installation

Go

go install github.com/bluebrown/tpl
Docker
curl -s https://jsonplaceholder.typicode.com/users/1 | docker run -i bluebrown/tpl '{{.name}}'

Example

$ 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>' | less

The result looks like this

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

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL