Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create parent directories if --split-exp is used. #2231

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion acceptance_tests/split-printer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

setUp() {
rm test*.yml || true
rm -rf test_dir* || true
}

testBasicSplitWithName() {
Expand Down Expand Up @@ -204,4 +205,23 @@ EOM
assertEquals "$expectedDoc3" "$doc3"
}

source ./scripts/shunit2
testSplitWithDirectories() {
cat >test.yml <<EOL
f: test_dir1/file1
---
f: test_dir2/dir22/file2
---
f: file3
EOL

./yq e --no-doc -s ".f" test.yml

doc1=$(cat test_dir1/file1.yml)
assertEquals "f: test_dir1/file1" "$doc1"
doc2=$(cat test_dir2/dir22/file2.yml)
assertEquals "f: test_dir2/dir22/file2" "$doc2"
doc3=$(cat file3.yml)
assertEquals "f: file3" "$doc3"
}

source ./scripts/shunit2
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,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().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.")
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 {
panic(err)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/yqlib/printer_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
)

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

err = os.MkdirAll(filepath.Dir(name), 0750)
if err != nil {
return nil, err
}
f, err := os.Create(name)

if err != nil {
Expand Down
Loading