gt

command module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 1 Imported by: 0

README

gt (JSON go-templating renderer)

License

Description

A command-line tool to render Go templates from JSON input data.

The tool is invoked with the path to the template file. If a second argument is provided, it is treated as the path to a JSON file to use as input for the template. If no second argument is provided, the tool reads JSON input from stdin.

Features

Builtin functions

env

The env function returns the value of the specified environment variable.

{{ env "ENV_VAR_NAME" }}

Expected output:

value
now

The now function returns the current date and time.

{{ now }}

Expected output:

2021-09-01 12:00:00
date

The date function formats a date and time value using a specified layout.

{{ now | date "2006-01-02"}}

Expected output:

2021-09-01
add

The add function adds two numbers.

{{ 1 | add 2 }}

Expected output:

3
sub

The sub function subtracts two numbers.

{{ 2 | sub 1 }}

Expected output:

1
mul

The mul function multiplies two numbers.

{{ 1 | mul 2 }}

Expected output:

2
div

The div function divides two numbers.

{{ 1 | div 2 }}

Expected output:

0.5
upper

The upper function converts a string to uppercase.

{{ "hello" | upper }}

Expected output:

HELLO
lower

The lower function converts a string to lowercase.

{{ "HELLO" | lower }}

Expected output:

hello
trim

The trim function removes leading and trailing whitespace from a string.

{{ "  hello  " | trim }}

Expected output:

hello
trimleft

The trimleft function removes leading whitespace from a string.

{{ "  hello  " | trimleft }}

Expected output:

hello  
trimright

The trimright function removes trailing whitespace from a string.

{{ "  hello  " | trimright }}
replace

The replace function replaces all occurrences of a substring in a string with another substring.

{{ "hello world" | replace "world" "go" }}

Expected output:

hello go
contains

The contains function returns true if a string contains a specified substring.

{{ "hello world" | contains "world" }}

Expected output:

true
hasprefix

The hasprefix function returns true if a string has a specified prefix.

{{ "hello world" | hasprefix "hello" }}

Expected output:

true
hassuffix

The hassuffix function returns true if a string has a specified suffix.

{{ "hello world" | hassuffix "world" }}

Expected output:

true
indexof

The indexof function returns the index of the first occurrence of a substring in a string.

{{ "hello world" | indexof "world" }}

Expected output:

6
lastindexof

The lastindexof function returns the index of the last occurrence of a substring in a string.

{{ "hello world" | lastindexof "o" }}

Expected output:

7
reverse

The reverse function reverses a string.

{{ "hello" | reverse }}

Expected output:

olleh
substr

The substr function returns a substring of a string.

{{ "hello world" | substr 6 }}

Expected output:

world
escapeString

The escapeString function escapes special characters in a string.

{{ "hello <world>" | escapeString }}

Expected output:

hello &lt;world&gt;
len

The len function returns the length of a string or array.

{{ "hello" | len }}

Expected output:

5
regexFind

The regexFind function returns the first match of a regular expression in a string.

{{ "hello world" | regexFind ".{4}$" }}

Expected output:

world
empty

The empty function returns true if a string or array is empty.

{{ "" | empty }}

Expected output:

true

Installation

Download from release page.

Usage

mytemplate.tpl:

{{ range . }}
{{ .name }} is {{ .age }} years old
{{ end }}

mydata.json:

[
  {
    "name": "Alice",
    "age": 30
  },
  {
    "name": "Bob",
    "age": 25
  }
]

Render from file

gt mytemplate.tpl mydata.json

Render from stdin

cat mydata.json | gt mytemplate.tpl

Expected output:

Alice is 30 years old
Bob is 25 years old

Contributing

Contributions are welcome! Please follow the guidelines in CONTRIBUTING.md.

License

This project is licensed under the MIT License.

Documentation

Overview

Copyright © 2024 Alexandre Pires

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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