Skip to content

Commit f73c862

Browse files
rudo-thomasmikefarah
authored andcommitted
feat: Create parent directories if --split-exp is used.
Problem: When --split-exp is used and produces filenames with slashes in them, the target directories must already exist otherwise yq fails. Fix/feature: Create the necessary directories with os.MkdirAll(). The permissions 0750 were chosen to satisfy the vulnerability checker.
1 parent 294a170 commit f73c862

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

acceptance_tests/split-printer.sh

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
setUp() {
44
rm test*.yml || true
5+
rm -rf test_dir* || true
56
}
67

78
testBasicSplitWithName() {
@@ -204,4 +205,23 @@ EOM
204205
assertEquals "$expectedDoc3" "$doc3"
205206
}
206207

207-
source ./scripts/shunit2
208+
testSplitWithDirectories() {
209+
cat >test.yml <<EOL
210+
f: test_dir1/file1
211+
---
212+
f: test_dir2/dir22/file2
213+
---
214+
f: file3
215+
EOL
216+
217+
./yq e --no-doc -s ".f" test.yml
218+
219+
doc1=$(cat test_dir1/file1.yml)
220+
assertEquals "f: test_dir1/file1" "$doc1"
221+
doc2=$(cat test_dir2/dir22/file2.yml)
222+
assertEquals "f: test_dir2/dir22/file2" "$doc2"
223+
doc3=$(cat file3.yml)
224+
assertEquals "f: file3" "$doc3"
225+
}
226+
227+
source ./scripts/shunit2

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ yq -P -oy sample.json
197197
}
198198
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
199199

200-
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.")
200+
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.")
201201
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {
202202
panic(err)
203203
}

pkg/yqlib/printer_writer.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"path/filepath"
89
"regexp"
910
)
1011

@@ -70,6 +71,10 @@ func (sp *multiPrintWriter) GetWriter(node *CandidateNode) (*bufio.Writer, error
7071
name = fmt.Sprintf("%v.%v", name, sp.extension)
7172
}
7273

74+
err = os.MkdirAll(filepath.Dir(name), 0750)
75+
if err != nil {
76+
return nil, err
77+
}
7378
f, err := os.Create(name)
7479

7580
if err != nil {

0 commit comments

Comments
 (0)