mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Array length and collect
This commit is contained in:
parent
6ef04e1e77
commit
8e6ceba2ac
@ -225,6 +225,8 @@ func TestReadArrayLengthDeepMultipleCmd(t *testing.T) {
|
|||||||
content := `holderA:
|
content := `holderA:
|
||||||
- things
|
- things
|
||||||
- whatever
|
- whatever
|
||||||
|
skipMe:
|
||||||
|
- yep
|
||||||
holderB:
|
holderB:
|
||||||
- other things
|
- other things
|
||||||
- cool
|
- cool
|
||||||
@ -233,11 +235,54 @@ holderB:
|
|||||||
defer test.RemoveTempYamlFile(filename)
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder*", filename))
|
result := test.RunCmd(cmd, fmt.Sprintf("read -l -c %s holder*", filename))
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
test.AssertResult(t, "4\n", result.Output)
|
test.AssertResult(t, "2\n", result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadCollectCmd(t *testing.T) {
|
||||||
|
content := `holderA: yep
|
||||||
|
skipMe: not me
|
||||||
|
holderB: me too
|
||||||
|
`
|
||||||
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := test.RunCmd(cmd, fmt.Sprintf("read -c %s holder*", filename))
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
expectedOutput := `- yep
|
||||||
|
- me too
|
||||||
|
`
|
||||||
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadCollectArrayCmd(t *testing.T) {
|
||||||
|
content := `- name: fred
|
||||||
|
value: 32
|
||||||
|
- name: sam
|
||||||
|
value: 67
|
||||||
|
- name: fernie
|
||||||
|
value: 103
|
||||||
|
`
|
||||||
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := test.RunCmd(cmd, fmt.Sprintf("read -c %s (name==f*)", filename))
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
expectedOutput := `- name: fred
|
||||||
|
value: 32
|
||||||
|
- name: fernie
|
||||||
|
value: 103
|
||||||
|
`
|
||||||
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadArrayLengthDeepMultipleWithPathCmd(t *testing.T) {
|
func TestReadArrayLengthDeepMultipleWithPathCmd(t *testing.T) {
|
||||||
@ -302,11 +347,11 @@ holderB:
|
|||||||
defer test.RemoveTempYamlFile(filename)
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder*", filename))
|
result := test.RunCmd(cmd, fmt.Sprintf("read -l -c %s holder*", filename))
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
test.AssertResult(t, "4\n", result.Output)
|
test.AssertResult(t, "2\n", result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadObjectLengthDeepMultipleWithPathsCmd(t *testing.T) {
|
func TestReadObjectLengthDeepMultipleWithPathsCmd(t *testing.T) {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
var customTag = ""
|
var customTag = ""
|
||||||
var printMode = "v"
|
var printMode = "v"
|
||||||
var printLength = false
|
var printLength = false
|
||||||
|
var collectIntoArray = false
|
||||||
var writeInplace = false
|
var writeInplace = false
|
||||||
var writeScript = ""
|
var writeScript = ""
|
||||||
var sourceYamlFile = ""
|
var sourceYamlFile = ""
|
||||||
|
@ -27,6 +27,7 @@ yq r -- things.yaml '--key-starting-with-dashes.blah'
|
|||||||
cmdRead.PersistentFlags().StringVarP(&printMode, "printMode", "p", "v", "print mode (v (values, default), p (paths), pv (path and value pairs)")
|
cmdRead.PersistentFlags().StringVarP(&printMode, "printMode", "p", "v", "print mode (v (values, default), p (paths), pv (path and value pairs)")
|
||||||
cmdRead.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
|
cmdRead.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
|
||||||
cmdRead.PersistentFlags().BoolVarP(&printLength, "length", "l", false, "print length of results")
|
cmdRead.PersistentFlags().BoolVarP(&printLength, "length", "l", false, "print length of results")
|
||||||
|
cmdRead.PersistentFlags().BoolVarP(&collectIntoArray, "collect", "c", false, "collect results into array")
|
||||||
cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors")
|
cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors")
|
||||||
return cmdRead
|
return cmdRead
|
||||||
}
|
}
|
||||||
|
17
cmd/utils.go
17
cmd/utils.go
@ -176,6 +176,9 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var errorWriting error
|
var errorWriting error
|
||||||
|
|
||||||
|
var arrayCollection = yaml.Node{Kind: yaml.SequenceNode}
|
||||||
|
|
||||||
for _, mappedDoc := range matchingNodes {
|
for _, mappedDoc := range matchingNodes {
|
||||||
switch printMode {
|
switch printMode {
|
||||||
case "p":
|
case "p":
|
||||||
@ -189,16 +192,26 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
|||||||
parentNode.Content = make([]*yaml.Node, 2)
|
parentNode.Content = make([]*yaml.Node, 2)
|
||||||
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
|
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
|
||||||
parentNode.Content[1] = transformNode(mappedDoc.Node)
|
parentNode.Content[1] = transformNode(mappedDoc.Node)
|
||||||
if err := printNode(&parentNode, bufferedWriter); err != nil {
|
if collectIntoArray {
|
||||||
|
arrayCollection.Content = append(arrayCollection.Content, &parentNode)
|
||||||
|
} else if err := printNode(&parentNode, bufferedWriter); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if err := printNode(transformNode(mappedDoc.Node), bufferedWriter); err != nil {
|
if collectIntoArray {
|
||||||
|
arrayCollection.Content = append(arrayCollection.Content, mappedDoc.Node)
|
||||||
|
} else if err := printNode(transformNode(mappedDoc.Node), bufferedWriter); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if collectIntoArray {
|
||||||
|
if err := printNode(transformNode(&arrayCollection), bufferedWriter); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user