mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-23 14:16:10 +00:00
Refactored doc generation, add fi fileIndex alias
This commit is contained in:
parent
5aff50a345
commit
04491e13c3
@ -1,9 +1,11 @@
|
|||||||
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).
|
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).
|
||||||
|
|
||||||
|
Note that the `fileIndex` operator has a short alias of `fi`.
|
||||||
|
|
||||||
## Merging files
|
## Merging files
|
||||||
Note the use of eval-all to ensure all documents are loaded into memory.
|
Note the use of eval-all to ensure all documents are loaded into memory.
|
||||||
```bash
|
```bash
|
||||||
yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
|
yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
|
||||||
```
|
```
|
||||||
## Get filename
|
## Get filename
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
@ -33,3 +35,17 @@ will output
|
|||||||
0
|
0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Get file index alias
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a: cat
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval 'fi' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
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).
|
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).
|
||||||
|
|
||||||
|
Note that the `fileIndex` operator has a short alias of `fi`.
|
||||||
|
|
||||||
## Merging files
|
## Merging files
|
||||||
Note the use of eval-all to ensure all documents are loaded into memory.
|
Note the use of eval-all to ensure all documents are loaded into memory.
|
||||||
```bash
|
```bash
|
||||||
yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
|
yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
|
||||||
```
|
```
|
@ -21,6 +21,14 @@ var fileOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!int)::0\n",
|
"D0, P[], (!!int)::0\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Get file index alias",
|
||||||
|
document: `{a: cat}`,
|
||||||
|
expression: `fi`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!int)::0\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileOperatorsScenarios(t *testing.T) {
|
func TestFileOperatorsScenarios(t *testing.T) {
|
||||||
|
@ -137,13 +137,28 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
|
|
||||||
for _, s := range scenarios {
|
for _, s := range scenarios {
|
||||||
if !s.skipDoc {
|
if !s.skipDoc {
|
||||||
|
documentScenario(t, w, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentScenario(t *testing.T, w *bufio.Writer, s expressionScenario) {
|
||||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||||
|
|
||||||
if s.subdescription != "" {
|
if s.subdescription != "" {
|
||||||
writeOrPanic(w, s.subdescription)
|
writeOrPanic(w, s.subdescription)
|
||||||
writeOrPanic(w, "\n\n")
|
writeOrPanic(w, "\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
formattedDoc, formattedDoc2 := documentInput(w, s)
|
||||||
|
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
documentOutput(t, w, s, formattedDoc, formattedDoc2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
|
||||||
formattedDoc := ""
|
formattedDoc := ""
|
||||||
formattedDoc2 := ""
|
formattedDoc2 := ""
|
||||||
command := "eval"
|
command := "eval"
|
||||||
@ -182,9 +197,10 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
writeOrPanic(w, "Running\n")
|
writeOrPanic(w, "Running\n")
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v --null-input '%v'\n```\n", command, s.expression))
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v --null-input '%v'\n```\n", command, s.expression))
|
||||||
}
|
}
|
||||||
|
return formattedDoc, formattedDoc2
|
||||||
|
}
|
||||||
|
|
||||||
writeOrPanic(w, "will output\n")
|
func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formattedDoc string, formattedDoc2 string) {
|
||||||
|
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
|
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
|
||||||
@ -233,9 +249,4 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
w.Flush()
|
|
||||||
}
|
}
|
||||||
|
@ -200,6 +200,7 @@ func initLexer() (*lex.Lexer, error) {
|
|||||||
lexer.Add([]byte(`alias`), opAssignableToken(GetAlias, AssignAlias))
|
lexer.Add([]byte(`alias`), opAssignableToken(GetAlias, AssignAlias))
|
||||||
lexer.Add([]byte(`filename`), opToken(GetFilename))
|
lexer.Add([]byte(`filename`), opToken(GetFilename))
|
||||||
lexer.Add([]byte(`fileIndex`), opToken(GetFileIndex))
|
lexer.Add([]byte(`fileIndex`), opToken(GetFileIndex))
|
||||||
|
lexer.Add([]byte(`fi`), opToken(GetFileIndex))
|
||||||
lexer.Add([]byte(`path`), opToken(GetPath))
|
lexer.Add([]byte(`path`), opToken(GetPath))
|
||||||
|
|
||||||
lexer.Add([]byte(`lineComment`), opTokenWithPrefs(GetComment, AssignComment, &CommentOpPreferences{LineComment: true}))
|
lexer.Add([]byte(`lineComment`), opTokenWithPrefs(GetComment, AssignComment, &CommentOpPreferences{LineComment: true}))
|
||||||
|
Loading…
Reference in New Issue
Block a user