Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add jq template function #2833

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions assets/chezmoi.io/docs/reference/templates/functions/jq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `jq` *query* *input*

`jq` runs the [jq](https://stedolan.github.io/jq/) query *query* against *input*
and returns a list of results.

!!! example

```
{{ dict "key" "value" | jq ".key" | first }}
```

!!! warning

`jq` uses [`github.com/itchyny/gojq`](https://github.com/itchyny/gojq),
which behaves slightly differently to the `jq` command in some [edge
cases](https://github.com/itchyny/gojq#difference-to-jq).
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ nav:
- includeTemplate: reference/templates/functions/includeTemplate.md
- ioreg: reference/templates/functions/ioreg.md
- joinPath: reference/templates/functions/joinPath.md
- jq: reference/templates/functions/jq.md
- lookPath: reference/templates/functions/lookPath.md
- lstat: reference/templates/functions/lstat.md
- mozillaInstallHash: reference/templates/functions/mozillaInstallHash.md
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/google/go-github/v50 v50.1.0
github.com/google/renameio/v2 v2.0.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/itchyny/gojq v0.12.12
github.com/klauspost/compress v1.16.0
github.com/mitchellh/mapstructure v1.5.0
github.com/muesli/combinator v0.3.0
Expand Down Expand Up @@ -92,6 +93,7 @@ require (
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA=
github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func newConfig(options ...configOption) (*Config, error) {
"includeTemplate": c.includeTemplateTemplateFunc,
"ioreg": c.ioregTemplateFunc,
"joinPath": c.joinPathTemplateFunc,
"jq": c.jqTemplateFunc,
"keepassxc": c.keepassxcTemplateFunc,
"keepassxcAttachment": c.keepassxcAttachmentTemplateFunc,
"keepassxcAttribute": c.keepassxcAttributeTemplateFunc,
Expand Down
25 changes: 25 additions & 0 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"

"github.com/bradenhilton/mozillainstallhash"
"github.com/itchyny/gojq"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -277,6 +278,30 @@ func (c *Config) joinPathTemplateFunc(elem ...string) string {
return filepath.Join(elem...)
}

func (c *Config) jqTemplateFunc(source string, input any) any {
query, err := gojq.Parse(source)
if err != nil {
panic(err)
}
code, err := gojq.Compile(query)
if err != nil {
panic(err)
}
iter := code.Run(input)
var result []any
for {
value, ok := iter.Next()
if !ok {
break
}
if err, ok := value.(error); ok {
panic(err)
}
result = append(result, value)
}
return result
}

func (c *Config) lookPathTemplateFunc(file string) string {
switch path, err := chezmoi.LookPath(file); {
case err == nil:
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ stdout ^data$
exec chezmoi execute-template '{{ joinPath "a" "b" }}'
stdout a${/}b

# test jq template function
exec chezmoi execute-template '{{ dict "key" "value" | jq ".key" | first }}'
stdout ^value$

# test lookPath template function to find in PATH
exec chezmoi execute-template '{{ lookPath "go" }}'
stdout go$exe
Expand Down