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

Allow for variable substitution #1

Open
apognu opened this issue May 5, 2019 · 2 comments
Open

Allow for variable substitution #1

apognu opened this issue May 5, 2019 · 2 comments
Assignees

Comments

@apognu
Copy link

apognu commented May 5, 2019

Currently (as far as I know), if one wants to use variables substitution in a filter expressions, it becomes a mess of escaped quotes that is diffcult to write and read, like so:

$ jg ".people[{\"craft\":\"$CRAFT\"}]"

Maybe allowing for single quotes in the filter would clean it up a bit:

$ jg ".people[{'craft':'$CRAFT'}]"

Another possibility, but that will only work for keys and value that do not contain : or ], would be to allow dropping the quotes altogether. Though it might be more difficult to implement. Something like:

$ jg ".people[{craft:$CRAFT}]"

What do you think?

@gmmorris
Copy link
Owner

gmmorris commented May 5, 2019

Hey Antoine,
Thanks for taking the time for this feedback. :)

I've been giving this some thought and I'm still playing around with ideas, so no decisions made, but here are some thoughts.

Starting at the end; dropping the quotes all together wouldn't work for a variety of reason such as the fact that we want to support every value that's possible in standard JSON, such as $ or } etc.

I'm not crazy about the single quotes idea because it drives the syntax further away from standard JSON (which will be unintuitive to users of the CLI, IMHO).
In addition, to be honest, relying on the standard BASH substitution doesn't really sit well with me... because it always feels very implicit (it's why I always spend 2 hours on every sed command I try 😂).
So, I'm not saying no to the single quote idea, but I'd rather explore other options first.

One idea I'm playing around with (in my head, so far) is passing in variables using a different CLI argument, and doing the substitution ourselves.

For example:

export VIDEO_MIME="mp4"
jg -e '.video.mimes[*="{}"]' --params "$VIDEO_MIME"

This would substitute internally as:

jg -e '.video.mimes[*="mp4"]'

We could also implement this slightly differently, where you specify the key too:

export VIDEO_MIME="mp4"
jg -e '.video.mimes[*="$VIDEO_MIME"]' --params "VIDEO_MIME=$VIDEO_MIME"

But this feels far too verbose, so I'd opt for the first implementation.

What do you think of that approach?

@gmmorris gmmorris self-assigned this May 5, 2019
@gmmorris
Copy link
Owner

gmmorris commented Aug 7, 2019

Sorry it took a while, but I've now added a release (0.1.7) with the above implementation.

If you try using the params flag you can pass in as many parameters as you wish.
Each {} marker will be replaced until no more {} s can be found or no more params have been specified.

So for example, this will work:

export VIDEO_MIME="mp4"
jg -e '.video.mimes[*="{}"]' --params "$VIDEO_MIME"

and will be treated as:

jg -e '.video.mimes[*="mp4"]'

I'll add it to the docs once I've put some usage into it and feel it works as needed.
Feel free to download the latest version and give it a whirl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants