An open API service indexing awesome lists of open source software.

https://github.com/ivanvc/tpl

Simple gotemplate wrapper
https://github.com/ivanvc/tpl

gotemplate template templates

Last synced: over 1 year ago
JSON representation

Simple gotemplate wrapper

Awesome Lists containing this project

README

          

# tpl

`tpl` is a simple [Go template] wrapper. It enables using them in the command
line by using the `tpl` binary.

## Installation

1. Download the binary from the [releases page], or
2. Download with go
```bash
$ go install github.com/ivanvc/tpl@latest
```
3. Or use it with Docker
```bash
$ docker run --rm ivan/tpl:latest --help
```

## Usage

```bash
$ tpl -h
Usage: tpl [options] [template input]:

Executes the template from the input applying the environment passed in options.

If you specify the input template via an option flag (-input), then it will
read it inline.
If you specify that the template comes from the stdin by setting the option flag
(-stdin), then it will read it inline from the stdin.
If you set it as the first argument, it assumes that it is a file.

For the environment (-env) it will expect it as inline data. However, if you
start it with @, it will assume it is a file. It tries to guess the format,
giving precedence to JSON, then TOML, and lastly to YAML. Unless you specify to
force parsing it via an option flag.

The output is sent to stdout.

Options:
-debug
Set log level to debug.
-env string
The environment for the template (YAML, JSON or TOML).
-input string
The template input to process.
-json
Force the environment to be parsed as a JSON.
-stdin
Read template from stdin.
-toml
Force the environment to be parsed as a TOML.
-yaml
Force the environment to be parsed as a YAML.
```

### YAML

Assuming you have a template with:

```hcl
module "app" {
source = "../modules/app"
name = "{{ .name }}"
}
```

Executing with the following YAML environment:

```YAML
name: test
```

```bash
$ cat <JSON

It works with JSON environments:

```bash
$ cat < /dev/null)"
{{- define "type" -}}
{{ . | replace "Event" "" | lower }}
{{- end -}}
{{- range . }}
{{ .created_at | toDate "2006-01-02T15:04:05Z07:00" | ago }} ago {{ include "type" .type }}:
{{- with .repo }}
{{ .name }}: {{ .url }}
{{- end }}
{{- end }}
EOF
```

Generates the output:

```
18h51m49s ago push:
ivanvc/tpl: https://api.github.com/repos/ivanvc/tpl
18h58m44s ago push:
ivanvc/tpl: https://api.github.com/repos/ivanvc/tpl
35h53m29s ago push:
ivanvc/tpl: https://api.github.com/repos/ivanvc/tpl
35h59m5s ago create:
ivanvc/tpl: https://api.github.com/repos/ivanvc/tpl
36h0m1s ago create:
ivanvc/tpl: https://api.github.com/repos/ivanvc/tpl
95h44m45s ago push:
ivanvc/aports: https://api.github.com/repos/ivanvc/aports
95h52m22s ago push:
ivanvc/aports: https://api.github.com/repos/ivanvc/aports
...
```

### TOML

It also supports TOML. Generating index pages is easy, given the
`index.html.tpl` template:

```html



Index


Index of {{ .dir }}



    {{- range .files }}
  • {{ . }}

  • {{- end }}

```

Running it with:

```bash
$ tpl -env @<(echo -e "dir='$(basename $PWD)'\nfiles=['$(echo * | sed s/[[:space:]]/\',\'/g)']") index.html.tpl
```

Generates the following output:

```html



Index


Index of tpl



```

### Template functions

`tpl` includes Sprig functions, refer to their
[documentation](https://masterminds.github.io/sprig/).

* `include`: This function is as defined by Helm. So you can include
templates defined before, see the [JSON](#JSON) example to see its usage.
* `fileSize`: This function returns a human-readable file size from
integer bytes, i.e. given the input "1024", it returns "1.0 kB".

## License

See [LICENSE](LICENSE) © [Ivan Valdes](https://github.com/ivanvc/)

[Go template]: https://pkg.go.dev/text/template
[releases page]: https://github.com/ivanvc/tpl/releases