When split expression includes an extension, dont add .yml automatically

This commit is contained in:
Mike Farah 2022-06-23 14:31:04 +10:00
parent f63d76f006
commit dfd396b480
3 changed files with 31 additions and 1 deletions

1
.gitignore vendored
View File

@ -41,6 +41,7 @@ yq*.snap
test.yml test.yml
test*.yml test*.yml
test*.yaml
0.yml 0.yml
1.yml 1.yml
2.yml 2.yml

View File

@ -25,6 +25,31 @@ EOM
assertEquals "$expectedDoc2" "$doc2" assertEquals "$expectedDoc2" "$doc2"
} }
testBasicSplitWithNameCustomExtension() {
rm test*.yaml || true
cat >test.yml <<EOL
a: test_doc1
---
a: test_doc2
EOL
./yq e test.yml -s '.a + ".yaml"'
doc1=$(cat test_doc1.yaml)
assertEquals "a: test_doc1" "$doc1"
doc2=$(cat test_doc2.yaml)
read -r -d '' expectedDoc2 << EOM
---
a: test_doc2
EOM
assertEquals "$expectedDoc2" "$doc2"
}
testSplitFromFile() { testSplitFromFile() {
cat >test.yml <<EOL cat >test.yml <<EOL
a: test_doc1 a: test_doc1

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"regexp"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -67,7 +68,10 @@ func (sp *multiPrintWriter) GetWriter(node *CandidateNode) (*bufio.Writer, error
if result.MatchingNodes.Len() > 0 { if result.MatchingNodes.Len() > 0 {
name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
} }
var extensionRegexp = regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
if !extensionRegexp.MatchString(name) {
name = fmt.Sprintf("%v.%v", name, sp.extension) name = fmt.Sprintf("%v.%v", name, sp.extension)
}
f, err := os.Create(name) f, err := os.Create(name)