mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
1 Commits
v4
...
4.0.0-beta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12edcafd8c |
58
README.md
58
README.md
@@ -108,18 +108,21 @@ Supported by @rmescandon (https://launchpad.net/~rmescandon/+archive/ubuntu/yq)
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
- Written in portable go, so you can download a lovely dependency free binary
|
- Written in portable go, so you can download a lovely dependency free binary
|
||||||
- Uses similar syntax as `jq` but works with YAML and JSON files
|
|
||||||
- Fully supports multi document yaml files
|
|
||||||
- Colorized yaml output
|
- Colorized yaml output
|
||||||
- [Deeply traverse yaml](https://mikefarah.gitbook.io/yq/v/v4.x/traverse)
|
- [Deep read a yaml file with a given path expression](https://mikefarah.gitbook.io/yq/v/v4.x/traverse)
|
||||||
- [Sort yaml by keys](https://mikefarah.gitbook.io/yq/v/v4.x/sort-keys)
|
- [Return the lengths of arrays/object/scalars](https://mikefarah.gitbook.io/yq/commands/read#printing-length-of-the-results)
|
||||||
- Manipulate yaml [comments](https://app.gitbook.com/@mikefarah/s/yq/v/v4.x/comment-operators), [styling](https://app.gitbook.com/@mikefarah/s/yq/v/v4.x/style), [tags](https://app.gitbook.com/@mikefarah/s/yq/v/v4.x/tag) and [anchors](https://app.gitbook.com/@mikefarah/s/yq/v/v4.x/explode).
|
- 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 yaml inplace](https://mikefarah.gitbook.io/yq/v/v4.x/commands/evaluate#flags)
|
- Update creates any missing entries in the path on the fly
|
||||||
- [Complex expressions to select and update](https://mikefarah.gitbook.io/yq/v/v4.x/select#select-and-update-matching-values-in-map)
|
- Deeply [compare](https://mikefarah.gitbook.io/yq/commands/compare) yaml files
|
||||||
- Keeps yaml formatting and comments when updating (though there are issues with whitespace)
|
- Keeps yaml formatting and comments when updating
|
||||||
- [Convert to/from json to yaml](https://mikefarah.gitbook.io/yq/v/v4.x/usage/convert)
|
- [Validate a yaml file](https://mikefarah.gitbook.io/yq/commands/validate)
|
||||||
- [Pipe data in by using '-'](https://mikefarah.gitbook.io/yq/v/v4.x/commands/evaluate)
|
- Create a yaml file given a [deep path and value](https://mikefarah.gitbook.io/yq/commands/create#creating-a-simple-yaml-file) or a [script file](https://mikefarah.gitbook.io/yq/commands/create#creating-using-a-create-script)
|
||||||
- General shell completion scripts (bash/zsh/fish/powershell) (https://mikefarah.gitbook.io/yq/v/v4.x/commands/shell-completion)
|
- [Prefix a path to a yaml file](https://mikefarah.gitbook.io/yq/commands/prefix)
|
||||||
|
- [Convert to/from json to yaml](https://mikefarah.gitbook.io/yq/usage/convert)
|
||||||
|
- [Pipe data in by using '-'](https://mikefarah.gitbook.io/yq/commands/read#from-stdin)
|
||||||
|
- [Merge](https://mikefarah.gitbook.io/yq/commands/merge) multiple yaml files with various options for [overriding](https://mikefarah.gitbook.io/yq/commands/merge#overwrite-values) and [appending](https://mikefarah.gitbook.io/yq/commands/merge#append-values-with-arrays)
|
||||||
|
- Supports multiple documents in a single yaml file for [reading](https://mikefarah.gitbook.io/yq/commands/read#multiple-documents), [writing](https://mikefarah.gitbook.io/yq/commands/write-update#multiple-documents) and [merging](https://mikefarah.gitbook.io/yq/commands/merge#multiple-documents)
|
||||||
|
- General shell completion scripts (bash/zsh/fish/powershell) (https://mikefarah.gitbook.io/yq/commands/shell-completion)
|
||||||
|
|
||||||
## [Usage](https://mikefarah.gitbook.io/yq/)
|
## [Usage](https://mikefarah.gitbook.io/yq/)
|
||||||
|
|
||||||
@@ -131,35 +134,32 @@ Usage:
|
|||||||
yq [command]
|
yq [command]
|
||||||
|
|
||||||
Available Commands:
|
Available Commands:
|
||||||
eval Apply expression to each document in each yaml file given in sequence
|
compare yq x [--prettyPrint/-P] dataA.yaml dataB.yaml 'b.e(name==fr*).value'
|
||||||
eval-all Loads _all_ yaml documents of _all_ yaml files and runs expression once
|
delete yq d [--inplace/-i] [--doc/-d index] sample.yaml 'b.e(name==fred)'
|
||||||
help Help about any command
|
help Help about any command
|
||||||
shell-completion Generate completion script
|
merge yq m [--inplace/-i] [--doc/-d index] [--overwrite/-x] [--append/-a] sample.yaml sample2.yaml
|
||||||
|
new yq n [--script/-s script_file] a.b.c newValue
|
||||||
|
prefix yq p [--inplace/-i] [--doc/-d index] sample.yaml a.b.c
|
||||||
|
read yq r [--printMode/-p pv] sample.yaml 'b.e(name==fr*).value'
|
||||||
|
shell-completion Generates shell completion scripts
|
||||||
|
validate yq v sample.yaml
|
||||||
|
write yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml 'b.e(name==fr*).value' newValue
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-C, --colors force print with colors
|
-C, --colors print with colors
|
||||||
-e, --exit-status set exit status if there are no matches or null or false is returned
|
|
||||||
-h, --help help for yq
|
-h, --help help for yq
|
||||||
-I, --indent int sets indent level for output (default 2)
|
-I, --indent int sets indent level for output (default 2)
|
||||||
-i, --inplace update the yaml file inplace of first yaml file given.
|
-P, --prettyPrint pretty print
|
||||||
-M, --no-colors force print with no colors
|
-j, --tojson output as json. By default it prints a json document in one line, use the prettyPrint flag to print a formatted doc.
|
||||||
-N, --no-doc Don't print document separators (---)
|
|
||||||
-n, --null-input Don't read input, simply evaluate the expression given. Useful for creating yaml docs from scratch.
|
|
||||||
-j, --tojson output as json. Set indent to 0 to print json in one line.
|
|
||||||
-v, --verbose verbose mode
|
-v, --verbose verbose mode
|
||||||
-V, --version Print version information and quit
|
-V, --version Print version information and quit
|
||||||
|
|
||||||
Use "yq [command] --help" for more information about a command.
|
Use "yq [command] --help" for more information about a command.
|
||||||
```
|
```
|
||||||
|
|
||||||
Simple Example:
|
## Upgrade from V2
|
||||||
|
If you've been using v2 and want/need to upgrade, checkout the [upgrade guide](https://mikefarah.gitbook.io/yq/upgrading-from-v2).
|
||||||
```bash
|
|
||||||
yq e '.a.b | length' f1.yml f2.yml
|
|
||||||
```
|
|
||||||
|
|
||||||
## Upgrade from V3
|
|
||||||
If you've been using v3 and want/need to upgrade, checkout the [upgrade guide](https://mikefarah.gitbook.io/yq/v/v4.x/upgrading-from-v3).
|
|
||||||
|
|
||||||
## Known Issues / Missing Features
|
## Known Issues / Missing Features
|
||||||
- `yq` attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details)
|
- `yq` attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details)
|
||||||
|
- You cannot (yet) select multiple paths/keys from the yaml to be printed out (https://github.com/mikefarah/yq/issues/287)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ var (
|
|||||||
GitDescribe string
|
GitDescribe string
|
||||||
|
|
||||||
// Version is main version number that is being run at the moment.
|
// Version is main version number that is being run at the moment.
|
||||||
Version = "4.0.0-beta1"
|
Version = "4.0.0-beta2"
|
||||||
|
|
||||||
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
|
// 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
|
// then it means that it is a final release. Otherwise, this is a pre-release
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
lex "github.com/timtadh/lexmachine"
|
lex "github.com/timtadh/lexmachine"
|
||||||
@@ -289,7 +288,7 @@ func (p *pathTokeniser) Tokenise(path string) ([]*Token, error) {
|
|||||||
scanner, err := p.lexer.Scanner([]byte(path))
|
scanner, err := p.lexer.Scanner([]byte(path))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Parsing expression: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
var tokens []*Token
|
var tokens []*Token
|
||||||
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
|
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
|
||||||
@@ -300,7 +299,7 @@ func (p *pathTokeniser) Tokenise(path string) ([]*Token, error) {
|
|||||||
tokens = append(tokens, token)
|
tokens = append(tokens, token)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Parsing expression: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var postProcessedTokens = make([]*Token, 0)
|
var postProcessedTokens = make([]*Token, 0)
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var myPathTokeniser = NewPathTokeniser()
|
var myPathTokeniser = NewPathTokeniser()
|
||||||
var myPathPostfixer = NewPathPostFixer()
|
var myPathPostfixer = NewPathPostFixer()
|
||||||
@@ -52,16 +49,10 @@ func (p *pathTreeCreator) CreatePathTree(postFixPath []*Operation) (*PathTreeNod
|
|||||||
if Operation.OperationType.NumArgs > 0 {
|
if Operation.OperationType.NumArgs > 0 {
|
||||||
numArgs := Operation.OperationType.NumArgs
|
numArgs := Operation.OperationType.NumArgs
|
||||||
if numArgs == 1 {
|
if numArgs == 1 {
|
||||||
if len(stack) < 1 {
|
|
||||||
return nil, fmt.Errorf("'%v' expects 1 arg but received none", strings.TrimSpace(Operation.StringValue))
|
|
||||||
}
|
|
||||||
remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
|
remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
|
||||||
newNode.Rhs = rhs
|
newNode.Rhs = rhs
|
||||||
stack = remaining
|
stack = remaining
|
||||||
} else if numArgs == 2 {
|
} else if numArgs == 2 {
|
||||||
if len(stack) < 2 {
|
|
||||||
return nil, fmt.Errorf("'%v' expects 2 args but there is %v", strings.TrimSpace(Operation.StringValue), len(stack))
|
|
||||||
}
|
|
||||||
remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
|
remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
|
||||||
newNode.Lhs = lhs
|
newNode.Lhs = lhs
|
||||||
newNode.Rhs = rhs
|
newNode.Rhs = rhs
|
||||||
@@ -71,7 +62,7 @@ func (p *pathTreeCreator) CreatePathTree(postFixPath []*Operation) (*PathTreeNod
|
|||||||
stack = append(stack, &newNode)
|
stack = append(stack, &newNode)
|
||||||
}
|
}
|
||||||
if len(stack) != 1 {
|
if len(stack) != 1 {
|
||||||
return nil, fmt.Errorf("expected end of expression but found '%v', please check expression syntax", strings.TrimSpace(stack[1].Operation.StringValue))
|
return nil, fmt.Errorf("expected stack to have 1 thing but its %v", stack)
|
||||||
}
|
}
|
||||||
return stack[0], nil
|
return stack[0], nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
package yqlib
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/mikefarah/yq/v4/test"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPathTreeNoArgsForTwoArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath("=")
|
|
||||||
test.AssertResultComplex(t, "'=' expects 2 args but there is 0", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeOneLhsArgsForTwoArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath(".a =")
|
|
||||||
test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeOneRhsArgsForTwoArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath("= .a")
|
|
||||||
test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeTwoArgsForTwoArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath(".a = .b")
|
|
||||||
test.AssertResultComplex(t, nil, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeNoArgsForOneArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath("explode")
|
|
||||||
test.AssertResultComplex(t, "'explode' expects 1 arg but received none", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeOneArgForOneArgOp(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath("explode(.)")
|
|
||||||
test.AssertResultComplex(t, nil, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPathTreeExtraArgs(t *testing.T) {
|
|
||||||
_, err := treeCreator.ParsePath("sortKeys(.) explode(.)")
|
|
||||||
test.AssertResultComplex(t, "expected end of expression but found 'explode', please check expression syntax", err.Error())
|
|
||||||
}
|
|
||||||
@@ -22,7 +22,6 @@ type resultsPrinter struct {
|
|||||||
writer io.Writer
|
writer io.Writer
|
||||||
firstTimePrinting bool
|
firstTimePrinting bool
|
||||||
previousDocIndex uint
|
previousDocIndex uint
|
||||||
previousFileIndex int
|
|
||||||
printedMatches bool
|
printedMatches bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,20 +89,19 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if p.firstTimePrinting {
|
if p.firstTimePrinting {
|
||||||
node := matchingNodes.Front().Value.(*CandidateNode)
|
p.previousDocIndex = matchingNodes.Front().Value.(*CandidateNode).Document
|
||||||
p.previousDocIndex = node.Document
|
|
||||||
p.previousFileIndex = node.FileIndex
|
|
||||||
p.firstTimePrinting = false
|
p.firstTimePrinting = false
|
||||||
}
|
}
|
||||||
|
|
||||||
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
mappedDoc := el.Value.(*CandidateNode)
|
mappedDoc := el.Value.(*CandidateNode)
|
||||||
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
|
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
|
||||||
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators {
|
if (p.previousDocIndex != mappedDoc.Document) && p.printDocSeparators {
|
||||||
log.Debug("-- writing doc sep")
|
log.Debug("-- writing doc sep")
|
||||||
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
|
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
||||||
|
|||||||
@@ -52,53 +52,7 @@ func TestPrinterMultipleDocsInSequence(t *testing.T) {
|
|||||||
|
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
test.AssertResult(t, multiDocSample, output.String())
|
test.AssertResult(t, multiDocSample, output.String())
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrinterMultipleFilesInSequence(t *testing.T) {
|
|
||||||
var output bytes.Buffer
|
|
||||||
var writer = bufio.NewWriter(&output)
|
|
||||||
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
||||||
|
|
||||||
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
el := inputs.Front()
|
|
||||||
elNode := el.Value.(*CandidateNode)
|
|
||||||
elNode.Document = 0
|
|
||||||
elNode.FileIndex = 0
|
|
||||||
sample1 := nodeToMap(elNode)
|
|
||||||
|
|
||||||
el = el.Next()
|
|
||||||
elNode = el.Value.(*CandidateNode)
|
|
||||||
elNode.Document = 0
|
|
||||||
elNode.FileIndex = 1
|
|
||||||
sample2 := nodeToMap(elNode)
|
|
||||||
|
|
||||||
el = el.Next()
|
|
||||||
elNode = el.Value.(*CandidateNode)
|
|
||||||
elNode.Document = 0
|
|
||||||
elNode.FileIndex = 2
|
|
||||||
sample3 := nodeToMap(elNode)
|
|
||||||
|
|
||||||
err = printer.PrintResults(sample1)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = printer.PrintResults(sample2)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = printer.PrintResults(sample3)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.Flush()
|
|
||||||
test.AssertResult(t, multiDocSample, output.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
|
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name: yq
|
name: yq
|
||||||
version: '4.0.0-beta1'
|
version: '4.0.0-beta2'
|
||||||
summary: A lightweight and portable command-line YAML processor
|
summary: A lightweight and portable command-line YAML processor
|
||||||
description: |
|
description: |
|
||||||
The aim of the project is to be the jq or sed of yaml files.
|
The aim of the project is to be the jq or sed of yaml files.
|
||||||
|
|||||||
Reference in New Issue
Block a user