Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aburdulescu/tpl
Generate file based on a json data file and a template file
https://github.com/aburdulescu/tpl
command-line-tool go-templates golang json
Last synced: about 2 months ago
JSON representation
Generate file based on a json data file and a template file
- Host: GitHub
- URL: https://github.com/aburdulescu/tpl
- Owner: aburdulescu
- Created: 2020-09-17T14:57:39.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-06T22:30:42.000Z (over 2 years ago)
- Last Synced: 2024-06-21T13:07:29.983Z (7 months ago)
- Topics: command-line-tool, go-templates, golang, json
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tpl
Generate file based on a json data file and a template file## Install:
- install golang
- then run
```
go install bandr.me/p/tpl@latest
```## Usage
Run the tool with `-h` to see the available options.
### Examples
You can find the files used here in the `example` directory.
The syntax of the template file can be found [here](https://pkg.go.dev/text/template).
#### Data file provided as argument
- template file(`example.tpl`)
```
My name is {{.name}}.
My friends are:
{{range .friends}}
- {{.}}
{{end}}
The answer to the "Ultimate Question to Life, the Universe, and Everything" is {{.answer}}.
```- data file(`example.json`)
```
{
"name": "Arthur Dent",
"friends": [
"Ford Prefect",
"Zaphod Beeblebrox",
"Marvin",
"Trillian"
],
"answer": 42
}
```- run the tool using the files above
```
tpl -t example.tpl example.json
``````
My name is Arthur Dent.
My friends are:- Ford Prefect
- Zaphod Beeblebrox
- Marvin
- Trillian
The answer to the "Ultimate Question to Life, the Universe, and Everything" is 42.
```#### Data file from `stdin`
- template file(`example2.tpl`)
```
Name: {{.name}}
Race: {{.race}}
```- data file(`example2.json`)
```
{
"persons": [
{
"name": "Arthur Dent",
"race": "human"
},
{
"name": "Ford Prefect",
"race": "alien"
}
]
}
```- run the tool and pass the data using [jq](https://stedolan.github.io/jq/)
```
jq ".persons[0]" | tpl -t example.tpl
``````
Name: Arthur Dent
Race: human
``````
jq ".persons[1]" | tpl -t example.tpl
``````
Name: Ford Prefect
Race: alien
```