Skip to content

Commit

Permalink
Can specify a split expression file #1194
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Apr 29, 2022
1 parent 67c79b7 commit 55c5497
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions acceptance_tests/split-printer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ EOM
assertEquals "$expectedDoc2" "$doc2"
}

testSplitFromFile() {
cat >test.yml <<EOL
a: test_doc1
---
a: test_doc2
EOL

cat >test_splitExp.yml <<EOL
.a
EOL

./yq test.yml --split-exp-file test_splitExp.yml

doc1=$(cat test_doc1.yml)

assertEquals "a: test_doc1" "$doc1"

doc2=$(cat test_doc2.yml)
read -r -d '' expectedDoc2 << EOM
---
a: test_doc2
EOM
assertEquals "$expectedDoc2" "$doc2"
}

testBasicSplitWithNameEvalAll() {
cat >test.yml <<EOL
a: test_doc1
Expand Down
1 change: 1 addition & 0 deletions cmd/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var prettyPrint = false
var frontMatter = ""

var splitFileExp = ""
var splitFileExpFile = ""

var completedSuccessfully = false

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ yq -P sample.json
rootCmd.PersistentFlags().BoolVarP(&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(&splitFileExpFile, "split-exp-file", "", "", "Use a file to specify the split-exp expression.")

rootCmd.PersistentFlags().StringVarP(&expressionFile, "from-file", "", "", "Load expression from specified file.")

Expand Down
8 changes: 8 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
return "", nil, err
}

if splitFileExpFile != "" {
splitExpressionBytes, err := os.ReadFile(splitFileExpFile)
if err != nil {
return "", nil, err
}
splitFileExp = string(splitExpressionBytes)
}

// backwards compatibility
if outputToJSON {
outputFormat = "json"
Expand Down

0 comments on commit 55c5497

Please sign in to comment.