diff --git a/cmd/root.go b/cmd/root.go index ffbf55cb..c33b230a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -203,6 +203,7 @@ yq -P -oy sample.json } rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.") rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.FixMergeAnchorToSpec, "yaml-fix-merge-anchor-to-spec", "", false, "Fix merge anchor to match YAML spec. Will default to true in late 2025") + rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.CompactSequenceIndent, "yaml-compact-seq-indent", "c", false, "Use compact sequence indentation where '- ' is considered part of the indentation.") rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.") if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil { diff --git a/pkg/yqlib/encoder_yaml.go b/pkg/yqlib/encoder_yaml.go index a5c86a27..6c7869fa 100644 --- a/pkg/yqlib/encoder_yaml.go +++ b/pkg/yqlib/encoder_yaml.go @@ -52,6 +52,9 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error { var encoder = yaml.NewEncoder(destination) encoder.SetIndent(ye.prefs.Indent) + if ye.prefs.CompactSequenceIndent { + encoder.CompactSeqIndent() + } target, err := node.MarshalYAML() diff --git a/pkg/yqlib/yaml.go b/pkg/yqlib/yaml.go index 1daba35d..9a185419 100644 --- a/pkg/yqlib/yaml.go +++ b/pkg/yqlib/yaml.go @@ -8,6 +8,7 @@ type YamlPreferences struct { UnwrapScalar bool EvaluateTogether bool FixMergeAnchorToSpec bool + CompactSequenceIndent bool } func NewDefaultYamlPreferences() YamlPreferences { @@ -19,6 +20,7 @@ func NewDefaultYamlPreferences() YamlPreferences { UnwrapScalar: true, EvaluateTogether: false, FixMergeAnchorToSpec: false, + CompactSequenceIndent: false, } } @@ -31,6 +33,7 @@ func (p *YamlPreferences) Copy() YamlPreferences { UnwrapScalar: p.UnwrapScalar, EvaluateTogether: p.EvaluateTogether, FixMergeAnchorToSpec: p.FixMergeAnchorToSpec, + CompactSequenceIndent: p.CompactSequenceIndent, } }