1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Compare commits

..

3 Commits

Author SHA1 Message Date
Mike Farah
bc8ee03911 Array length and collect 2020-02-28 12:18:27 +11:00
Mike Farah
98fef5cbe2 wip 2020-02-28 11:37:27 +11:00
Mike Farah
9e8a60b48a wip 2020-02-28 11:30:16 +11:00
10 changed files with 10 additions and 213 deletions

View File

@@ -79,10 +79,8 @@ yq() {
## Features
- Written in portable go, so you can download a lovely dependency free binary
- [Colorize the output](https://mikefarah.gitbook.io/yq/usage/output-format#colorize-output)
- [Deep read a yaml file with a given path expression](https://mikefarah.gitbook.io/yq/commands/read#basic)
- [List matching paths of a given path expression](https://mikefarah.gitbook.io/yq/commands/read#path-only)
- [Return the lengths of arrays/object/scalars](https://mikefarah.gitbook.io/yq/commands/read#printing-length-of-the-results)
- Update a yaml file given a [path expression](https://mikefarah.gitbook.io/yq/commands/write-update#basic) or [script file](https://mikefarah.gitbook.io/yq/commands/write-update#basic)
- Update creates any missing entries in the path on the fly
- Deeply [compare](https://mikefarah.gitbook.io/yq/commands/compare) yaml files
@@ -116,7 +114,6 @@ Available Commands:
write yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml 'b.e(name==fr*).value' newValue
Flags:
-C, --colors print using colors
-h, --help help for yq
-I, --indent int sets indent level for output (default 2)
-P, --prettyPrint pretty print

View File

@@ -1,7 +1,5 @@
name: 'yq - portable yaml processor'
description: 'create, read, update, delete, merge, validate and do more with yaml'
icon: command
color: gray-dark
name: 'YAML processor'
description: 'YAML processor for running in Github action'
inputs:
cmd:
description: 'The Command which should be run'

View File

@@ -94,21 +94,12 @@ func TestReadCmd(t *testing.T) {
test.AssertResult(t, "2\n", result.Output)
}
func TestCompareSameCmd(t *testing.T) {
func TestCompareCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data1.yaml")
result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data3.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
func TestCompareDifferentCmd(t *testing.T) {
forceOsExit = false
cmd := getRootCommand()
result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data3.yaml")
expectedOutput := `-a: simple # just the best
-b: [1, 2]
+a: "simple" # just the best
@@ -120,7 +111,6 @@ func TestCompareDifferentCmd(t *testing.T) {
}
func TestComparePrettyCmd(t *testing.T) {
forceOsExit = false
cmd := getRootCommand()
result := test.RunCmd(cmd, "compare -P ../examples/data1.yaml ../examples/data3.yaml")
if result.Error != nil {
@@ -138,7 +128,6 @@ func TestComparePrettyCmd(t *testing.T) {
}
func TestComparePathsCmd(t *testing.T) {
forceOsExit = false
cmd := getRootCommand()
result := test.RunCmd(cmd, "compare -P -ppv ../examples/data1.yaml ../examples/data3.yaml **")
if result.Error != nil {
@@ -397,97 +386,6 @@ func TestReadScalarLengthCmd(t *testing.T) {
test.AssertResult(t, "4\n", result.Output)
}
func TestReadDoubleQuotedStringCmd(t *testing.T) {
content := `name: "meow face"`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "meow face\n", result.Output)
}
func TestReadSingleQuotedStringCmd(t *testing.T) {
content := `name: 'meow face'`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "meow face\n", result.Output)
}
func TestReadQuotedMultinlineStringCmd(t *testing.T) {
content := `test: |
abcdefg
hijklmno
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s test", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `abcdefg
hijklmno
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadQuotedMultinlineNoNewLineStringCmd(t *testing.T) {
content := `test: |-
abcdefg
hijklmno
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s test", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `abcdefg
hijklmno
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadBooleanCmd(t *testing.T) {
content := `name: true`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "true\n", result.Output)
}
func TestReadNumberCmd(t *testing.T) {
content := `name: 32.13`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "32.13\n", result.Output)
}
func TestReadDeepSplatCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**")
@@ -2107,75 +2005,6 @@ func TestDeleteYamlArrayCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadExpression(t *testing.T) {
content := `name: value`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("r %s (x==f)", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadFindValueArrayCmd(t *testing.T) {
content := `- cat
- dog
- rat
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("r %s (.==dog)", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `dog
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadFindValueDeepArrayCmd(t *testing.T) {
content := `animals:
- cat
- dog
- rat
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("r %s animals(.==dog)", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `dog
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadFindValueDeepObjectCmd(t *testing.T) {
content := `animals:
great: yes
small: sometimes
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("r %s animals(.==yes) -ppv", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `animals.great: yes
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlArrayExpressionCmd(t *testing.T) {
content := `- name: fred
- name: cat

View File

@@ -3,7 +3,6 @@ package cmd
import (
"bufio"
"bytes"
"os"
"strings"
"github.com/kylelemons/godebug/diff"
@@ -12,9 +11,6 @@ import (
"github.com/spf13/cobra"
)
// turn off for unit tests :(
var forceOsExit = true
func createCompareCmd() *cobra.Command {
var cmdCompare = &cobra.Command{
Use: "compare [yaml_file_a] [yaml_file_b]",
@@ -74,14 +70,7 @@ func compareDocuments(cmd *cobra.Command, args []string) error {
return errorDoingThings
}
diffString := diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n"))
if len(diffString) > 1 {
cmd.Print(diffString)
cmd.Print(diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n")))
cmd.Print("\n")
if forceOsExit {
os.Exit(1)
}
}
return nil
}

View File

@@ -43,7 +43,7 @@ func New() *cobra.Command {
rootCmd.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print")
rootCmd.PersistentFlags().IntVarP(&indent, "indent", "I", 2, "sets indent level for output")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit")
rootCmd.PersistentFlags().BoolVarP(&colorsEnabled, "colors", "C", false, "print with colors")
rootCmd.PersistentFlags().BoolVarP(&colorsEnabled, "colorsEnabled", "C", false, "enable colors")
rootCmd.AddCommand(
createReadCmd(),

View File

@@ -103,9 +103,6 @@ func transformNode(node *yaml.Node) *yaml.Node {
func printNode(node *yaml.Node, writer io.Writer) error {
var encoder yqlib.Encoder
if node.Kind == yaml.ScalarNode {
return writeString(writer, node.Value+"\n")
}
if outputToJSON {
encoder = yqlib.NewJsonEncoder(writer, prettyPrint, indent)
} else {

View File

@@ -11,7 +11,7 @@ var (
GitDescribe string
// Version is main version number that is being run at the moment.
Version = "3.2.1"
Version = "3.1.2"
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"strconv"
"strings"
yaml "gopkg.in/yaml.v3"
)
type PathParser interface {
@@ -47,7 +45,7 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str
}
var headString = fmt.Sprintf("%v", head)
if strings.Contains(headString, "==") && nodeContext.Node.Kind != yaml.ScalarNode {
if strings.Contains(headString, "==") {
log.Debug("ooh deep recursion time")
result := strings.SplitN(headString, "==", 2)
path := strings.TrimSpace(result[0])
@@ -65,14 +63,6 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str
}
log.Debug("done deep recursing, found %v matches", len(navigationStrategy.GetVisitedNodes()))
return len(navigationStrategy.GetVisitedNodes()) > 0
} else if strings.Contains(headString, "==") && nodeContext.Node.Kind == yaml.ScalarNode {
result := strings.SplitN(headString, "==", 2)
path := strings.TrimSpace(result[0])
value := strings.TrimSpace(result[1])
if path == "." {
log.Debug("need to match scalar")
return matchesString(value, nodeContext.Node.Value)
}
}
if head == "+" {

View File

@@ -7,6 +7,3 @@ gox -ldflags "${LDFLAGS}" -output="build/yq_{{.OS}}_{{.Arch}}"
# include non-default linux builds too
gox -ldflags "${LDFLAGS}" -os=linux -output="build/yq_{{.OS}}_{{.Arch}}"
cd build
rhash -r -a . -P -o checksums

View File

@@ -1,5 +1,5 @@
name: yq
version: '3.2.1'
version: '3.1.2'
summary: A lightweight and portable command-line YAML processor
description: |
The aim of the project is to be the jq or sed of yaml files.