yq icon indicating copy to clipboard operation
yq copied to clipboard

RFE: ability to specify input filename for stdin

Open scop opened this issue 1 year ago • 5 comments

Please describe your feature request.

In https://github.com/mikefarah/yq/issues/1484, the input/output mode choice was made defaulting dependent on the input filename. Thanks!

Now, if this would be also possible when processing stdin input, it'd be even nicer. For example, various editor formatter integrations work by piping input to external formatters through stdin. They may have a filename, but the file might not be saved anywhere yet, etc.

Describe the solution you'd like

Other formatters tend to implement this with a cmdline option --stdin <filename> (example: dprint), or --stdin-filename <filename> (example: ruff format). I'd personally go with the latter, it's clearer.

Note that this is just an indication of the filename for format detection purposes. The file does not necessarily exist.

Describe alternatives you've considered

N/A.

Additional context

N/A.

scop avatar Nov 20 '23 21:11 scop

Interesting - I'm guessing you can't just get the editor to pass in a value for the -p flag to give the format directly?

mikefarah avatar Nov 29 '23 03:11 mikefarah

That can be done. The drawback is having to hardcode knowledge of what input and output formats are available in yq, for example in https://github.com/radian-software/apheleia/pull/250

Being able to specify a filename for stdin and rely on yq picking it up if supported would be easier and less dependent on the installed yq version. (On the other hand, that ship has partially sailed though as we probably couldn't blindly pass this new flag to anything but new yq versions having it though.)

scop avatar Nov 29 '23 06:11 scop

Then again now that I had a look at the source, it appears the formats default to yaml if no other is detected, so passing arbitrary text data through yq might not be safe after all. I had assumed it does a pass through if it doesn't detect a supported file type, but I tested that in a faulty way. Oh well, passthrough might be another thing to consider, would be backwards incompatible though.

Also the defaulting to yaml could be documented more prominently e.g. in --help output.

scop avatar Nov 29 '23 07:11 scop

Good idea re help doc. Yeah it was never the intention to be able to pass through arbitrary text data - the main purpose is to query/manipulate data files (and so you would be passing an some sort expression to do that); and you'd expect an error if yq didn't recognise the format.

What are you using it for where it would retrieve data like that?

mikefarah avatar Nov 30 '23 04:11 mikefarah

My use case is using yq as a pretty printer for formats it has read+write support for.

scop avatar Dec 03 '23 19:12 scop