-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add builtin to output to file #3153
Comments
If you ok with using fq i've used a tar hack a few times to output multiple files. Something like this: Copy tar code from https://github.com/wader/fq/wiki/snippets into tar.jq then $ fq -n -L . 'include "tar"; to_tar({filename: "a", data: "aaa"}, {filename: "b", data: "bbb"})' | tar tv
-rw-r--r-- 0 user group 3 Jan 1 1970 a
-rw-r--r-- 0 user group 3 Jan 1 1970 b Maybe you could rewrite the tar code to work with standard jq but then as jq does not support raw binary output you might be limited to just ASCII data in files etc. |
Simple way of doing this is outputting a shell script from jq. That's how
|
In general, I also prefer outputting shell scripts over something like #3133 (although I didn't know about However, there's been several times where I've had to split the output into multiple shell scripts, and the resulting doubly-escaped script was a headache to review, which was what inspired this proposal. Presumably, there's many other places where having this as a builtin would make for a nice quality-of-life improvement. |
Agree that it would be nice with more I/O features. In my view the biggest issue is how to make it all fit nicely together, e.g #1843 includes file handles support that would make some of this possible to implement as builtins i think. Then also what would be good names and API? Maybe a way forward could be to flash out how these API could look like and be used by a user and then maybe see what subset could be implement without major changes? that way we could minimize risk of adding something that turns out to be incompatible or awkward to combine with future fancier I/O, coeval, etc support. |
For IO, I would advocate for having very few individually tailored high-level primitives, rather than many low-level building blocks like in that PR. Due to the nature of jq being a functional language, interacting with the outside world is a much more advanced feature than usual1, and can end up being surprisingly asymmetrical (see below). I'm even willing to be convinced that IO doesn't even belong in jq at all (hence this proposal being opened as an issue rather than a PR).
I would actually advocate for something like an Usage would be something like: jq --input-var '$input | .["a.json"]' *.json
Another way to manage this risk could be to prefix experimental APIs (for example, this could be named Footnotes
|
I would like to propose a new builtin, say
output_file("filename"; "contents")
, which copies its input to its output while saving its arguments as a file. It's similar in principle to thedebug
builtin.If
--sandbox
is specified, it will simply output the filename to stderr, essentially acting as a dry run.Having this builtin would make it less awkward to split json files. See below for some workarounds that are currently required:
Proposed semantics:
The text was updated successfully, but these errors were encountered: