Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/link-society/gotempl

Generic templating tool with support of JSON, YAML and TOML data
https://github.com/link-society/gotempl

golang json template toml yaml

Last synced: 3 months ago
JSON representation

Generic templating tool with support of JSON, YAML and TOML data

Awesome Lists containing this project

README

        

# gotempl

Small binary used to generate files from Go Templates, environment variables
and data files.

The following formats are supported:

- JSON
- YAML
- TOML
- ENV (environment variables in the form "key=value")

## Usage

```text
usage: gotempl [--help] [--completion] [--template TEMPLATE [TEMPLATE ...]] [--output OUTPUT] [--data-json DATA-JSON [DATA-JSON ...]] [--data-yaml DATA-YAML [DATA-YAML ...]] [--data-toml DATA-TOML [DATA-TOML ...]] [--data-env DATA-ENV [DATA-ENV ...]]

Generic templating tool which use both environment variables and data files as template data

optional arguments:
--help, -h show this help message
--completion show command completion script
--html, -H Escape template for HTML output
--template TEMPLATE, -t TEMPLATE Path to Go Template file. Default is stdin.
--output OUTPUT, -o OUTPUT Path to output file. Default is stdout
--data-json DATA-JSON, -j DATA-JSON Path to JSON file
--data-yaml DATA-YAML, -y DATA-YAML Path to YAML file
--data-toml DATA-TOML, -T DATA-TOML Path to TOML file
--data-env DATA-ENV, -e DATA-ENV Path to ENV file
```

### Example: Rendering JSON

Let's create a file named `sample.json` containing the following:

```json
{
"name": "John Smith"
}
```

And a file named `greeting.template` containing the following:

```tmpl
Hello {{ .Data.name }}
```

Using **gotempl**, you can then render those files to:

```bash
$ gotempl -t greeting.template --data-json sample.json
Hello John Smith
```

### Example: Rendering multiple files

Let's create a file named `greeting.yaml` containing the following:

```yaml
greeting:
polite: Good morning
informal: Hi
```

Then a file `sample.json` containing the following:

```json
{
"informal": true,
"name": "John"
}
```

And finally, a file `greeting.template` containing the following:

```tmpl
{{- if .Data.informal -}}
{{ .Data.greeting.informal }} {{ .Data.name }}
{{- else -}}
{{ .Data.greeting.polite }} {{ .Data.name }}
{{- end -}}
```

Using **gotempl**, you can then render those files to:

```bash
$ gotempl -t greeting.template --data-yaml greeting.yaml --data-json sample.json
Hi John
```

### Example: Reading template from stdin

Using the previous `sample.json` file and **gotempl**, you can render it to:

```bash
$ export GREETING="Hello"
$ cat <