feat: add --stdin-filename flag to auto-detect the format of STDIN input#2784
Open
hsdfat wants to merge 1 commit into
Open
feat: add --stdin-filename flag to auto-detect the format of STDIN input#2784hsdfat wants to merge 1 commit into
hsdfat wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1876.
What
Adds a
--stdin-filenameflag. When input comes from STDIN, the given name is used for automatic input/output format detection, exactly as if the data had been read from that file:The name is only ever passed to
filepath.Ext— the file is never opened and does not need to exist, which is what makes this useful for editor and formatter integrations. This follows the naming suggested in the issue (--stdin-filename, as used by ruff) rather than dprint's--stdin.Why
Per the issue: an editor integration that wants yq to format a buffer currently has to hardcode yq's list of formats and pass the right
-pitself. Handing yq the buffer's filename instead lets yq's own detection do the work, so integrations stay correct as yq gains formats.How
The production change is three lines in
configureInputFormat:formatFilenamefeeds format detection;inputFilenameis left untouched so the existinginputFilename != "-"backwards-compatibility check still behaves correctly. Everything else is flag registration, tests and the regenerated README flag table. Nothing inpkg/yqlibis touched.Behaviour is unchanged when the flag is absent, an explicit
-p/--input-formatstill wins, and the flag is ignored when a real file argument is supplied.Tests
cmd/utils_test.go— 11 new cases in the existingTestConfigureInputFormat/TestConfigureFormatstables: stdin with and without the flag, known/unknown/absent extension, explicit-format-wins, and ignored-when-a-file-is-given.acceptance_tests/inputs-format-auto.sh— 5 e2e cases acrossyqandyq ea, covering JSON and XML stdin, unknown extensions, the explicit-argument form, and the-p+--stdin-filenamecombination../scripts/format.sh,./scripts/check.sh,./scripts/spelling.sh,go test ./cmd/...andscripts/acceptance.shall pass.Two failures exist on clean
masterand are unrelated to this change (confirmed by re-running there):TestTomlScenariosafter the go-toml v2.4.2 bump in e2f1d5c changed an error string, and agcilint hit inpkg/yqlib/encoder_toml.go.One question on scope
I kept this to format detection only, since that is what the issue asks for — it does not touch
yqlib.SetFilenameAlias(), so thefilenameoperator andbad file '-'errors still report-for STDIN.Wiring it through would make those report the given name too, which is arguably the least surprising behaviour, but it goes beyond the request and widens the diff into
pkg/yqlib. Happy to add it in this PR or a follow-up if you'd prefer that — just say which.