Skip to content

Commit

Permalink
Add sprig functions
Browse files Browse the repository at this point in the history
  • Loading branch information
docwhat committed Apr 12, 2018
1 parent 4880207 commit aebab90
Show file tree
Hide file tree
Showing 23 changed files with 2,291 additions and 36 deletions.
69 changes: 34 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/docwhat/temple)](https://goreportcard.com/report/github.com/docwhat/temple) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/56ac41ac47614f7dabd5e30145c224b3)](https://www.codacy.com/app/docwhat/temple?utm_source=github.com&utm_medium=referral&utm_content=docwhat/temple&utm_campaign=Badge_Grade) [![Code Climate](https://codeclimate.com/github/docwhat/temple/badges/gpa.svg)](https://codeclimate.com/github/docwhat/temple) [![Issue Count](https://codeclimate.com/github/docwhat/temple/badges/issue_count.svg)](https://codeclimate.com/github/docwhat/temple)

Temple
======
# Temple

Sick of `sed`? Peaked about `perl`? Use `temple` to substitute your variables!

Installation
------------
## Installation

### Binaries

Expand All @@ -18,12 +16,11 @@ I have pre-built binaries for several platform already. They are available on th

If you have go v1.6 installed, then you can build the binary with the following command:

``` .sh
$ go get -u -v docwhat.org/temple
```bash
go get -u -v docwhat.org/temple
```

Usage
-----
## Usage

usage: temple [<flags>] <template>

Expand All @@ -40,56 +37,58 @@ Usage

Note that the JSON file must be an object at the top level. Example:

``` json
```json
{
"key": "value",
"key2": 2
}
```

Template Syntax
---------------
## Template Syntax

For complete documentation, read go's [text/template](https://golang.org/pkg/text/template/) and [html/template](https://golang.org/pkg/html/template/).

### Sprig Functions

Temple supports the complete list of [Sprig functions](http://masterminds.github.io/sprig/).

### Data Sources

- `{{env "VARIABLE"}}` -- The environment variable `VARIABLE`. If `VARIABLE` isn't set, then you get an empty string.
- `{{hostname}}` -- The systems fully qualified domain name.
- `{{uid}}` -- `UID` of the user running `temple`.
- `{{gid}}` -- `GID` of the user running `temple`.
- `{{euid}}` -- Effective `UID` of the user running `temple`.
- `{{egid}}` -- Effective `GID` of the user running `temple`.
- `{{pwd}}` -- The current working directory.
- `{{json}}` -- Access to your JSON data. Use dot notation to get access to items. e.g. `{{json.authors.greenwood.first_name}}`
* `{{hostname}}` -- The systems fully qualified domain name.
* `{{uid}}` -- `UID` of the user running `temple`.
* `{{gid}}` -- `GID` of the user running `temple`.
* `{{euid}}` -- Effective `UID` of the user running `temple`.
* `{{egid}}` -- Effective `GID` of the user running `temple`.
* `{{pwd}}` -- The current working directory.
* `{{json}}` -- Access to your JSON data. Use dot notation to get access to items. e.g. `{{json.authors.greenwood.first_name}}`

### Functions

- `{{index <expr> 99}}` -- The 99th item of the array `<expr>`.
- `{{<expr> | js}}` -- `<expr>` escaped/quoted for JavaScript & JSON.
- `{{<expr> | html}}` -- `<expr>` escaped/quoted for HTML.
- `{{<expr> | urlquery}}` -- `<expr>` escaped/quoted for a URL quoting. i.e. replacing spaces with `+` and using `%NN` syntax.
- `{{<expr> | shellquote}}` -- `<expr>` escaped/quoted for POSIX shells.
- `{{<expr> | len}}` -- The length of the `<expr>`.
* `{{index <expr> 99}}` -- The 99th item of the array `<expr>`.
* `{{<expr> | js}}` -- `<expr>` escaped/quoted for JavaScript & JSON.
* `{{<expr> | html}}` -- `<expr>` escaped/quoted for HTML.
* `{{<expr> | urlquery}}` -- `<expr>` escaped/quoted for a URL quoting. i.e. replacing spaces with `+` and using `%NN` syntax.
* `{{<expr> | shellquote}}` -- `<expr>` escaped/quoted for POSIX shells.
* `{{<expr> | len}}` -- The length of the `<expr>`.

### Flow Control

- `{{if <expr>}}true string{{else}}false string{{end}}` -- If/Else syntax. The `{{else}}` is optional.
- `{{range <array>}} item: {{.}} {{else}} The list is empty {{end}}` -- Iterate over `<array>`. The `{{else}}` is optional.
* `{{if <expr>}}true string{{else}}false string{{end}}` -- If/Else syntax. The `{{else}}` is optional.
* `{{range <array>}} item: {{.}} {{else}} The list is empty {{end}}` -- Iterate over `<array>`. The `{{else}}` is optional.

### Misc.
### Miscellaneous

- `{{<expr> -}}` -- Trim whitespace to the right. e.g. `{{1 -}} .0` becomes `1.0`.
- `{{- <expr>}}` -- Trim whitespace to the left.
- `{{- <expr> -}}` -- Trim whitespace to the right and left.
- `{{/* comment */}}` -- Comments!
* `{{<expr> -}}` -- Trim whitespace to the right. e.g. `{{1 -}} .0` becomes `1.0`.
* `{{- <expr>}}` -- Trim whitespace to the left.
* `{{- <expr> -}}` -- Trim whitespace to the right and left.
* `{{/* comment */}}` -- Comments!

## Related Projects

* [gomplate](https://github.com/hairyhenderson/gomplate)

## Thanks

- [@alecthomas](https://github.com/alecthomas) for [kingpin](https://github.com/alecthomas/kingpin)
- [@kballard](https://github.com/kballard) for [go-shellquote](https://github.com/kballard/go-shellquote)
- [@seh](https://github.com/seh) for Go help
* [@alecthomas](https://github.com/alecthomas) for [kingpin](https://github.com/alecthomas/kingpin)
* [@kballard](https://github.com/kballard) for [go-shellquote](https://github.com/kballard/go-shellquote)
* [@seh](https://github.com/seh) for Go help
9 changes: 9 additions & 0 deletions examples/sprig/sprig.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Simple Sprig Tests

| given | expected |
| ------------------------------- | ------------ |
| `{{trim " hello "}}` | `hello` |
| `{{nospace "hello w o r l d"}}` | `helloworld` |
| `{{ now | date "2006-01-02" }}` | Today's Date |

<!-- vim:set ft=markdown: -->
5 changes: 4 additions & 1 deletion functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path"
textTemplate "text/template"

sprig "github.com/Masterminds/sprig"

shellquote "github.com/kballard/go-shellquote"
)

Expand All @@ -18,7 +20,6 @@ type FuncMap map[string]interface{}
func buildFuncMap(jsonDataFile string) FuncMap {
funcMap := make(FuncMap)

funcMap["env"] = os.Getenv
funcMap["uid"] = os.Getuid
funcMap["gid"] = os.Getgid
funcMap["euid"] = os.Geteuid
Expand Down Expand Up @@ -55,6 +56,7 @@ func dataFunc(jsonDataFileName string) func() map[string]interface{} {
func doTextTemplate(file string, funcMap FuncMap, emitter io.Writer) {
template := textTemplate.
New(path.Base(file)).
Funcs(sprig.TxtFuncMap()).
Funcs(textTemplate.FuncMap(funcMap)).
Option("missingkey=zero")

Expand All @@ -70,6 +72,7 @@ func doTextTemplate(file string, funcMap FuncMap, emitter io.Writer) {
func doHTMLTemplate(file string, funcMap FuncMap, emitter io.Writer) {
template := htmlTemplate.
New(path.Base(file)).
Funcs(sprig.FuncMap()).
Funcs(htmlTemplate.FuncMap(funcMap)).
Option("missingkey=zero")

Expand Down
153 changes: 153 additions & 0 deletions vendor/github.com/Masterminds/sprig/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/Masterminds/sprig/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/Masterminds/sprig/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aebab90

Please sign in to comment.