mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
2 Commits
array-leng
...
risentvebe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
783f92ecc9 | ||
|
|
9e083be47f |
@@ -190,202 +190,6 @@ func TestReadArrayCmd(t *testing.T) {
|
|||||||
test.AssertResult(t, "b.e.[1].name: sam\n", result.Output)
|
test.AssertResult(t, "b.e.[1].name: sam\n", result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadArrayLengthCmd(t *testing.T) {
|
|
||||||
content := `- things
|
|
||||||
- whatever
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadArrayLengthDeepCmd(t *testing.T) {
|
|
||||||
content := `holder:
|
|
||||||
- things
|
|
||||||
- whatever
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadArrayLengthDeepMultipleCmd(t *testing.T) {
|
|
||||||
content := `holderA:
|
|
||||||
- things
|
|
||||||
- whatever
|
|
||||||
skipMe:
|
|
||||||
- yep
|
|
||||||
holderB:
|
|
||||||
- other things
|
|
||||||
- cool
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l -c %s holder*", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
content := `holderA:
|
|
||||||
- things
|
|
||||||
- whatever
|
|
||||||
holderB:
|
|
||||||
- other things
|
|
||||||
- cool
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s -ppv holder*", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "holderA: 2\nholderB: 2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadObjectLengthCmd(t *testing.T) {
|
|
||||||
content := `cat: meow
|
|
||||||
dog: bark
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadObjectLengthDeepCmd(t *testing.T) {
|
|
||||||
content := `holder:
|
|
||||||
cat: meow
|
|
||||||
dog: bark
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadObjectLengthDeepMultipleCmd(t *testing.T) {
|
|
||||||
content := `holderA:
|
|
||||||
cat: meow
|
|
||||||
dog: bark
|
|
||||||
holderB:
|
|
||||||
elephant: meow
|
|
||||||
zebra: bark
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l -c %s holder*", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadObjectLengthDeepMultipleWithPathsCmd(t *testing.T) {
|
|
||||||
content := `holderA:
|
|
||||||
cat: meow
|
|
||||||
dog: bark
|
|
||||||
holderB:
|
|
||||||
elephant: meow
|
|
||||||
zebra: bark
|
|
||||||
`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l -ppv %s holder*", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "holderA: 2\nholderB: 2\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadScalarLengthCmd(t *testing.T) {
|
|
||||||
content := `meow`
|
|
||||||
filename := test.WriteTempYamlFile(content)
|
|
||||||
defer test.RemoveTempYamlFile(filename)
|
|
||||||
|
|
||||||
cmd := getRootCommand()
|
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
|
|
||||||
if result.Error != nil {
|
|
||||||
t.Error(result.Error)
|
|
||||||
}
|
|
||||||
test.AssertResult(t, "4\n", result.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadDeepSplatCmd(t *testing.T) {
|
func TestReadDeepSplatCmd(t *testing.T) {
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**")
|
result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**")
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
|
|
||||||
var customTag = ""
|
var customTag = ""
|
||||||
var printMode = "v"
|
var printMode = "v"
|
||||||
var printLength = false
|
|
||||||
var collectIntoArray = false
|
|
||||||
var writeInplace = false
|
var writeInplace = false
|
||||||
var writeScript = ""
|
var writeScript = ""
|
||||||
var sourceYamlFile = ""
|
var sourceYamlFile = ""
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ yq r -- things.yaml '--key-starting-with-dashes.blah'
|
|||||||
cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||||
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(&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
|
||||||
}
|
}
|
||||||
|
|||||||
43
cmd/utils.go
43
cmd/utils.go
@@ -77,30 +77,6 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml.
|
|||||||
return append(originalMatchingNodes, matchingNodes...), nil
|
return append(originalMatchingNodes, matchingNodes...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func lengthOf(node *yaml.Node) int {
|
|
||||||
kindToCheck := node.Kind
|
|
||||||
if node.Kind == yaml.DocumentNode && len(node.Content) == 1 {
|
|
||||||
log.Debugf("length of document node, calculating length of child")
|
|
||||||
kindToCheck = node.Content[0].Kind
|
|
||||||
}
|
|
||||||
switch kindToCheck {
|
|
||||||
case yaml.ScalarNode:
|
|
||||||
return len(node.Value)
|
|
||||||
case yaml.MappingNode:
|
|
||||||
return len(node.Content) / 2
|
|
||||||
default:
|
|
||||||
return len(node.Content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// transforms node before printing, if required
|
|
||||||
func transformNode(node *yaml.Node) *yaml.Node {
|
|
||||||
if printLength {
|
|
||||||
return &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", lengthOf(node))}
|
|
||||||
}
|
|
||||||
return node
|
|
||||||
}
|
|
||||||
|
|
||||||
func printNode(node *yaml.Node, writer io.Writer) error {
|
func printNode(node *yaml.Node, writer io.Writer) error {
|
||||||
var encoder yqlib.Encoder
|
var encoder yqlib.Encoder
|
||||||
if outputToJSON {
|
if outputToJSON {
|
||||||
@@ -176,9 +152,6 @@ 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":
|
||||||
@@ -191,27 +164,17 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
|||||||
var parentNode = yaml.Node{Kind: yaml.MappingNode}
|
var parentNode = yaml.Node{Kind: yaml.MappingNode}
|
||||||
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] = mappedDoc.Node
|
||||||
if collectIntoArray {
|
if err := printNode(&parentNode, bufferedWriter); err != nil {
|
||||||
arrayCollection.Content = append(arrayCollection.Content, &parentNode)
|
|
||||||
} else if err := printNode(&parentNode, bufferedWriter); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if collectIntoArray {
|
if err := printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user