diff --git a/pkg/yqlib/doc/File Operators.md b/pkg/yqlib/doc/File Operators.md index 9ecc52fd..bf122b63 100644 --- a/pkg/yqlib/doc/File Operators.md +++ b/pkg/yqlib/doc/File Operators.md @@ -1,13 +1,13 @@ -The file operator is used to filter based on filename. This is most often used with merge when needing to merge specific files together. +File operators are most often used with merge when needing to merge specific files together. Note that when doing this, you will need to use `eval-all` to ensure all yaml documents are loaded into memory before performing the merge (as opposed to `eval` which runs the expression once per document). ```bash -yq eval 'filename == "file1.yaml" * fileIndex == 0' file1.yaml file2.yaml +yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml ``` ## Examples ### Get filename Given a sample.yml file of: ```yaml -'': null +a: cat ``` then ```bash @@ -21,7 +21,7 @@ sample.yaml ### Get file index Given a sample.yml file of: ```yaml -'': null +a: cat ``` then ```bash diff --git a/pkg/yqlib/doc/headers/File Operators.md b/pkg/yqlib/doc/headers/File Operators.md index 6f836113..2efc91d8 100644 --- a/pkg/yqlib/doc/headers/File Operators.md +++ b/pkg/yqlib/doc/headers/File Operators.md @@ -1,5 +1,5 @@ -The file operator is used to filter based on filename. This is most often used with merge when needing to merge specific files together. +File operators are most often used with merge when needing to merge specific files together. Note that when doing this, you will need to use `eval-all` to ensure all yaml documents are loaded into memory before performing the merge (as opposed to `eval` which runs the expression once per document). ```bash -yq eval 'filename == "file1.yaml" * fileIndex == 0' file1.yaml file2.yaml +yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml ``` \ No newline at end of file diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 4cd7b597..b2ea9350 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -18,8 +18,6 @@ type OperationType struct { // operators TODO: // - get path operator (like doc index) -// - get file index op (like doc index) -// - get file name op (like doc index) // - write in place // - mergeAppend (merges and appends arrays) // - mergeEmpty (sets only if the document is empty, do I do that now?) diff --git a/pkg/yqlib/operator_file_test.go b/pkg/yqlib/operator_file_test.go index 7117426c..a4735cef 100644 --- a/pkg/yqlib/operator_file_test.go +++ b/pkg/yqlib/operator_file_test.go @@ -7,7 +7,7 @@ import ( var fileOperatorScenarios = []expressionScenario{ { description: "Get filename", - document: `{}`, + document: `{a: cat}`, expression: `filename`, expected: []string{ "D0, P[], (!!str)::sample.yml\n", @@ -15,7 +15,7 @@ var fileOperatorScenarios = []expressionScenario{ }, { description: "Get file index", - document: `{}`, + document: `{a: cat}`, expression: `fileIndex`, expected: []string{ "D0, P[], (!!int)::0\n",