Skip to content

Commit dfd396b

Browse files
committed
When split expression includes an extension, dont add .yml automatically
1 parent f63d76f commit dfd396b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ yq*.snap
4141

4242
test.yml
4343
test*.yml
44+
test*.yaml
4445
0.yml
4546
1.yml
4647
2.yml

acceptance_tests/split-printer.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ EOM
2525
assertEquals "$expectedDoc2" "$doc2"
2626
}
2727

28+
testBasicSplitWithNameCustomExtension() {
29+
rm test*.yaml || true
30+
cat >test.yml <<EOL
31+
a: test_doc1
32+
---
33+
a: test_doc2
34+
EOL
35+
36+
./yq e test.yml -s '.a + ".yaml"'
37+
38+
doc1=$(cat test_doc1.yaml)
39+
40+
assertEquals "a: test_doc1" "$doc1"
41+
42+
doc2=$(cat test_doc2.yaml)
43+
read -r -d '' expectedDoc2 << EOM
44+
---
45+
a: test_doc2
46+
EOM
47+
assertEquals "$expectedDoc2" "$doc2"
48+
}
49+
50+
51+
52+
2853
testSplitFromFile() {
2954
cat >test.yml <<EOL
3055
a: test_doc1

pkg/yqlib/printer_writer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"regexp"
89

910
"gopkg.in/yaml.v3"
1011
)
@@ -67,7 +68,10 @@ func (sp *multiPrintWriter) GetWriter(node *CandidateNode) (*bufio.Writer, error
6768
if result.MatchingNodes.Len() > 0 {
6869
name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
6970
}
70-
name = fmt.Sprintf("%v.%v", name, sp.extension)
71+
var extensionRegexp = regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
72+
if !extensionRegexp.MatchString(name) {
73+
name = fmt.Sprintf("%v.%v", name, sp.extension)
74+
}
7175

7276
f, err := os.Create(name)
7377

0 commit comments

Comments
 (0)