-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: allow writing to output file #3367
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
base: master
Are you sure you want to change the base?
Conversation
|
Test on linux with utf-8: ✘-INT ~/Programmierung/jq [ofile|✚ 1…1] |
|
There has been talks long time ago about adding adding $ echo -n 12 > a.json
$ echo -n 34 > b.json
$ jq . a.json b.json
1234
$ jaq . a.json b.json
12
34
$ jaq -i '.+1' a.json b.json
$ jaq . a.json b.json
13
35 |
that is an interesting thought. In the use case I am hoping to solve, the output file needs to be in a certain place that is designed to hold only results. This is, what the tekton task specification might look like |
This allows to write the output to a file and introduces the options -o and --output-file that take a filename as parameter. When not specifying -o, stdout will be used for compatibility. This will be helpful when calling jq inside a docker context as it means jq will not have to be called from within a shell with output redirection.
|
How can this best be progressed? |
|
Hey sorry for the delay. I think the implementation look resonable. I'm mostly concern and reluctant about adding more arguments and features and in this case maybe that we might want inplace edit in the future, bloat to have both? It would also be nice to get some input from some other maintainers, ping @itchyny @emanuele6 @pkoppstein |
You, of course can make that decision. For the case I outlined above, in-place edit would not solve the problem as tekton separates inputs and outputs, with inputs being accessible read-only. |
|
I don’t agree that this is bloat, even if |
|
To clarify i menat to have both might be bloaty both in terms of implementation and adding more cli arguments, but don't think i have that strong opinion about more than concern about making the code a lot less maintainable. Maybe a good pitch to do any of this could be a nice refactor? not that i know that much the input/output code |
Where do you see the refactoring potential in the area of the output code? |
|
@wader I think calling it bloat is definitely reaching too far. It seems useful to me and the code only needs minor fixups. @christf I'm reviewing it now. I think jq avoids global state so multiple VMs can be instantiated, so the global |
I'm probably the last person to have touched the argument parsing. I don't think this adds an abnormal amount of code for a new flag. As for in-place editing, I think the work here actually assists in that effort and it probably wouldn't take much effort on top of what's here. Before processing any of the input, it would read the first input file to the end and store it in a buffer. then seek back to the start, change it to write-only, set it to be the Also, in my version of --output-file, I clearly delineate between what things write to the primary output, so the JSON output stream would go to --output-file/--inplace and a disassembly or debug trace would still go to stdout. You'd want that for --inplace too. |
|
Thank you very much for picking up the pieces. Your work goes much beyond what I initially raised as a PR. Also it leaves the codebase in better shape by preparing for a feature. That bit about the debug trace is an aspect that wasn't even discussed in here. Thank you! |
Sorry i didn't mean to come of as harsh. Maybe just my bad memories of trying to add/fix some IO-related things like #1225 that ended up quite complex for how to implement and how it would work in streaming mode, with But great work and it would be good to get some more maintainers opinon on this before spending much time on it. |
This allows to write the output to a file and closes #2418 . When calling jq inside a docker context this will be helpful as it means jq will not have to be called from within a shell with output redirection.
Caveats do apply: