Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dolmen-go/goproc
Tool to apply Go templates to JSON/YAML data
https://github.com/dolmen-go/goproc
Last synced: about 1 month ago
JSON representation
Tool to apply Go templates to JSON/YAML data
- Host: GitHub
- URL: https://github.com/dolmen-go/goproc
- Owner: dolmen-go
- License: apache-2.0
- Created: 2018-12-07T10:16:09.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-30T13:45:28.000Z (9 months ago)
- Last Synced: 2024-11-09T19:41:24.287Z (2 months ago)
- Language: Go
- Size: 27.3 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-repositories - dolmen-go/goproc - Tool to apply Go templates to JSON/YAML data (Go)
README
# goproc
Apply [Go templates](https://golang.org/pkg/text/template/#hdr-Text_and_spaces) to JSON or YAML data.
## Install
go install github.com/dolmen-go/goproc@latest
## Usage
goproc [ -env | -env=true | -env=VAR ] -e [ ]
goproc [ -env | -env=true | -env=VAR ] -i [ ]The default input is STDIN in JSON format. Use `-yaml` flag to handle STDIN as YAML.
When an input file is given, the file extension determines if it is parsed as JSON or YAML.
In any case there is no magic detection (that usually lead to security issues).
## Template syntax
See the [Go templates](https://golang.org/pkg/text/template/#hdr-Text_and_spaces) documentation.
The [Hugo documentation on Go templates](https://gohugo.io/templates/introduction/) may also be useful for a friendlier approach, but note that it contains references to features unique to Hugo (ex: `partial`).
## Functions extensions
The following functions are available in addition to the [standard functions](https://golang.org/pkg/text/template/#hdr-Functions).
### `env`
Allow to use environment variables, with security protections.
This function must be explicitely enabled using the `-env` flag:
* `-env`: enables the `env` function. Any environment variable can be used, but listing variables is blocked.
* `-env=`: enables the `env` function, but whitelist of allowed variables is cleared (no variables allowed).
* `-env=name1,name2`: enables the `env` function. Only the variables `name1` and `name2` are visible in calls to `env`.Usage:
{{ env "HOME" }} {{- /* Get value of HOME environment variable */}}
{{ "HOME" | env }} {{- /* Get value of HOME environment variable */}}
{{ range $name, $value := env "HOME" "LANG" -}}
{{ $name }}={{ $value }}
{{ end }}### `error`
Usage:
{{ error "message" }}
Example:
echo 0 | goproc -e '{{ error "fail!" }}'
template: :1:3: executing "" at : error calling error: fail!### `jsonptr`
To ease the extraction of data, `jsonptr` allows to express data location using
JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)).Usages:
{{ jsonptr "pointer" . }}
{{ . | jsonptr "pointer" }}Examples:
1. `goproc` [`testdata/02.gotmpl`](testdata/02.gotmpl) [`testdata/02.json`](testdata/02.json)
2. `goproc` [`testdata/03.gotmpl`](testdata/03.gotmpl) [`testdata/03.json`](testdata/03.json)### `json`
Convert input to JSON.
Usage:
{{ json }}
Example:
echo '{"data": ["x"]}' | goproc -e '{{.data | json}}{{print "\n"}}'
["x"]### `yaml`
Convert input to YAML.
Usage:
{{ yaml }}
Example:
echo '{"data": ["x"]}' | goproc -e '{{.data | yaml}}'
- x## See also
https://github.com/naotookuda/go-template-cli
## License
Copyright 2018-2024 Olivier Mengué
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.