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

feat: Add jq template function #2833

merged 1 commit into from
Mar 17, 2023

Conversation

twpayne
Copy link
Owner

@twpayne twpayne commented Mar 8, 2023

Refs #2826.

@twpayne twpayne requested a review from halostatue March 8, 2023 00:35
@halostatue
Copy link
Collaborator

halostatue commented Mar 8, 2023

I’m getting an unexpected [] wrapping the output:

$ go run . execute-template '{{ dict "hello" 1  | jq "." | toJson }}'
[{"hello":1}]
$ go run . execute-template '{{ 1  | jq "." | toJson }}'
[1]⏎

EDIT: Having read the documentation, I see that’s expected. It doesn’t feel quite right to me.

@halostatue
Copy link
Collaborator

This is going to lead to very hard to read template functions:

op document list --format json |
  go run . execute-template --with-stdin \
    '{{ .chezmoi.stdin | fromJson | jq ". | map(select(.title | startswith(\"aws-credentials: \")) | { (.title): .id }) | add" | first | toJson }}'

I’m also not sure how I would embed dynamic variables given the overloading of . and the fact that the JQ source string isn’t a template itself.

I don’t think this is a bad addition, but I don’t know how useful it will be.

@halostatue
Copy link
Collaborator

I think that this will be more useful if we approach the jq source file as . | jq (includeTemplate "…" (data)). There’s no need for any additional functions (I was going to suggest jqFile, but that probably isn’t necessary with include and includeTemplate).

Copy link
Collaborator

@halostatue halostatue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be on the lookout for more complex examples to demonstrate, because the value of jq here will be in the flexibility provided.

@twpayne
Copy link
Owner Author

twpayne commented Mar 10, 2023

I don’t think this is a bad addition, but I don’t know how useful it will be.

That's a very fair point. I'll close this PR for now. We can re-open it if it turns out that there's a good use for this functionality.

@twpayne twpayne closed this Mar 10, 2023
@twpayne twpayne reopened this Mar 16, 2023
@twpayne
Copy link
Owner Author

twpayne commented Mar 16, 2023

Re-opening as @halostatue identified a possible use case in #2864 (reply in thread).

Another use case is a replacement for some of the password manager functions like onepasswordDetailsFields and bitwardenFields. In short, the jq template function allows extraction of specific data from complex structures in a succinct way.

@halostatue
Copy link
Collaborator

halostatue commented Mar 17, 2023

It would be nice to have this be able to call out to jq instead of using gojq if desired (similar to opting into the built-in age, but instead opting into the use of the jq binary).

This might be better done as a future PR.

@twpayne twpayne merged commit 6893312 into master Mar 17, 2023
@twpayne twpayne deleted the jq branch March 17, 2023 20:33
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants