Compare commits

...

5 Commits

Author SHA1 Message Date
Tony
7e63f45fa3
Merge 249efaee46 into e2f1d5ccf7 2026-07-02 09:56:36 -04:00
dependabot[bot]
e2f1d5ccf7
Bump go.yaml.in/yaml/v4 from 4.0.0-rc.5 to 4.0.0-rc.6 (#2759)
Bumps [go.yaml.in/yaml/v4](https://github.com/yaml/go-yaml) from 4.0.0-rc.5 to 4.0.0-rc.6.
- [Commits](https://github.com/yaml/go-yaml/compare/v4.0.0-rc.5...v4.0.0-rc.6)

---
updated-dependencies:
- dependency-name: go.yaml.in/yaml/v4
  dependency-version: 4.0.0-rc.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:54:05 +10:00
dependabot[bot]
16f149b351
Bump github.com/pelletier/go-toml/v2 from 2.4.0 to 2.4.2 (#2762)
Bumps [github.com/pelletier/go-toml/v2](https://github.com/pelletier/go-toml) from 2.4.0 to 2.4.2.
- [Release notes](https://github.com/pelletier/go-toml/releases)
- [Commits](https://github.com/pelletier/go-toml/compare/v2.4.0...v2.4.2)

---
updated-dependencies:
- dependency-name: github.com/pelletier/go-toml/v2
  dependency-version: 2.4.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:53:39 +10:00
dependabot[bot]
5da9215306
Bump actions/setup-go from 6.4.0 to 6.5.0 (#2763)
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 6.4.0 to 6.5.0.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](4a3601121d...924ae3a1cd)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-version: 6.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:53:18 +10:00
Tony
249efaee46 feat: add --split-exp-no-overwrite flag to refuse overwriting existing files
When using --split-exp to split documents into per-file outputs, yq
silently overwrites any pre-existing files at the target paths because
os.Create truncates. For workflows that generate filenames from input
data (e.g. '.metadata.name + ".yml"'), this can clobber unrelated files
when two documents map to the same name, or when a target path collides
with something already on disk.

This change adds an opt-in --split-exp-no-overwrite flag (and a
matching yqlib constructor NewMultiPrinterWriterWithOptions) that uses
O_WRONLY|O_CREATE|O_EXCL so existing files are left untouched and yq
exits with a clear error message instead.

The default behaviour (overwrite) is unchanged; the original
NewMultiPrinterWriter constructor still exists and now delegates to the
new options-aware constructor with noOverwrite=false.

Fixes #2028
2026-05-22 05:41:48 +08:00
10 changed files with 131 additions and 13 deletions

View File

@ -37,7 +37,7 @@ jobs:
steps:
- name: Set up Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: '^1.20'
id: go

View File

@ -15,7 +15,7 @@ jobs:
id-token: write
steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: '^1.20'
check-latest: true

View File

@ -433,6 +433,7 @@ Flags:
--shell-key-separator string separator for shell variable key paths (default "_")
-s, --split-exp string 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.
--split-exp-file string Use a file to specify the split-exp expression.
--split-exp-no-overwrite When using --split-exp, fail if a target file already exists instead of overwriting it.
--string-interpolation Toggles strings interpolation of \(exp) (default true)
--tsv-auto-parse parse TSV YAML/JSON values (default true)
-r, --unwrapScalar unwrap scalar, print the value with no quotes, colors or comments. Defaults to true for yaml (default true)

View File

@ -31,6 +31,7 @@ var frontMatter = ""
var splitFileExp = ""
var splitFileExpFile = ""
var splitFileNoOverwrite = false
var completedSuccessfully = false

View File

@ -206,6 +206,7 @@ yq -P -oy sample.json
if err = rootCmd.MarkPersistentFlagFilename("split-exp-file"); err != nil {
panic(err)
}
rootCmd.PersistentFlags().BoolVarP(&splitFileNoOverwrite, "split-exp-no-overwrite", "", false, "When using --split-exp, fail if a target file already exists instead of overwriting it.")
rootCmd.PersistentFlags().StringVarP(&expressionFile, "from-file", "", "", "Load expression from specified file.")
if err = rootCmd.MarkPersistentFlagFilename("from-file"); err != nil {

View File

@ -186,7 +186,7 @@ func configurePrinterWriter(format *yqlib.Format, out io.Writer) (yqlib.PrinterW
if err != nil {
return nil, fmt.Errorf("bad split document expression: %w", err)
}
printerWriter = yqlib.NewMultiPrinterWriter(splitExp, format)
printerWriter = yqlib.NewMultiPrinterWriterWithOptions(splitExp, format, splitFileNoOverwrite)
} else {
printerWriter = yqlib.NewSinglePrinterWriter(out)
}

4
go.mod
View File

@ -13,13 +13,13 @@ require (
github.com/hashicorp/hcl/v2 v2.24.0
github.com/jinzhu/copier v0.4.0
github.com/magiconair/properties v1.8.10
github.com/pelletier/go-toml/v2 v2.4.0
github.com/pelletier/go-toml/v2 v2.4.2
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/yuin/gopher-lua v1.1.2
github.com/zclconf/go-cty v1.18.1
go.yaml.in/yaml/v4 v4.0.0-rc.5
go.yaml.in/yaml/v4 v4.0.0-rc.6
golang.org/x/mod v0.37.0
golang.org/x/net v0.56.0
golang.org/x/text v0.38.0

8
go.sum
View File

@ -46,8 +46,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pelletier/go-toml/v2 v2.4.0 h1:Mwu0mAkUKbittDs3/ADDWXqMmq3EOK2VHiuCkV00Row=
github.com/pelletier/go-toml/v2 v2.4.0/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pelletier/go-toml/v2 v2.4.2 h1:M2fKKbmyvI+hGId/D0W64qDBMVhJnNR10O5gIbMc//Q=
github.com/pelletier/go-toml/v2 v2.4.2/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -68,8 +68,8 @@ github.com/zclconf/go-cty v1.18.1/go.mod h1:qpnV6EDNgC1sns/AleL1fvatHw72j+S+nS+M
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
go.yaml.in/yaml/v4 v4.0.0-rc.5 h1:JVliQq9EGOYaTgMi+k8BhUJyqcGk4ZqeuiN1Cirba9c=
go.yaml.in/yaml/v4 v4.0.0-rc.5/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
go.yaml.in/yaml/v4 v4.0.0-rc.6 h1:1h7H1ohdUh93/FyE4YaDa1Zh64K6VVbjF4K6WUxMtH4=
go.yaml.in/yaml/v4 v4.0.0-rc.6/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=

View File

@ -32,9 +32,17 @@ type multiPrintWriter struct {
nameExpression *ExpressionNode
extension string
index int
noOverwrite bool
}
func NewMultiPrinterWriter(expression *ExpressionNode, format *Format) PrinterWriter {
return NewMultiPrinterWriterWithOptions(expression, format, false)
}
// NewMultiPrinterWriterWithOptions creates a multi-file printer writer.
// When noOverwrite is true, attempting to write to a file that already
// exists will fail with an error instead of silently overwriting it.
func NewMultiPrinterWriterWithOptions(expression *ExpressionNode, format *Format, noOverwrite bool) PrinterWriter {
extension := "yml"
switch format {
@ -49,6 +57,7 @@ func NewMultiPrinterWriter(expression *ExpressionNode, format *Format) PrinterWr
extension: extension,
treeNavigator: NewDataTreeNavigator(),
index: 0,
noOverwrite: noOverwrite,
}
}
@ -75,10 +84,20 @@ func (sp *multiPrintWriter) GetWriter(node *CandidateNode) (*bufio.Writer, error
if err != nil {
return nil, err
}
f, err := os.Create(name)
if err != nil {
return nil, err
var f *os.File
if sp.noOverwrite {
f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("refusing to overwrite existing file %q (--no-overwrite is set)", name)
}
return nil, err
}
} else {
f, err = os.Create(name)
if err != nil {
return nil, err
}
}
sp.index = sp.index + 1

View File

@ -0,0 +1,96 @@
package yqlib
import (
"os"
"path/filepath"
"strings"
"testing"
)
// helper to build an ExpressionNode that just yields a fixed string for the file name
func parseFilenameExp(t *testing.T, exp string) *ExpressionNode {
t.Helper()
InitExpressionParser()
node, err := ExpressionParser.ParseExpression(exp)
if err != nil {
t.Fatalf("failed to parse split-exp test expression %q: %v", exp, err)
}
return node
}
func TestMultiPrinterWriterOverwriteDefault(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "out.yml")
if err := os.WriteFile(target, []byte("pre-existing\n"), 0600); err != nil {
t.Fatalf("setup: %v", err)
}
exp := parseFilenameExp(t, `"`+target+`"`)
pw := NewMultiPrinterWriter(exp, YamlFormat)
node := &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "hello"}
w, err := pw.GetWriter(node)
if err != nil {
t.Fatalf("default behaviour should silently overwrite, got error: %v", err)
}
if w == nil {
t.Fatalf("expected a writer, got nil")
}
// confirm the file was truncated/recreated by os.Create
info, err := os.Stat(target)
if err != nil {
t.Fatalf("stat target: %v", err)
}
if info.Size() != 0 {
t.Fatalf("expected file to be truncated (size 0) before writes, got %d bytes", info.Size())
}
}
func TestMultiPrinterWriterNoOverwriteRefusesExisting(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "out.yml")
if err := os.WriteFile(target, []byte("pre-existing\n"), 0600); err != nil {
t.Fatalf("setup: %v", err)
}
exp := parseFilenameExp(t, `"`+target+`"`)
pw := NewMultiPrinterWriterWithOptions(exp, YamlFormat, true)
node := &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "hello"}
_, err := pw.GetWriter(node)
if err == nil {
t.Fatalf("expected error when --no-overwrite is set and target exists, got nil")
}
if !strings.Contains(err.Error(), "refusing to overwrite") {
t.Fatalf("expected refusing-to-overwrite error message, got: %v", err)
}
// file must be untouched
data, err := os.ReadFile(target)
if err != nil {
t.Fatalf("read target: %v", err)
}
if string(data) != "pre-existing\n" {
t.Fatalf("file should be untouched, contents = %q", string(data))
}
}
func TestMultiPrinterWriterNoOverwriteCreatesNew(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "new.yml")
exp := parseFilenameExp(t, `"`+target+`"`)
pw := NewMultiPrinterWriterWithOptions(exp, YamlFormat, true)
node := &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "hello"}
w, err := pw.GetWriter(node)
if err != nil {
t.Fatalf("no-overwrite should still create new files, got: %v", err)
}
if w == nil {
t.Fatalf("expected a writer, got nil")
}
if _, err := os.Stat(target); err != nil {
t.Fatalf("expected new file to exist, stat err: %v", err)
}
}