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

Compare commits

...

47 Commits

Author SHA1 Message Date
Mike Farah
d84254c30f moar tests 2018-07-08 22:04:31 +10:00
Mike Farah
18bb4eee96 moar tests 2018-07-08 21:57:56 +10:00
Mike Farah
86b9fe3ef9 Can read from all documents 2018-07-08 21:47:01 +10:00
Mike Farah
2c15048ddb Added merge with append 2018-07-07 15:26:56 +10:00
Mike Farah
ce2ee42f71 Release instructions for gopkg 2018-07-01 14:45:19 +10:00
Mike Farah
c2c49dcb17 Fixed help length to prevent horizontal scroll in README 2018-06-27 19:37:18 +10:00
Mike Farah
60de18391c Incrementing version for next release 2018-06-27 12:06:46 +10:00
Mike Farah
c86f8b426b Fixed writing inplace from docker 2018-06-27 12:06:31 +10:00
Mike Farah
b3532e0a61 Cache devtools and vendor libs in docker 2018-06-27 12:05:52 +10:00
Mike Farah
b3b60665e4 Fixed toJson command line option, should only apply to read command 2018-06-26 14:09:56 +10:00
Mike Farah
df08b055cf Shorten instructions in readme to avoid horizontal scroll 2018-06-20 19:50:19 +10:00
Mike Farah
e822313e82 Merge branch 'multi' 2018-06-20 19:48:44 +10:00
Mike Farah
a9f25c9d76 Added more snapcraft instructions 2018-06-20 19:46:47 +10:00
Roberto Mier Escandon
0a8c268dcc Bumped deb version 2018-06-20 19:28:44 +10:00
Mike Farah
a0e70279a8 Updating snapcraft version 2018-06-20 17:51:07 +10:00
Mike Farah
25c9c22d8d Updating docs 2018-06-20 14:29:17 +10:00
Mike Farah
d46d555b07 Incrementing version 2018-06-20 14:29:10 +10:00
Mike Farah
fb87f638f2 Multi doc supports updating all docs 2018-06-20 11:45:51 +10:00
Mike Farah
facc81d1f4 github version of mousetrap required for xcompile 2018-06-20 08:14:14 +10:00
Mike Farah
c1f9065c68 pflag has to be github :eye_roll: 2018-06-18 20:12:09 +10:00
Mike Farah
be84cc3082 Add pflag back in 2018-06-18 20:08:41 +10:00
Mike Farah
a2571da1a1 Updated docs to refer to gopkg.in when using go get 2018-06-18 11:45:46 +10:00
Mike Farah
6d6e476ac8 Use gopkg managed versions of dependencies, for better go get support 2018-06-18 11:37:42 +10:00
Mike Farah
ae0c042ae6 Use gopkg managed version of yaml to properly support go get 2018-06-18 11:12:52 +10:00
Mike Farah
867ec92d3a Version bump 2018-06-15 20:50:20 +10:00
Mike Farah
113586b5e0 Updating help for multi doc 2018-06-15 20:31:29 +10:00
Mike Farah
c38f19e0a9 Enabled multi document support for merge (first document only) 2018-06-15 16:48:36 +10:00
Mike Farah
8ca85b1c64 Simplified merge command 2018-06-15 16:40:52 +10:00
Mike Farah
08870f8ec9 Simplified 'new' command 2018-06-15 16:21:18 +10:00
Mike Farah
94b217984c Better error handling 2018-06-15 16:11:13 +10:00
Mike Farah
2f5a481cc3 Detect when there is no document X to update 2018-06-15 09:54:11 +10:00
Mike Farah
1a4064429d Delete now supports multi docs! 2018-06-15 09:43:20 +10:00
Mike Farah
1b22e1d812 Fixed delete command for arrays 2018-06-15 09:03:42 +10:00
Mike Farah
297522cbdd Write supports multidoc yaml, better use of yaml library streaming 2018-06-15 08:39:59 +10:00
Mike Farah
be991fdacd Read test 2018-06-15 08:39:29 +10:00
Mike Farah
be08214773 fixed version test 2018-06-13 10:00:01 +10:00
Mike Farah
9e971ebeae Beta version of multiple document support for read, write coming soon 2018-06-13 09:26:52 +10:00
Mike Farah
f340db5795 Extract out reading of write commands 2018-06-13 09:24:37 +10:00
Mike Farah
ab852ceafa Separate reading stream from processing 2018-06-13 09:11:54 +10:00
Mike Farah
06a843e9b2 Read now handles multiple documents 2018-06-12 15:41:09 +10:00
Mike Farah
ebdc092688 Updating user docs 2018-06-12 10:17:09 +10:00
Roberto Mier Escandon
27089d1ca1 Updated some documentation pointing to deb install option 2018-06-12 10:12:55 +10:00
Roberto Mier Escandon
1853585d22 Add debian packaging 2018-06-12 10:12:55 +10:00
Mike Farah
0124d26086 Added mkdocs instructions to Contribute 2018-06-12 10:11:15 +10:00
Mike Farah
0d68bea3dc Release instructions 2018-05-08 10:55:16 +10:00
Mike Farah
54603b3607 Updated version in snapcraft.yml 2018-05-08 10:06:41 +10:00
Mike Farah
c86aeca325 Fixed script for publishing from a mac 2018-05-07 20:39:18 +10:00
46 changed files with 1406 additions and 458 deletions

View File

@@ -2,9 +2,16 @@ FROM golang:1.9 as builder
WORKDIR /go/src/mikefarah/yq WORKDIR /go/src/mikefarah/yq
# cache devtools
COPY ./scripts/devtools.sh /go/src/mikefarah/yq/scripts/devtools.sh
RUN ./scripts/devtools.sh
# cache vendor
COPY ./vendor/vendor.json /go/src/mikefarah/yq/vendor/vendor.json
RUN govendor sync
COPY . /go/src/mikefarah/yq COPY . /go/src/mikefarah/yq
RUN scripts/devtools.sh
RUN CGO_ENABLED=0 make local build RUN CGO_ENABLED=0 make local build
# Choose alpine as a base image to make this useful for CI, as many # Choose alpine as a base image to make this useful for CI, as many

View File

@@ -12,9 +12,15 @@ On Ubuntu and other Linux distros supporting `snap` packages:
``` ```
snap install yq snap install yq
``` ```
On Ubuntu 16.04 or higher from Debian package:
```
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq -y
```
or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively: or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively:
``` ```
go get github.com/mikefarah/yq go get gopkg.in/mikefarah/yq.v2
``` ```
## Run with Docker ## Run with Docker
@@ -44,6 +50,7 @@ docker run -it -v ${PWD}:/workdir mikefarah/yq sh
- Pipe data in by using '-' - Pipe data in by using '-'
- Merge multiple yaml files where each additional file sets values for missing or null value keys. - Merge multiple yaml files where each additional file sets values for missing or null value keys.
- Merge multiple yaml files with overwrite to support overriding previous values. - Merge multiple yaml files with overwrite to support overriding previous values.
- Supports multiple documents in a single yaml file
## [Usage](http://mikefarah.github.io/yq/) ## [Usage](http://mikefarah.github.io/yq/)
@@ -55,16 +62,15 @@ Usage:
yq [command] yq [command]
Available Commands: Available Commands:
delete yq d [--inplace/-i] sample.yaml a.b.c delete yq d [--inplace/-i] [--doc/-d index] sample.yaml a.b.c
help Help about any command help Help about any command
merge yq m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml merge yq m [--inplace/-i] [--doc/-d index] [--overwrite/-x] sample.yaml sample2.yaml
new yq n [--script/-s script_file] a.b.c newValueForC new yq n [--script/-s script_file] a.b.c newValue
read yq r sample.yaml a.b.c read yq r [--doc/-d index] sample.yaml a.b.c
write yq w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC write yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml a.b.c newValue
Flags: Flags:
-h, --help help for yq -h, --help help for yq
-j, --tojson output as json
-t, --trim trim yaml output (default true) -t, --trim trim yaml output (default true)
-v, --verbose verbose mode -v, --verbose verbose mode
-V, --version Print version information and quit -V, --version Print version information and quit
@@ -75,6 +81,10 @@ Use "yq [command] --help" for more information about a command.
## Contribute ## Contribute
1. `make [local] vendor` 1. `make [local] vendor`
2. add unit tests 2. add unit tests
3. apply changes 3. apply changes (use govendor with a preference to [gopkg](https://gopkg.in/) for package dependencies)
4. `make [local] build` 4. `make [local] build`
5. profit 5. If required, update the user documentation
- Update README.md and/or documentation under the mkdocs folder
- `make [local] build-docs`
- browse to docs/index.html and check your changes
6. profit

View File

@@ -5,7 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/spf13/cobra" "gopkg.in/spf13/cobra.v0"
) )
func getRootCommand() *cobra.Command { func getRootCommand() *cobra.Command {
@@ -73,30 +73,6 @@ func TestRootCmd_TrimShort(t *testing.T) {
} }
} }
func TestRootCmd_ToJsonLong(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "--tojson")
if result.Error != nil {
t.Error(result.Error)
}
if !outputToJSON {
t.Error("Expected outputToJSON to be true")
}
}
func TestRootCmd_ToJsonShort(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "-j")
if result.Error != nil {
t.Error(result.Error)
}
if !outputToJSON {
t.Error("Expected outputToJSON to be true")
}
}
func TestRootCmd_VersionShort(t *testing.T) { func TestRootCmd_VersionShort(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := runCmd(cmd, "-V") result := runCmd(cmd, "-V")
@@ -128,6 +104,61 @@ func TestReadCmd(t *testing.T) {
assertResult(t, "2\n", result.Output) assertResult(t, "2\n", result.Output)
} }
func TestReadInvalidDocumentIndexCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read -df examples/sample.yaml b.c")
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Document index f is not a integer or *: strconv.ParseInt: parsing "f": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error())
}
func TestReadBadDocumentIndexCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read -d1 examples/sample.yaml b.c")
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Asked to process document index 1 but there are only 1 document(s)`
assertResult(t, expectedOutput, result.Error.Error())
}
func TestReadOrderCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/order.yaml")
if result.Error != nil {
t.Error(result.Error)
}
assertResult(t,
`version: 3
application: MyApp
`,
result.Output)
}
func TestReadMultiCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read -d 1 examples/multiple_docs.yaml another.document")
if result.Error != nil {
t.Error(result.Error)
}
assertResult(t, "here\n", result.Output)
}
func TestReadMultiAllCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read -d* examples/multiple_docs.yaml commonKey")
if result.Error != nil {
t.Error(result.Error)
}
assertResult(t,
`- first document
- second document
- third document
`, result.Output)
}
func TestReadCmd_ArrayYaml(t *testing.T) { func TestReadCmd_ArrayYaml(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [0].gather_facts") result := runCmd(cmd, "read examples/array.yaml [0].gather_facts")
@@ -207,7 +238,7 @@ func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to invalid path") t.Error("Expected command to fail due to invalid path")
} }
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax` expectedOutput := `Error reading path in document index 0: Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@@ -217,7 +248,7 @@ func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to invalid path") t.Error("Expected command to fail due to invalid path")
} }
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax` expectedOutput := `Error reading path in document index 0: Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@@ -269,7 +300,7 @@ func TestReadCmd_ErrorBadPath(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Fatal("Expected command to fail due to invalid path") t.Fatal("Expected command to fail due to invalid path")
} }
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax` expectedOutput := `Error reading path in document index 0: Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@@ -293,7 +324,16 @@ func TestReadCmd_NoTrim(t *testing.T) {
func TestReadCmd_ToJson(t *testing.T) { func TestReadCmd_ToJson(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := runCmd(cmd, "-j read examples/sample.yaml b.c") result := runCmd(cmd, "read -j examples/sample.yaml b.c")
if result.Error != nil {
t.Error(result.Error)
}
assertResult(t, "2\n", result.Output)
}
func TestReadCmd_ToJsonLong(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read --tojson examples/sample.yaml b.c")
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
@@ -334,17 +374,6 @@ func TestNewCmd_Verbose(t *testing.T) {
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestNewCmd_ToJson(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "-j new b.c 3")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `{"b":{"c":3}}
`
assertResult(t, expectedOutput, result.Output)
}
func TestWriteCmd(t *testing.T) { func TestWriteCmd(t *testing.T) {
content := `b: content := `b:
c: 3 c: 3
@@ -363,6 +392,80 @@ func TestWriteCmd(t *testing.T) {
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestWriteMultiCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s -d 1 apples ok", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
---
apples: ok
`
assertResult(t, expectedOutput, result.Output)
}
func TestWriteInvalidDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s -df apples ok", filename))
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Document index f is not a integer or *: strconv.ParseInt: parsing "f": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error())
}
func TestWriteBadDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s -d 1 apples ok", filename))
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Asked to process document index 1 but there are only 1 document(s)`
assertResult(t, expectedOutput, result.Error.Error())
}
func TestWriteMultiAllCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s -d * apples ok", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
apples: ok
---
apples: ok`
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestWriteCmd_EmptyArray(t *testing.T) { func TestWriteCmd_EmptyArray(t *testing.T) {
content := `b: 3` content := `b: 3`
filename := writeTempYamlFile(content) filename := writeTempYamlFile(content)
@@ -432,7 +535,7 @@ func TestWriteCmd_Inplace(t *testing.T) {
gotOutput := readTempYamlFile(filename) gotOutput := readTempYamlFile(filename)
expectedOutput := `b: expectedOutput := `b:
c: 7` c: 7`
assertResult(t, expectedOutput, gotOutput) assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
} }
func TestWriteCmd_Append(t *testing.T) { func TestWriteCmd_Append(t *testing.T) {
@@ -472,6 +575,95 @@ b:
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestDeleteYaml(t *testing.T) {
content := `a: 2
b:
c: things
d: something else
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete %s b.c", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
d: something else
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlArray(t *testing.T) {
content := `- 1
- 2
- 3
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete %s [1]", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- 1
- 3
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlMulti(t *testing.T) {
content := `apples: great
---
- 1
- 2
- 3
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete -d 1 %s [1]", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `apples: great
---
- 1
- 3
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlMultiAllCmd(t *testing.T) {
content := `b:
c: 3
apples: great
---
apples: great
something: else
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete %s -d * apples", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
---
something: else`
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeCmd(t *testing.T) { func TestMergeCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := runCmd(cmd, "merge examples/data1.yaml examples/data2.yaml") result := runCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
@@ -488,6 +680,134 @@ c:
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestMergeOverwriteCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "merge --overwrite examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: other
b:
- 3
- 4
c:
test: 1
`
assertResult(t, expectedOutput, result.Output)
}
func TestMergeAppendCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "merge --append examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: simple
b:
- 1
- 2
- 3
- 4
c:
test: 1
`
assertResult(t, expectedOutput, result.Output)
}
func TestMergeArraysCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- 1
- 2
- 3
- 4
- 5
`
assertResult(t, expectedOutput, result.Output)
}
func TestMergeCmd_Multi(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: Easy! as one two three
---
a: other
another:
document: here
b:
- 3
- 4
c:
test: 1
---
- 1
- 2`
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeYamlMultiAllCmd(t *testing.T) {
content := `b:
c: 3
apples: green
---
something: else`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
mergeContent := `apples: red
something: good`
mergeFilename := writeTempYamlFile(mergeContent)
defer removeTempYamlFile(mergeFilename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("merge -d* %s %s", filename, mergeFilename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `apples: green
b:
c: 3
something: good
---
apples: red
something: else`
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
content := `b:
c: 3
apples: green
---
something: else`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
mergeContent := `apples: red
something: good`
mergeFilename := writeTempYamlFile(mergeContent)
defer removeTempYamlFile(mergeFilename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("merge --overwrite -d* %s %s", filename, mergeFilename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `apples: red
b:
c: 3
something: good
---
apples: red
something: good`
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeCmd_Error(t *testing.T) { func TestMergeCmd_Error(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := runCmd(cmd, "merge examples/data1.yaml") result := runCmd(cmd, "merge examples/data1.yaml")
@@ -504,7 +824,7 @@ func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to unknown file") t.Error("Expected command to fail due to unknown file")
} }
expectedOutput := `open fake-unknown: no such file or directory` expectedOutput := `Error updating document at index 0: open fake-unknown: no such file or directory`
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@@ -540,5 +860,5 @@ b:
- 2 - 2
c: c:
test: 1` test: 1`
assertResult(t, expectedOutput, gotOutput) assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
} }

View File

@@ -2,10 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"gopkg.in/yaml.v2" yaml "gopkg.in/mikefarah/yaml.v2"
) )
func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem { func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem {
@@ -194,23 +193,6 @@ func calculateValue(value interface{}, tail []string) (interface{}, error) {
return value, nil return value, nil
} }
func mapToMapSlice(data map[interface{}]interface{}) yaml.MapSlice {
var mapSlice yaml.MapSlice
for k, v := range data {
if mv, ok := v.(map[interface{}]interface{}); ok {
v = mapToMapSlice(mv)
}
item := yaml.MapItem{Key: k, Value: v}
mapSlice = append(mapSlice, item)
}
// because the parsing of the yaml was done via a map the order will be inconsistent
// apply order to allow a consistent output
sort.SliceStable(mapSlice, func(i, j int) bool { return mapSlice[i].Key.(string) < mapSlice[j].Key.(string) })
return mapSlice
}
func deleteMap(context interface{}, paths []string) yaml.MapSlice { func deleteMap(context interface{}, paths []string) yaml.MapSlice {
log.Debugf("deleteMap for %v for %v\n", paths, context) log.Debugf("deleteMap for %v for %v\n", paths, context)

View File

@@ -5,7 +5,7 @@ import (
"sort" "sort"
"testing" "testing"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/mikefarah/yaml.v2"
) )
func TestReadMap_simple(t *testing.T) { func TestReadMap_simple(t *testing.T) {

12
debian/changelog vendored Normal file
View File

@@ -0,0 +1,12 @@
yq (2.0-0) bionic; urgency=medium
* Release 2.0.0
-- Roberto Mier EscandĂłn <rmescandon@gmail.com> Wed, 20 Jun 2018 10:29:53 +0200
yq (1.15-0) bionic; urgency=medium
* Release 1.15
-- Roberto Mier EscandĂłn <rmescandon@gmail.com> Wed, 06 Jun 2018 11:32:03 +0200

1
debian/compat vendored Normal file
View File

@@ -0,0 +1 @@
9

22
debian/control vendored Normal file
View File

@@ -0,0 +1,22 @@
Source: yq
Section: devel
Priority: extra
Maintainer: Roberto Mier EscandĂłn <rmescandon@gmail.com>
Build-Depends: debhelper (>= 9),
dh-golang,
golang-1.10-go,
rsync
Standards-Version: 3.9.6
Homepage: https://github.com/mikefarah/yq.git
Vcs-Browser: https://github.com/mikefarah/yq.git
Vcs-Git: https://github.com/mikefarah/yq.git
Package: yq
Architecture: all
Built-Using: ${misc:Built-Using}
Depends: ${shlibs:Depends},
${misc:Depends}
Description:
a lightweight and portable command-line YAML processor
.
The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.

7
debian/copyright vendored Normal file
View File

@@ -0,0 +1,7 @@
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: yq
Source: https://github.com/mikefarah/yq.git
Files: *
Copyright: 2017 Mike Farah Ltd. All rights reserved
License: Proprietary

2
debian/gbp.conf vendored Normal file
View File

@@ -0,0 +1,2 @@
[DEFAULT]
pristine-tar = True

59
debian/rules vendored Executable file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/make -f
#
# Copyright (C) 2018 Roberto Mier EscandĂłn <rmescandon@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PROJECT := yq
OWNER := mikefarah
REPO := github.com
GOVERSION := 1.10
export DH_OPTIONS
export DH_GOPKG := ${REPO}/${OWNER}/${PROJECT}
export GOROOT := /usr/lib/go-${GOVERSION}
export GOPATH := ${CURDIR}/_build
export GOBIN := ${GOPATH}/bin
export PATH := ${GOROOT}/bin:${GOBIN}:${PATH}
BLDPATH := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
SRCDIR := ${CURDIR}/_build/src/${DH_GOPKG}
DESTDIR := ${CURDIR}/debian/${PROJECT}
BINDIR := /usr/bin
ASSETSDIR := /usr/share/${PROJECT}
%:
dh $@ --buildsystem=golang --with=golang
override_dh_auto_build:
mkdir -p ${SRCDIR}
mkdir -p ${GOBIN}
# copy project to local srcdir to build from there
rsync -avz --progress --exclude=obj-${BLDPATH} --exclude=debian . $(SRCDIR)
# build go code
(cd ${SRCDIR} && go install ./...)
override_dh_auto_test:
(cd ${SRCDIR} && go test -v ./...)
override_dh_auto_install:
mkdir -p ${DESTDIR}/${BINDIR}
mkdir -p ${DESTDIR}/${ASSETSDIR}
cp ${CURDIR}/_build/bin/yq ${DESTDIR}/${BINDIR}
cp -rf ${SRCDIR}/LICENSE ${DESTDIR}/${ASSETSDIR}
cp -rf ${SRCDIR}/README.md ${DESTDIR}/${PLUGINSDIR}
chmod a+x ${DESTDIR}/${BINDIR}/yq
override_dh_auto_clean:
dh_clean
rm -rf ${CURDIR}/obj-${BLDPATH}
rm -rf ${CURDIR}/_build

1
debian/source/format vendored Normal file
View File

@@ -0,0 +1 @@
3.0 (native)

View File

@@ -372,7 +372,7 @@
<h1>Convert</h1> <h1>Convert</h1>
<h3 id="yaml-to-json">Yaml to Json<a class="headerlink" href="#yaml-to-json" title="Permanent link">&para;</a></h3> <h3 id="yaml-to-json">Yaml to Json<a class="headerlink" href="#yaml-to-json" title="Permanent link">&para;</a></h3>
<p>To convert output to json, use the --tojson (or -j) flag. This can be used with any command.</p> <p>To convert output to json, use the --tojson (or -j) flag. This can only be used with the read command.</p>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:
c: 2 c: 2

View File

@@ -385,7 +385,7 @@
<h1>Create</h1> <h1>Create</h1>
<p>Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file.</p> <p>Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file. Currently this does not support creating multiple documents in a single yaml file.</p>
<pre><code>yq n &lt;path&gt; &lt;new value&gt; <pre><code>yq n &lt;path&gt; &lt;new value&gt;
</code></pre> </code></pre>

View File

@@ -287,6 +287,20 @@
Deleting nodes in-place Deleting nodes in-place
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-delete-from-single-document" title="Multiple Documents - delete from single document" class="md-nav__link">
Multiple Documents - delete from single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-delete-from-all-documents" title="Multiple Documents - delete from all documents" class="md-nav__link">
Multiple Documents - delete from all documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -385,6 +399,20 @@
Deleting nodes in-place Deleting nodes in-place
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-delete-from-single-document" title="Multiple Documents - delete from single document" class="md-nav__link">
Multiple Documents - delete from single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-delete-from-all-documents" title="Multiple Documents - delete from all documents" class="md-nav__link">
Multiple Documents - delete from all documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -413,10 +441,9 @@
<h1>Delete</h1> <h1>Delete</h1>
<pre><code>yq d &lt;yaml_file|json_file&gt; &lt;path_to_delete&gt; <pre><code>yq d &lt;yaml_file&gt; &lt;path_to_delete&gt;
</code></pre> </code></pre>
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
<h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3> <h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:
@@ -469,6 +496,50 @@
</code></pre> </code></pre>
<p>will update the sample.yaml file so that the 'c' node is deleted</p> <p>will update the sample.yaml file so that the 'c' node is deleted</p>
<h3 id="multiple-documents-delete-from-single-document">Multiple Documents - delete from single document<a class="headerlink" href="#multiple-documents-delete-from-single-document" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">something: else
field: leaveMe
---
b:
c: 2
field: deleteMe
</code></pre>
<p>then</p>
<pre><code class="bash">yq w -d1 sample.yaml field
</code></pre>
<p>will output:</p>
<pre><code class="yaml">something: else
field: leaveMe
---
b:
c: 2
</code></pre>
<h3 id="multiple-documents-delete-from-all-documents">Multiple Documents - delete from all documents<a class="headerlink" href="#multiple-documents-delete-from-all-documents" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">something: else
field: deleteMe
---
b:
c: 2
field: deleteMeToo
</code></pre>
<p>then</p>
<pre><code class="bash">yq w -d'*' sample.yaml field
</code></pre>
<p>will output:</p>
<pre><code class="yaml">something: else
---
b:
c: 2
</code></pre>
<p>Note that '*' is in quotes to avoid being interpreted by your shell.</p>
<h3 id="keys-with-dots">Keys with dots<a class="headerlink" href="#keys-with-dots" title="Permanent link">&para;</a></h3> <h3 id="keys-with-dots">Keys with dots<a class="headerlink" href="#keys-with-dots" title="Permanent link">&para;</a></h3>
<p>When specifying a key that has a dot use key lookup indicator.</p> <p>When specifying a key that has a dot use key lookup indicator.</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:

View File

@@ -373,8 +373,14 @@
<pre><code>snap install yq <pre><code>snap install yq
</code></pre> </code></pre>
<p>On Ubuntu 16.04 or higher from Debian package:</p>
<pre><code>sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq -y
</code></pre>
<p>or, <a href="https://github.com/mikefarah/yq/releases/latest">Download latest binary</a> or alternatively:</p> <p>or, <a href="https://github.com/mikefarah/yq/releases/latest">Download latest binary</a> or alternatively:</p>
<pre><code>go get github.com/mikefarah/yq <pre><code>go get gopkg.in/mikefarah/yq.v2
</code></pre> </code></pre>
<p><a href="https://github.com/mikefarah/yq">View on GitHub</a></p> <p><a href="https://github.com/mikefarah/yq">View on GitHub</a></p>

View File

@@ -325,6 +325,20 @@
</li> </li>
<li class="md-nav__item">
<a href="#multiple-documents-merge-into-single-document" title="Multiple Documents - merge into single document" class="md-nav__link">
Multiple Documents - merge into single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-merge-into-all-documents" title="Multiple Documents - merge into all documents" class="md-nav__link">
Multiple Documents - merge into all documents
</a>
</li>
</ul> </ul>
@@ -380,6 +394,20 @@
</li> </li>
<li class="md-nav__item">
<a href="#multiple-documents-merge-into-single-document" title="Multiple Documents - merge into single document" class="md-nav__link">
Multiple Documents - merge into single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-merge-into-all-documents" title="Multiple Documents - merge into all documents" class="md-nav__link">
Multiple Documents - merge into all documents
</a>
</li>
</ul> </ul>
@@ -401,10 +429,9 @@
<p>Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will <p>Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will
set values for any key not existing already or where the key has no value.</p> set values for any key not existing already or where the key has no value.</p>
<pre><code>yq m &lt;yaml_file|json_file&gt; &lt;path&gt;... <pre><code>yq m &lt;yaml_file&gt; &lt;path&gt;...
</code></pre> </code></pre>
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
<h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3> <h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3>
<p>Given a data1.yaml file of:</p> <p>Given a data1.yaml file of:</p>
<pre><code class="yaml">a: simple <pre><code class="yaml">a: simple
@@ -497,6 +524,54 @@ d: false
<p>Notice that 'b' does not result in the merging of the values within an array. The underlying library does not <p>Notice that 'b' does not result in the merging of the values within an array. The underlying library does not
currently handle merging values within an array.</p> currently handle merging values within an array.</p>
<h3 id="multiple-documents-merge-into-single-document">Multiple Documents - merge into single document<a class="headerlink" href="#multiple-documents-merge-into-single-document" title="Permanent link">&para;</a></h3>
<p>Currently yq only has multi-document support for the <em>first</em> document being merged into. The remaining yaml files will have their first document selected.</p>
<p>Given a data1.yaml file of:</p>
<pre><code class="yaml">something: else
---
a: simple
b: cat
</code></pre>
<p>and data3.yaml file of:</p>
<pre><code class="yaml">b: dog
</code></pre>
<p>then</p>
<pre><code class="bash">yq m -x -d1 data1.yaml data3.yaml
</code></pre>
<p>will output:</p>
<pre><code class="yaml">something: else
---
a: simple
b: dog
</code></pre>
<h3 id="multiple-documents-merge-into-all-documents">Multiple Documents - merge into all documents<a class="headerlink" href="#multiple-documents-merge-into-all-documents" title="Permanent link">&para;</a></h3>
<p>Currently yq only has multi-document support for the <em>first</em> document being merged into. The remaining yaml files will have their first document selected.</p>
<p>Given a data1.yaml file of:</p>
<pre><code class="yaml">something: else
---
a: simple
b: cat
</code></pre>
<p>and data3.yaml file of:</p>
<pre><code class="yaml">b: dog
</code></pre>
<p>then</p>
<pre><code class="bash">yq m -x -d'*' data1.yaml data3.yaml
</code></pre>
<p>will output:</p>
<pre><code class="yaml">b: dog
something: else
---
a: simple
b: dog
</code></pre>

View File

@@ -256,6 +256,13 @@
Splat Splat
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents" title="Multiple Documents" class="md-nav__link">
Multiple Documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -385,6 +392,13 @@
Splat Splat
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents" title="Multiple Documents" class="md-nav__link">
Multiple Documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -467,6 +481,19 @@ bob:
- apples - apples
</code></pre> </code></pre>
<h3 id="multiple-documents">Multiple Documents<a class="headerlink" href="#multiple-documents" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">something: else
---
b:
c: 2
</code></pre>
<p>then</p>
<pre><code class="bash">yq r -d1 sample.yaml b.c
</code></pre>
<p>will output the value of '2'.</p>
<h3 id="arrays">Arrays<a class="headerlink" href="#arrays" title="Permanent link">&para;</a></h3> <h3 id="arrays">Arrays<a class="headerlink" href="#arrays" title="Permanent link">&para;</a></h3>
<p>You can give an index to access a specific element: <p>You can give an index to access a specific element:
e.g.: given a sample file of</p> e.g.: given a sample file of</p>

View File

@@ -2,7 +2,7 @@
"docs": [ "docs": [
{ {
"location": "/", "location": "/",
"text": "yq\n\u00b6\n\n\nyq is a lightweight and portable command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\nInstall\n\u00b6\n\n\nOn MacOS:\n\n\nbrew install yq\n\n\n\n\nOn Ubuntu and other Linux distros supporting \nsnap\n packages:\n\n\nsnap install yq\n\n\n\n\nor, \nDownload latest binary\n or alternatively:\n\n\ngo get github.com/mikefarah/yq\n\n\n\n\nView on GitHub", "text": "yq\n\u00b6\n\n\nyq is a lightweight and portable command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\nInstall\n\u00b6\n\n\nOn MacOS:\n\n\nbrew install yq\n\n\n\n\nOn Ubuntu and other Linux distros supporting \nsnap\n packages:\n\n\nsnap install yq\n\n\n\n\nOn Ubuntu 16.04 or higher from Debian package:\n\n\nsudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y\n\n\n\n\nor, \nDownload latest binary\n or alternatively:\n\n\ngo get gopkg.in/mikefarah/yq.v2\n\n\n\n\nView on GitHub",
"title": "Install" "title": "Install"
}, },
{ {
@@ -12,12 +12,12 @@
}, },
{ {
"location": "/#install", "location": "/#install",
"text": "On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq or, Download latest binary or alternatively: go get github.com/mikefarah/yq View on GitHub", "text": "On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq On Ubuntu 16.04 or higher from Debian package: sudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y or, Download latest binary or alternatively: go get gopkg.in/mikefarah/yq.v2 View on GitHub",
"title": "Install" "title": "Install"
}, },
{ {
"location": "/read/", "location": "/read/",
"text": "yq r <yaml_file|json_file> <path>\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nBasic\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yq r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyq r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nArrays\n\u00b6\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyq r sample.yaml 'b.e[1].name'\n\n\n\n\nwill output 'sam'\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nArray Splat\n\u00b6\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyq r sample.yaml 'b.e[*].name'\n\n\n\n\nwill output:\n\n\n- fred\n- sam\n\n\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "yq r <yaml_file|json_file> <path>\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nBasic\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yq r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyq r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nMultiple Documents\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nsomething: else\n---\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq r -d1 sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\n\u00b6\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyq r sample.yaml 'b.e[1].name'\n\n\n\n\nwill output 'sam'\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nArray Splat\n\u00b6\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyq r sample.yaml 'b.e[*].name'\n\n\n\n\nwill output:\n\n\n- fred\n- sam\n\n\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.",
"title": "Read" "title": "Read"
}, },
{ {
@@ -35,6 +35,11 @@
"text": "Given a sample.yaml file of: ---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples then yq r sample.yaml bob.*.cats will output - bananas\n- apples", "text": "Given a sample.yaml file of: ---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples then yq r sample.yaml bob.*.cats will output - bananas\n- apples",
"title": "Splat" "title": "Splat"
}, },
{
"location": "/read/#multiple-documents",
"text": "Given a sample.yaml file of: something: else\n---\nb:\n c: 2 then yq r -d1 sample.yaml b.c will output the value of '2'.",
"title": "Multiple Documents"
},
{ {
"location": "/read/#arrays", "location": "/read/#arrays",
"text": "You can give an index to access a specific element:\ne.g.: given a sample file of b:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4 then yq r sample.yaml 'b.e[1].name' will output 'sam' Note that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "You can give an index to access a specific element:\ne.g.: given a sample file of b:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4 then yq r sample.yaml 'b.e[1].name' will output 'sam' Note that the path is in quotes to avoid the square brackets being interpreted by your shell.",
@@ -52,7 +57,7 @@
}, },
{ {
"location": "/write/", "location": "/write/",
"text": "yq w <yaml_file|json_file> <path> <new value>\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\u00b6\n\n\ncat sample.yaml | yq w - b.c blah\n\n\n\n\nAdding new fields\n\u00b6\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w sample.yaml b.d[0] \"new thing\"\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nAppending value to an array field\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n d:\n - new thing\n - foo thing\n\n\n\n\nthen\n\n\nyq w sample.yaml \"b.d[+]\" \"bar thing\"\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing\n\n\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nUpdating files in-place\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyq w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yq w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\u00b6\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyq w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3\n\n\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "yq w <yaml_file> <path> <new value>\n\n\n\n\nTo Stdout\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\u00b6\n\n\ncat sample.yaml | yq w - b.c blah\n\n\n\n\nAdding new fields\n\u00b6\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w sample.yaml b.d[0] \"new thing\"\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nAppending value to an array field\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n d:\n - new thing\n - foo thing\n\n\n\n\nthen\n\n\nyq w sample.yaml \"b.d[+]\" \"bar thing\"\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing\n\n\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.\n\n\nMultiple Documents - update a single document\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nsomething: else\n---\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w -d1 sample.yaml b.c 5\n\n\n\n\nwill output:\n\n\nsomething: else\n---\nb:\n c: 5\n\n\n\n\nMultiple Documents - update all documents\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nsomething: else\n---\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w -d'*' sample.yaml b.c 5\n\n\n\n\nwill output:\n\n\nsomething: else\nb:\n c: 5\n---\nb:\n c: 5\n\n\n\n\nNote that '*' is in quotes to avoid being interpreted by your shell.\n\n\nUpdating files in-place\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyq w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yq w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\u00b6\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyq w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3\n\n\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.",
"title": "Write/Update" "title": "Write/Update"
}, },
{ {
@@ -75,6 +80,16 @@
"text": "Given a sample.yaml file of: b:\n c: 2\n d:\n - new thing\n - foo thing then yq w sample.yaml \"b.d[+]\" \"bar thing\" will output: b:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing Note that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "Given a sample.yaml file of: b:\n c: 2\n d:\n - new thing\n - foo thing then yq w sample.yaml \"b.d[+]\" \"bar thing\" will output: b:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing Note that the path is in quotes to avoid the square brackets being interpreted by your shell.",
"title": "Appending value to an array field" "title": "Appending value to an array field"
}, },
{
"location": "/write/#multiple-documents-update-a-single-document",
"text": "Given a sample.yaml file of: something: else\n---\nb:\n c: 2 then yq w -d1 sample.yaml b.c 5 will output: something: else\n---\nb:\n c: 5",
"title": "Multiple Documents - update a single document"
},
{
"location": "/write/#multiple-documents-update-all-documents",
"text": "Given a sample.yaml file of: something: else\n---\nb:\n c: 2 then yq w -d'*' sample.yaml b.c 5 will output: something: else\nb:\n c: 5\n---\nb:\n c: 5 Note that '*' is in quotes to avoid being interpreted by your shell.",
"title": "Multiple Documents - update all documents"
},
{ {
"location": "/write/#updating-files-in-place", "location": "/write/#updating-files-in-place",
"text": "Given a sample.yaml file of: b:\n c: 2 then yq w -i sample.yaml b.c cat will update the sample.yaml file so that the value of 'c' is cat.", "text": "Given a sample.yaml file of: b:\n c: 2 then yq w -i sample.yaml b.c cat will update the sample.yaml file so that the value of 'c' is cat.",
@@ -97,7 +112,7 @@
}, },
{ {
"location": "/delete/", "location": "/delete/",
"text": "yq d <yaml_file|json_file> <path_to_delete>\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n apples: green\n\n\n\n\nthen\n\n\nyq d sample.yaml b.c\n\n\n\n\nwill output:\n\n\nb:\n apples: green\n\n\n\n\nFrom STDIN\n\u00b6\n\n\ncat sample.yaml | yq d - b.c\n\n\n\n\nDeleting array elements\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: \n - 1\n - 2\n - 3\n\n\n\n\nthen\n\n\nyq d sample.yaml 'b.c[1]'\n\n\n\n\nwill output:\n\n\nb:\n c:\n - 1\n - 3\n\n\n\n\nDeleting nodes in-place\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n apples: green\n\n\n\n\nthen\n\n\nyq d -i sample.yaml b.c\n\n\n\n\nwill update the sample.yaml file so that the 'c' node is deleted\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "yq d <yaml_file> <path_to_delete>\n\n\n\n\nTo Stdout\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n apples: green\n\n\n\n\nthen\n\n\nyq d sample.yaml b.c\n\n\n\n\nwill output:\n\n\nb:\n apples: green\n\n\n\n\nFrom STDIN\n\u00b6\n\n\ncat sample.yaml | yq d - b.c\n\n\n\n\nDeleting array elements\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: \n - 1\n - 2\n - 3\n\n\n\n\nthen\n\n\nyq d sample.yaml 'b.c[1]'\n\n\n\n\nwill output:\n\n\nb:\n c:\n - 1\n - 3\n\n\n\n\nDeleting nodes in-place\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n apples: green\n\n\n\n\nthen\n\n\nyq d -i sample.yaml b.c\n\n\n\n\nwill update the sample.yaml file so that the 'c' node is deleted\n\n\nMultiple Documents - delete from single document\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nsomething: else\nfield: leaveMe\n---\nb:\n c: 2\nfield: deleteMe\n\n\n\n\nthen\n\n\nyq w -d1 sample.yaml field\n\n\n\n\nwill output:\n\n\nsomething: else\nfield: leaveMe\n---\nb:\n c: 2\n\n\n\n\nMultiple Documents - delete from all documents\n\u00b6\n\n\nGiven a sample.yaml file of:\n\n\nsomething: else\nfield: deleteMe\n---\nb:\n c: 2\nfield: deleteMeToo\n\n\n\n\nthen\n\n\nyq w -d'*' sample.yaml field\n\n\n\n\nwill output:\n\n\nsomething: else\n---\nb:\n c: 2\n\n\n\n\nNote that '*' is in quotes to avoid being interpreted by your shell.\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.",
"title": "Delete" "title": "Delete"
}, },
{ {
@@ -120,6 +135,16 @@
"text": "Given a sample.yaml file of: b:\n c: 2\n apples: green then yq d -i sample.yaml b.c will update the sample.yaml file so that the 'c' node is deleted", "text": "Given a sample.yaml file of: b:\n c: 2\n apples: green then yq d -i sample.yaml b.c will update the sample.yaml file so that the 'c' node is deleted",
"title": "Deleting nodes in-place" "title": "Deleting nodes in-place"
}, },
{
"location": "/delete/#multiple-documents-delete-from-single-document",
"text": "Given a sample.yaml file of: something: else\nfield: leaveMe\n---\nb:\n c: 2\nfield: deleteMe then yq w -d1 sample.yaml field will output: something: else\nfield: leaveMe\n---\nb:\n c: 2",
"title": "Multiple Documents - delete from single document"
},
{
"location": "/delete/#multiple-documents-delete-from-all-documents",
"text": "Given a sample.yaml file of: something: else\nfield: deleteMe\n---\nb:\n c: 2\nfield: deleteMeToo then yq w -d'*' sample.yaml field will output: something: else\n---\nb:\n c: 2 Note that '*' is in quotes to avoid being interpreted by your shell.",
"title": "Multiple Documents - delete from all documents"
},
{ {
"location": "/delete/#keys-with-dots", "location": "/delete/#keys-with-dots",
"text": "When specifying a key that has a dot use key lookup indicator. b:\n foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "When specifying a key that has a dot use key lookup indicator. b:\n foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.",
@@ -127,7 +152,7 @@
}, },
{ {
"location": "/create/", "location": "/create/",
"text": "Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file.\n\n\nyq n <path> <new value>\n\n\n\n\nCreating a simple yaml file\n\u00b6\n\n\nyq n b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nCreating using a create script\n\u00b6\n\n\nCreate scripts follow the same format as the update scripts.\n\n\nGiven a script create_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyq n -s create_instructions.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nYou can also pipe the instructions in:\n\n\ncat create_instructions.yaml | yq n -s -\n\n\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.", "text": "Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file. Currently this does not support creating multiple documents in a single yaml file.\n\n\nyq n <path> <new value>\n\n\n\n\nCreating a simple yaml file\n\u00b6\n\n\nyq n b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nCreating using a create script\n\u00b6\n\n\nCreate scripts follow the same format as the update scripts.\n\n\nGiven a script create_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyq n -s create_instructions.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nYou can also pipe the instructions in:\n\n\ncat create_instructions.yaml | yq n -s -\n\n\n\n\nKeys with dots\n\u00b6\n\n\nWhen specifying a key that has a dot use key lookup indicator.\n\n\nb:\n foo.bar: 7\n\n\n\n\nyaml r sample.yaml 'b[foo.bar]'\n\n\n\n\nyaml w sample.yaml 'b[foo.bar]' 9\n\n\n\n\nAny valid yaml key can be specified as part of a key lookup.\n\n\nNote that the path is in quotes to avoid the square brackets being interpreted by your shell.",
"title": "Create" "title": "Create"
}, },
{ {
@@ -147,12 +172,12 @@
}, },
{ {
"location": "/convert/", "location": "/convert/",
"text": "Yaml to Json\n\u00b6\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can be used with any command.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\"b\":{\"c\":2}}\n\n\n\n\nJson to Yaml\n\u00b6\n\n\nTo read in json, just pass in a json file instead of yaml, it will just work :)\n\n\ne.g given a json file\n\n\n{\"a\":\"Easy! as one two three\",\"b\":{\"c\":2,\"d\":[3,4]}}\n\n\n\n\nthen\n\n\nyq r sample.json\n\n\n\n\nwill output\n\n\na: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4", "text": "Yaml to Json\n\u00b6\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can only be used with the read command.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyq r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\"b\":{\"c\":2}}\n\n\n\n\nJson to Yaml\n\u00b6\n\n\nTo read in json, just pass in a json file instead of yaml, it will just work :)\n\n\ne.g given a json file\n\n\n{\"a\":\"Easy! as one two three\",\"b\":{\"c\":2,\"d\":[3,4]}}\n\n\n\n\nthen\n\n\nyq r sample.json\n\n\n\n\nwill output\n\n\na: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4",
"title": "Convert" "title": "Convert"
}, },
{ {
"location": "/convert/#yaml-to-json", "location": "/convert/#yaml-to-json",
"text": "To convert output to json, use the --tojson (or -j) flag. This can be used with any command. Given a sample.yaml file of: b:\n c: 2 then yq r -j sample.yaml b.c will output {\"b\":{\"c\":2}}", "text": "To convert output to json, use the --tojson (or -j) flag. This can only be used with the read command. Given a sample.yaml file of: b:\n c: 2 then yq r -j sample.yaml b.c will output {\"b\":{\"c\":2}}",
"title": "Yaml to Json" "title": "Yaml to Json"
}, },
{ {
@@ -162,7 +187,7 @@
}, },
{ {
"location": "/merge/", "location": "/merge/",
"text": "Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will\nset values for any key not existing already or where the key has no value.\n\n\nyq m <yaml_file|json_file> <path>...\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m data1.yaml data2.yaml\n\n\n\n\nwill output:\n\n\na: simple\nb: [1, 2]\nc:\n test: 1\n\n\n\n\nUpdating files in-place\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m -i data1.yaml data2.yaml\n\n\n\n\nwill update the data1.yaml file so that the value of 'c' is 'test: 1'.\n\n\nOverwrite values\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m -x data1.yaml data2.yaml\n\n\n\n\nwill output:\n\n\na: other\nb: [1, 2]\nc:\n test: 1\n\n\n\n\nOverwrite values with arrays\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data3.yaml file of:\n\n\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false\n\n\n\n\nthen\n\n\nyq m -x data1.yaml data3.yaml\n\n\n\n\nwill output:\n\n\na: simple\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false\n\n\n\n\nNotice that 'b' does not result in the merging of the values within an array. The underlying library does not\ncurrently handle merging values within an array.", "text": "Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will\nset values for any key not existing already or where the key has no value.\n\n\nyq m <yaml_file> <path>...\n\n\n\n\nTo Stdout\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m data1.yaml data2.yaml\n\n\n\n\nwill output:\n\n\na: simple\nb: [1, 2]\nc:\n test: 1\n\n\n\n\nUpdating files in-place\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m -i data1.yaml data2.yaml\n\n\n\n\nwill update the data1.yaml file so that the value of 'c' is 'test: 1'.\n\n\nOverwrite values\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data2.yaml file of:\n\n\na: other\nc:\n test: 1\n\n\n\n\nthen\n\n\nyq m -x data1.yaml data2.yaml\n\n\n\n\nwill output:\n\n\na: other\nb: [1, 2]\nc:\n test: 1\n\n\n\n\nOverwrite values with arrays\n\u00b6\n\n\nGiven a data1.yaml file of:\n\n\na: simple\nb: [1, 2]\n\n\n\n\nand data3.yaml file of:\n\n\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false\n\n\n\n\nthen\n\n\nyq m -x data1.yaml data3.yaml\n\n\n\n\nwill output:\n\n\na: simple\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false\n\n\n\n\nNotice that 'b' does not result in the merging of the values within an array. The underlying library does not\ncurrently handle merging values within an array.\n\n\nMultiple Documents - merge into single document\n\u00b6\n\n\nCurrently yq only has multi-document support for the \nfirst\n document being merged into. The remaining yaml files will have their first document selected.\n\n\nGiven a data1.yaml file of:\n\n\nsomething: else\n---\na: simple\nb: cat\n\n\n\n\nand data3.yaml file of:\n\n\nb: dog\n\n\n\n\nthen\n\n\nyq m -x -d1 data1.yaml data3.yaml\n\n\n\n\nwill output:\n\n\nsomething: else\n---\na: simple\nb: dog\n\n\n\n\nMultiple Documents - merge into all documents\n\u00b6\n\n\nCurrently yq only has multi-document support for the \nfirst\n document being merged into. The remaining yaml files will have their first document selected.\n\n\nGiven a data1.yaml file of:\n\n\nsomething: else\n---\na: simple\nb: cat\n\n\n\n\nand data3.yaml file of:\n\n\nb: dog\n\n\n\n\nthen\n\n\nyq m -x -d'*' data1.yaml data3.yaml\n\n\n\n\nwill output:\n\n\nb: dog\nsomething: else\n---\na: simple\nb: dog",
"title": "Merge" "title": "Merge"
}, },
{ {
@@ -184,6 +209,16 @@
"location": "/merge/#overwrite-values-with-arrays", "location": "/merge/#overwrite-values-with-arrays",
"text": "Given a data1.yaml file of: a: simple\nb: [1, 2] and data3.yaml file of: b: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false then yq m -x data1.yaml data3.yaml will output: a: simple\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false Notice that 'b' does not result in the merging of the values within an array. The underlying library does not\ncurrently handle merging values within an array.", "text": "Given a data1.yaml file of: a: simple\nb: [1, 2] and data3.yaml file of: b: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false then yq m -x data1.yaml data3.yaml will output: a: simple\nb: [2, 3, 4]\nc:\n test: 2\n other: true\nd: false Notice that 'b' does not result in the merging of the values within an array. The underlying library does not\ncurrently handle merging values within an array.",
"title": "Overwrite values with arrays" "title": "Overwrite values with arrays"
},
{
"location": "/merge/#multiple-documents-merge-into-single-document",
"text": "Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else\n---\na: simple\nb: cat and data3.yaml file of: b: dog then yq m -x -d1 data1.yaml data3.yaml will output: something: else\n---\na: simple\nb: dog",
"title": "Multiple Documents - merge into single document"
},
{
"location": "/merge/#multiple-documents-merge-into-all-documents",
"text": "Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else\n---\na: simple\nb: cat and data3.yaml file of: b: dog then yq m -x -d'*' data1.yaml data3.yaml will output: b: dog\nsomething: else\n---\na: simple\nb: dog",
"title": "Multiple Documents - merge into all documents"
} }
] ]
} }

View File

@@ -4,7 +4,7 @@
<url> <url>
<loc>/</loc> <loc>/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -12,7 +12,7 @@
<url> <url>
<loc>/read/</loc> <loc>/read/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -20,7 +20,7 @@
<url> <url>
<loc>/write/</loc> <loc>/write/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -28,7 +28,7 @@
<url> <url>
<loc>/delete/</loc> <loc>/delete/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -36,7 +36,7 @@
<url> <url>
<loc>/create/</loc> <loc>/create/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -44,7 +44,7 @@
<url> <url>
<loc>/convert/</loc> <loc>/convert/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -52,7 +52,7 @@
<url> <url>
<loc>/merge/</loc> <loc>/merge/</loc>
<lastmod>2018-05-07</lastmod> <lastmod>2018-06-20</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>

View File

@@ -275,6 +275,20 @@
Appending value to an array field Appending value to an array field
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-update-a-single-document" title="Multiple Documents - update a single document" class="md-nav__link">
Multiple Documents - update a single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-update-all-documents" title="Multiple Documents - update all documents" class="md-nav__link">
Multiple Documents - update all documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -406,6 +420,20 @@
Appending value to an array field Appending value to an array field
</a> </a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-update-a-single-document" title="Multiple Documents - update a single document" class="md-nav__link">
Multiple Documents - update a single document
</a>
</li>
<li class="md-nav__item">
<a href="#multiple-documents-update-all-documents" title="Multiple Documents - update all documents" class="md-nav__link">
Multiple Documents - update all documents
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@@ -455,10 +483,9 @@
<h1>Write/Update</h1> <h1>Write/Update</h1>
<pre><code>yq w &lt;yaml_file|json_file&gt; &lt;path&gt; &lt;new value&gt; <pre><code>yq w &lt;yaml_file&gt; &lt;path&gt; &lt;new value&gt;
</code></pre> </code></pre>
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
<h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3> <h3 id="to-stdout">To Stdout<a class="headerlink" href="#to-stdout" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:
@@ -519,6 +546,47 @@
</code></pre> </code></pre>
<p>Note that the path is in quotes to avoid the square brackets being interpreted by your shell.</p> <p>Note that the path is in quotes to avoid the square brackets being interpreted by your shell.</p>
<h3 id="multiple-documents-update-a-single-document">Multiple Documents - update a single document<a class="headerlink" href="#multiple-documents-update-a-single-document" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">something: else
---
b:
c: 2
</code></pre>
<p>then</p>
<pre><code class="bash">yq w -d1 sample.yaml b.c 5
</code></pre>
<p>will output:</p>
<pre><code class="yaml">something: else
---
b:
c: 5
</code></pre>
<h3 id="multiple-documents-update-all-documents">Multiple Documents - update all documents<a class="headerlink" href="#multiple-documents-update-all-documents" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">something: else
---
b:
c: 2
</code></pre>
<p>then</p>
<pre><code class="bash">yq w -d'*' sample.yaml b.c 5
</code></pre>
<p>will output:</p>
<pre><code class="yaml">something: else
b:
c: 5
---
b:
c: 5
</code></pre>
<p>Note that '*' is in quotes to avoid being interpreted by your shell.</p>
<h3 id="updating-files-in-place">Updating files in-place<a class="headerlink" href="#updating-files-in-place" title="Permanent link">&para;</a></h3> <h3 id="updating-files-in-place">Updating files in-place<a class="headerlink" href="#updating-files-in-place" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:

View File

@@ -1,3 +1,4 @@
a: other a: other
b: [3, 4]
c: c:
test: 1 test: 1

View File

@@ -1,5 +1 @@
b: [2, 3, 4] b: dog
c:
test: 2
other: true
d: false

View File

@@ -0,0 +1,18 @@
commonKey: first document
a: Easy! as one two three
b:
c: 2
d: [3, 4]
e:
- name: fred
value: 3
- name: sam
value: 4
---
commonKey: second document
another:
document: here
---
commonKey: third document
wow:
- here is another

View File

@@ -0,0 +1,7 @@
a: Easy! as one two three
---
another:
document: here
---
- 1
- 2

View File

@@ -0,0 +1 @@
[4,5]

View File

@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/mikefarah/yaml.v2"
) )
func jsonToString(context interface{}) (string, error) { func jsonToString(context interface{}) (string, error) {

View File

@@ -1,12 +1,12 @@
package main package main
import ( import "gopkg.in/imdario/mergo.v0"
"github.com/imdario/mergo"
)
func merge(dst, src interface{}, overwrite bool) error { func merge(dst interface{}, src interface{}, overwrite bool, append bool) error {
if overwrite { if overwrite {
return mergo.MergeWithOverwrite(dst, src) return mergo.Merge(dst, src, mergo.WithOverride)
} else if append {
return mergo.Merge(dst, src, mergo.WithAppendSlice)
} }
return mergo.Merge(dst, src) return mergo.Merge(dst, src)
} }

View File

@@ -1,30 +0,0 @@
package main
import (
"testing"
yaml "gopkg.in/yaml.v2"
)
func TestMerge(t *testing.T) {
result, _ := mergeYaml([]string{"examples/data1.yaml", "examples/data2.yaml", "examples/data3.yaml"})
expected := yaml.MapSlice{
yaml.MapItem{Key: "a", Value: "simple"},
yaml.MapItem{Key: "b", Value: []interface{}{1, 2}},
yaml.MapItem{Key: "c", Value: yaml.MapSlice{yaml.MapItem{Key: "other", Value: true}, yaml.MapItem{Key: "test", Value: 1}}},
yaml.MapItem{Key: "d", Value: false},
}
assertResultComplex(t, expected, result)
}
func TestMergeWithOverwrite(t *testing.T) {
overwriteFlag = true
result, _ := mergeYaml([]string{"examples/data1.yaml", "examples/data2.yaml", "examples/data3.yaml"})
expected := yaml.MapSlice{
yaml.MapItem{Key: "a", Value: "other"},
yaml.MapItem{Key: "b", Value: []interface{}{2, 3, 4}},
yaml.MapItem{Key: "c", Value: yaml.MapSlice{yaml.MapItem{Key: "other", Value: true}, yaml.MapItem{Key: "test", Value: 2}}},
yaml.MapItem{Key: "d", Value: false},
}
assertResultComplex(t, expected, result)
}

View File

@@ -1,5 +1,5 @@
### Yaml to Json ### Yaml to Json
To convert output to json, use the --tojson (or -j) flag. This can be used with any command. To convert output to json, use the --tojson (or -j) flag. This can only be used with the read command.
Given a sample.yaml file of: Given a sample.yaml file of:
```yaml ```yaml

View File

@@ -1,4 +1,4 @@
Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file. Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file. Currently this does not support creating multiple documents in a single yaml file.
``` ```
yq n <path> <new value> yq n <path> <new value>

View File

@@ -1,7 +1,6 @@
``` ```
yq d <yaml_file|json_file> <path_to_delete> yq d <yaml_file> <path_to_delete>
``` ```
{!snippets/works_with_json.md!}
### To Stdout ### To Stdout
Given a sample.yaml file of: Given a sample.yaml file of:
@@ -60,4 +59,52 @@ yq d -i sample.yaml b.c
will update the sample.yaml file so that the 'c' node is deleted will update the sample.yaml file so that the 'c' node is deleted
### Multiple Documents - delete from single document
Given a sample.yaml file of:
```yaml
something: else
field: leaveMe
---
b:
c: 2
field: deleteMe
```
then
```bash
yq w -d1 sample.yaml field
```
will output:
```yaml
something: else
field: leaveMe
---
b:
c: 2
```
### Multiple Documents - delete from all documents
Given a sample.yaml file of:
```yaml
something: else
field: deleteMe
---
b:
c: 2
field: deleteMeToo
```
then
```bash
yq w -d'*' sample.yaml field
```
will output:
```yaml
something: else
---
b:
c: 2
```
Note that '*' is in quotes to avoid being interpreted by your shell.
{!snippets/keys_with_dots.md!} {!snippets/keys_with_dots.md!}

View File

@@ -12,9 +12,15 @@ On Ubuntu and other Linux distros supporting `snap` packages:
``` ```
snap install yq snap install yq
``` ```
On Ubuntu 16.04 or higher from Debian package:
```
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq -y
```
or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively: or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively:
``` ```
go get github.com/mikefarah/yq go get gopkg.in/mikefarah/yq.v2
``` ```
[View on GitHub](https://github.com/mikefarah/yq) [View on GitHub](https://github.com/mikefarah/yq)

View File

@@ -2,9 +2,9 @@ Yaml files can be merged using the 'merge' command. Each additional file merged
set values for any key not existing already or where the key has no value. set values for any key not existing already or where the key has no value.
``` ```
yq m <yaml_file|json_file> <path>... yq m <yaml_file> <path>...
``` ```
{!snippets/works_with_json.md!}
### To Stdout ### To Stdout
Given a data1.yaml file of: Given a data1.yaml file of:
@@ -102,3 +102,56 @@ d: false
Notice that 'b' does not result in the merging of the values within an array. The underlying library does not Notice that 'b' does not result in the merging of the values within an array. The underlying library does not
currently handle merging values within an array. currently handle merging values within an array.
### Multiple Documents - merge into single document
Currently yq only has multi-document support for the _first_ document being merged into. The remaining yaml files will have their first document selected.
Given a data1.yaml file of:
```yaml
something: else
---
a: simple
b: cat
```
and data3.yaml file of:
```yaml
b: dog
```
then
```bash
yq m -x -d1 data1.yaml data3.yaml
```
will output:
```yaml
something: else
---
a: simple
b: dog
```
### Multiple Documents - merge into all documents
Currently yq only has multi-document support for the _first_ document being merged into. The remaining yaml files will have their first document selected.
Given a data1.yaml file of:
```yaml
something: else
---
a: simple
b: cat
```
and data3.yaml file of:
```yaml
b: dog
```
then
```bash
yq m -x -d'*' data1.yaml data3.yaml
```
will output:
```yaml
b: dog
something: else
---
a: simple
b: dog
```

View File

@@ -43,6 +43,20 @@ will output
- apples - apples
``` ```
### Multiple Documents
Given a sample.yaml file of:
```yaml
something: else
---
b:
c: 2
```
then
```bash
yq r -d1 sample.yaml b.c
```
will output the value of '2'.
### Arrays ### Arrays
You can give an index to access a specific element: You can give an index to access a specific element:
e.g.: given a sample file of e.g.: given a sample file of

View File

@@ -1,7 +1,6 @@
``` ```
yq w <yaml_file|json_file> <path> <new value> yq w <yaml_file> <path> <new value>
``` ```
{!snippets/works_with_json.md!}
### To Stdout ### To Stdout
Given a sample.yaml file of: Given a sample.yaml file of:
@@ -69,6 +68,50 @@ b:
Note that the path is in quotes to avoid the square brackets being interpreted by your shell. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.
### Multiple Documents - update a single document
Given a sample.yaml file of:
```yaml
something: else
---
b:
c: 2
```
then
```bash
yq w -d1 sample.yaml b.c 5
```
will output:
```yaml
something: else
---
b:
c: 5
```
### Multiple Documents - update all documents
Given a sample.yaml file of:
```yaml
something: else
---
b:
c: 2
```
then
```bash
yq w -d'*' sample.yaml b.c 5
```
will output:
```yaml
something: else
b:
c: 5
---
b:
c: 5
```
Note that '*' is in quotes to avoid being interpreted by your shell.
### Updating files in-place ### Updating files in-place
Given a sample.yaml file of: Given a sample.yaml file of:
```yaml ```yaml

37
release_instructions.txt Normal file
View File

@@ -0,0 +1,37 @@
- increment version in version.go
- increment version in snapcraft.yaml
- tag git with same version number, be sure to start with 'v' for gopkg.in
- make sure local build passes
- push tag to git
- make local xcompile (builds binaries for all platforms)
- git release
./scripts/publish.sh
- snapcraft
- will auto create a candidate, test it works then promote
- see https://build.snapcraft.io/user/mikefarah/yq
sudo snap remove yq
sudo snap install --edge yq
then on the mac snapcraft release yq <snap_build_number> stable
- brew
- create pull request pointing to latest git release
- docker
- build and push latest and new version tag
- debian package
- execute
```dch -i```
- fill debian/changelog with changes from last version
- build the package sources
```debuild -i -I -S -sa```
(signing with gpg key is required in order to put it to ppa)
- put to PPA
```dput ppa:<REPOSITORY> ../yq_<VERSION>_source.changes```
(current distro repository is ppa:rmescandon/yq. In case that a new version
is released, please contact rmescandon@gmail.com to bump debian package)

View File

@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
set -ex
GITHUB_TOKEN="${GITHUB_TOKEN:?missing required input \'GITHUB_TOKEN\'}" GITHUB_TOKEN="${GITHUB_TOKEN:?missing required input \'GITHUB_TOKEN\'}"
CURRENT="$(git describe --tags --abbrev=0)" CURRENT="$(git describe --tags --abbrev=0)"
@@ -8,32 +8,25 @@ OWNER="mikefarah"
REPO="yq" REPO="yq"
release() { release() {
mapfile -t logs < <(git log --pretty=oneline --abbrev-commit "${PREVIOUS}".."${CURRENT}")
description="$(printf '%s\n' "${logs[@]}")"
github-release release \ github-release release \
--user "$OWNER" \ --user "$OWNER" \
--repo "$REPO" \ --repo "$REPO" \
--tag "$CURRENT" \ --tag "$CURRENT"
--description "$description" ||
github-release edit \
--user "$OWNER" \
--repo "$REPO" \
--tag "$CURRENT" \
--description "$description"
} }
upload() { upload() {
mapfile -t files < <(find ./build -mindepth 1 -maxdepth 1) while IFS= read -r -d $'\0'; do
for file in "${files[@]}"; do file=$REPLY
BINARY=$(basename "${file}") BINARY=$(basename "${file}")
echo "--> ${BINARY}" echo "--> ${BINARY}"
github-release upload \ github-release upload \
--replace \
--user "$OWNER" \ --user "$OWNER" \
--repo "$REPO" \ --repo "$REPO" \
--tag "$CURRENT" \ --tag "$CURRENT" \
--name "${BINARY}" \ --name "${BINARY}" \
--file "$file" --file "$file"
done done < <(find ./build -mindepth 1 -maxdepth 1 -print0)
} }
release release

View File

@@ -1,5 +1,5 @@
name: yq name: yq
version: 1.14.1 version: 2.0.0
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.

View File

@@ -9,8 +9,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/spf13/cobra" yaml "gopkg.in/mikefarah/yaml.v2"
yaml "gopkg.in/yaml.v2" "gopkg.in/spf13/cobra.v0"
) )
type resulter struct { type resulter struct {

52
vendor/vendor.json vendored
View File

@@ -2,12 +2,6 @@
"comment": "", "comment": "",
"ignore": "test", "ignore": "test",
"package": [ "package": [
{
"checksumSHA1": "66lykxpWgSmQodnhkADqn6tnroQ=",
"path": "github.com/imdario/mergo",
"revision": "e3000cb3d28c72b837601cac94debd91032d19fe",
"revisionTime": "2017-06-20T10:47:01Z"
},
{ {
"checksumSHA1": "40vJyUB4ezQSn/NSadsKEOrudMc=", "checksumSHA1": "40vJyUB4ezQSn/NSadsKEOrudMc=",
"path": "github.com/inconshreveable/mousetrap", "path": "github.com/inconshreveable/mousetrap",
@@ -15,28 +9,40 @@
"revisionTime": "2014-10-17T20:07:13Z" "revisionTime": "2014-10-17T20:07:13Z"
}, },
{ {
"checksumSHA1": "BoXdUBWB8UnSlFlbnuTQaPqfCGk=", "checksumSHA1": "ljd3FhYRJ91cLZz3wsH9BQQ2JbA=",
"path": "github.com/op/go-logging", "path": "github.com/pkg/errors",
"revision": "970db520ece77730c7e4724c61121037378659d9", "revision": "816c9085562cd7ee03e7f8188a1cfd942858cded",
"revisionTime": "2016-03-15T20:05:05Z" "revisionTime": "2018-03-11T21:45:15Z"
}, },
{ {
"checksumSHA1": "xPKgXygsORkmXnLdtFaFmipYKaA=", "checksumSHA1": "OJI0OgC5V8gZtfS1e0CDYMhkDNc=",
"path": "github.com/spf13/cobra",
"revision": "b78744579491c1ceeaaa3b40205e56b0591b93a3",
"revisionTime": "2017-09-05T17:20:51Z"
},
{
"checksumSHA1": "Q52Y7t0lEtk/wcDn5q7tS7B+jqs=",
"path": "github.com/spf13/pflag", "path": "github.com/spf13/pflag",
"revision": "7aff26db30c1be810f9de5038ec5ef96ac41fd7c", "revision": "3ebe029320b2676d667ae88da602a5f854788a8a",
"revisionTime": "2017-08-24T17:57:12Z" "revisionTime": "2018-06-01T13:25:42Z"
}, },
{ {
"checksumSHA1": "RDJpJQwkF012L6m/2BJizyOksNw=", "checksumSHA1": "RwlkCZz8VFXAE4aHQQOSC0hLu5k=",
"path": "gopkg.in/yaml.v2", "path": "gopkg.in/imdario/mergo.v0",
"revision": "eb3733d160e74a9c7e442f435eb3bea458e1d19f", "revision": "9316a62528ac99aaecb4e47eadd6dc8aa6533d58",
"revisionTime": "2017-08-12T16:00:11Z" "revisionTime": "2018-06-08T14:01:56Z"
},
{
"checksumSHA1": "7wtGubs4v7+RZovtlmyT9KwA/gE=",
"path": "gopkg.in/mikefarah/yaml.v2",
"revision": "e175af14aaa1d0eff2ee04b691e4a4827a111416",
"revisionTime": "2018-06-13T04:05:11Z"
},
{
"checksumSHA1": "rL5r44ASTGubGW88gqQwlvVQshw=",
"path": "gopkg.in/op/go-logging.v1",
"revision": "b2cb9fa56473e98db8caba80237377e83fe44db5",
"revisionTime": "2016-02-11T21:21:56Z"
},
{
"checksumSHA1": "xsZjAbfLrXcMtY6fyQ8QC6EvJD0=",
"path": "gopkg.in/spf13/cobra.v0",
"revision": "ef82de70bb3f60c65fb8eebacbb2d122ef517385",
"revisionTime": "2018-04-27T13:45:50Z"
} }
], ],
"rootPath": "github.com/mikefarah/yq" "rootPath": "github.com/mikefarah/yq"

View File

@@ -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 = "1.15.0" Version = "2.1.0"
// 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

View File

@@ -3,13 +3,18 @@ package main
import "testing" import "testing"
func TestGetVersionDisplay(t *testing.T) { func TestGetVersionDisplay(t *testing.T) {
var expectedVersion = ProductName + " version " + Version
if VersionPrerelease != "" {
expectedVersion = expectedVersion + "-" + VersionPrerelease
}
expectedVersion = expectedVersion + "\n"
tests := []struct { tests := []struct {
name string name string
want string want string
}{ }{
{ {
name: "Display Version", name: "Display Version",
want: ProductName + " version " + Version + "\n", want: expectedVersion,
}, },
} }
for _, tt := range tests { for _, tt := range tests {

519
yq.go
View File

@@ -1,16 +1,20 @@
package main package main
import ( import (
"errors" "bufio"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect"
"strconv" "strconv"
"strings" "strings"
logging "github.com/op/go-logging" errors "github.com/pkg/errors"
"github.com/spf13/cobra" "gopkg.in/spf13/cobra.v0"
yaml "gopkg.in/yaml.v2"
yaml "gopkg.in/mikefarah/yaml.v2"
logging "gopkg.in/op/go-logging.v1"
) )
var trimOutput = true var trimOutput = true
@@ -18,19 +22,22 @@ var writeInplace = false
var writeScript = "" var writeScript = ""
var outputToJSON = false var outputToJSON = false
var overwriteFlag = false var overwriteFlag = false
var appendFlag = false
var verbose = false var verbose = false
var version = false var version = false
var docIndex = "0"
var log = logging.MustGetLogger("yq") var log = logging.MustGetLogger("yq")
func main() { func main() {
cmd := newCommandCLI() cmd := newCommandCLI()
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
fmt.Println(err.Error()) log.Error(err.Error())
os.Exit(1) os.Exit(1)
} }
} }
func newCommandCLI() *cobra.Command { func newCommandCLI() *cobra.Command {
yaml.DefaultMapType = reflect.TypeOf(yaml.MapSlice{})
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "yq", Use: "yq",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -60,7 +67,6 @@ func newCommandCLI() *cobra.Command {
} }
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit")
@@ -77,35 +83,38 @@ func newCommandCLI() *cobra.Command {
} }
func createReadCmd() *cobra.Command { func createReadCmd() *cobra.Command {
return &cobra.Command{ var cmdRead = &cobra.Command{
Use: "read [yaml_file] [path]", Use: "read [yaml_file] [path]",
Aliases: []string{"r"}, Aliases: []string{"r"},
Short: "yq r sample.yaml a.b.c", Short: "yq r [--doc/-d index] sample.yaml a.b.c",
Example: ` Example: `
yq read things.yaml a.b.c yq read things.yaml a.b.c
yq r - a.b.c (reads from stdin) yq r - a.b.c (reads from stdin)
yq r things.yaml a.*.c yq r things.yaml a.*.c
yq r things.yaml a.array[0].blah yq r -d1 things.yaml a.array[0].blah
yq r things.yaml a.array[*].blah yq r things.yaml a.array[*].blah
`, `,
Long: "Outputs the value of the given path in the yaml file to STDOUT", Long: "Outputs the value of the given path in the yaml file to STDOUT",
RunE: readProperty, RunE: readProperty,
} }
cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
cmdRead.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
return cmdRead
} }
func createWriteCmd() *cobra.Command { func createWriteCmd() *cobra.Command {
var cmdWrite = &cobra.Command{ var cmdWrite = &cobra.Command{
Use: "write [yaml_file] [path] [value]", Use: "write [yaml_file] [path] [value]",
Aliases: []string{"w"}, Aliases: []string{"w"},
Short: "yq w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC", Short: "yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml a.b.c newValue",
Example: ` Example: `
yq write things.yaml a.b.c cat yq write things.yaml a.b.c cat
yq write --inplace things.yaml a.b.c cat yq write --inplace things.yaml a.b.c cat
yq w -i things.yaml a.b.c cat yq w -i things.yaml a.b.c cat
yq w --script update_script.yaml things.yaml yq w --script update_script.yaml things.yaml
yq w -i -s update_script.yaml things.yaml yq w -i -s update_script.yaml things.yaml
yq w things.yaml a.b.d[+] foo yq w --doc 2 things.yaml a.b.d[+] foo
yq w things.yaml a.b.d[+] foo yq w -d2 things.yaml a.b.d[+] foo
`, `,
Long: `Updates the yaml file w.r.t the given path and value. Long: `Updates the yaml file w.r.t the given path and value.
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead. Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
@@ -124,6 +133,7 @@ a.b.e:
} }
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml") cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
cmdWrite.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
return cmdWrite return cmdWrite
} }
@@ -131,7 +141,7 @@ func createDeleteCmd() *cobra.Command {
var cmdDelete = &cobra.Command{ var cmdDelete = &cobra.Command{
Use: "delete [yaml_file] [path]", Use: "delete [yaml_file] [path]",
Aliases: []string{"d"}, Aliases: []string{"d"},
Short: "yq d [--inplace/-i] sample.yaml a.b.c", Short: "yq d [--inplace/-i] [--doc/-d index] sample.yaml a.b.c",
Example: ` Example: `
yq delete things.yaml a.b.c yq delete things.yaml a.b.c
yq delete --inplace things.yaml a.b.c yq delete --inplace things.yaml a.b.c
@@ -144,6 +154,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
RunE: deleteProperty, RunE: deleteProperty,
} }
cmdDelete.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdDelete.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdDelete.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
return cmdDelete return cmdDelete
} }
@@ -151,7 +162,7 @@ func createNewCmd() *cobra.Command {
var cmdNew = &cobra.Command{ var cmdNew = &cobra.Command{
Use: "new [path] [value]", Use: "new [path] [value]",
Aliases: []string{"n"}, Aliases: []string{"n"},
Short: "yq n [--script/-s script_file] a.b.c newValueForC", Short: "yq n [--script/-s script_file] a.b.c newValue",
Example: ` Example: `
yq new a.b.c cat yq new a.b.c cat
yq n a.b.c cat yq n a.b.c cat
@@ -173,32 +184,83 @@ func createMergeCmd() *cobra.Command {
var cmdMerge = &cobra.Command{ var cmdMerge = &cobra.Command{
Use: "merge [initial_yaml_file] [additional_yaml_file]...", Use: "merge [initial_yaml_file] [additional_yaml_file]...",
Aliases: []string{"m"}, Aliases: []string{"m"},
Short: "yq m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml", Short: "yq m [--inplace/-i] [--doc/-d index] [--overwrite/-x] [--append/-a] sample.yaml sample2.yaml",
Example: ` Example: `
yq merge things.yaml other.yaml yq merge things.yaml other.yaml
yq merge --inplace things.yaml other.yaml yq merge --inplace things.yaml other.yaml
yq m -i things.yaml other.yaml yq m -i things.yaml other.yaml
yq m --overwrite things.yaml other.yaml yq m --overwrite things.yaml other.yaml
yq m -i -x things.yaml other.yaml yq m -i -x things.yaml other.yaml
yq m -i -a things.yaml other.yaml
`, `,
Long: `Updates the yaml file by adding/updating the path(s) and value(s) from additional yaml file(s). Long: `Updates the yaml file by adding/updating the path(s) and value(s) from additional yaml file(s).
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead. Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
If overwrite flag is set then existing values will be overwritten using the values from each additional yaml file. If overwrite flag is set then existing values will be overwritten using the values from each additional yaml file.
If append flag is set then existing arrays will be merged with the arrays from each additional yaml file.
Note that if you set both flags only overwrite will take effect.
`, `,
RunE: mergeProperties, RunE: mergeProperties,
} }
cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values") cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values")
cmdMerge.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "update the yaml file by appending array values")
cmdMerge.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
return cmdMerge return cmdMerge
} }
func readProperty(cmd *cobra.Command, args []string) error { func readProperty(cmd *cobra.Command, args []string) error {
data, err := read(args) var path = ""
if err != nil {
return err if len(args) < 1 {
return errors.New("Must provide filename")
} else if len(args) > 1 {
path = args[1]
} }
dataStr, err := toString(data)
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
if errorParsingDocIndex != nil {
return errorParsingDocIndex
}
var mappedDocs []interface{}
var dataBucket interface{}
var currentIndex = 0
var errorReadingStream = readStream(args[0], func(decoder *yaml.Decoder) error {
for {
errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF {
log.Debugf("done %v / %v", currentIndex, docIndexInt)
if !updateAll && currentIndex <= docIndexInt {
return fmt.Errorf("Asked to process document index %v but there are only %v document(s)", docIndex, currentIndex)
}
return nil
}
log.Debugf("processing %v - requested index %v", currentIndex, docIndexInt)
if updateAll || currentIndex == docIndexInt {
log.Debugf("reading %v in index %v", path, currentIndex)
mappedDoc, errorParsing := readPath(dataBucket, path)
log.Debugf("%v", mappedDoc)
if errorParsing != nil {
return errors.Wrapf(errorParsing, "Error reading path in document index %v", currentIndex)
}
mappedDocs = append(mappedDocs, mappedDoc)
}
currentIndex = currentIndex + 1
}
})
if errorReadingStream != nil {
return errorReadingStream
}
if !updateAll {
dataBucket = mappedDocs[0]
} else {
dataBucket = mappedDocs
}
dataStr, err := toString(dataBucket)
if err != nil { if err != nil {
return err return err
} }
@@ -206,76 +268,13 @@ func readProperty(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func read(args []string) (interface{}, error) { func readPath(dataBucket interface{}, path string) (interface{}, error) {
var parsedData yaml.MapSlice
var path = ""
if len(args) < 1 {
return nil, errors.New("Must provide filename")
} else if len(args) > 1 {
path = args[1]
}
if err := readData(args[0], &parsedData); err != nil {
var generalData interface{}
if err = readData(args[0], &generalData); err != nil {
return nil, err
}
item := yaml.MapItem{Key: "thing", Value: generalData}
parsedData = yaml.MapSlice{item}
path = "thing." + path
}
if parsedData != nil && parsedData[0].Key == nil {
var interfaceData []map[interface{}]interface{}
if err := readData(args[0], &interfaceData); err == nil {
var listMap []yaml.MapSlice
for _, item := range interfaceData {
listMap = append(listMap, mapToMapSlice(item))
}
return readYamlArray(listMap, path)
}
}
if path == "" { if path == "" {
return parsedData, nil log.Debug("no path")
return dataBucket, nil
} }
var paths = parsePath(path) var paths = parsePath(path)
return recurse(dataBucket, paths[0], paths[1:])
return readMap(parsedData, paths[0], paths[1:])
}
func readYamlArray(listMap []yaml.MapSlice, path string) (interface{}, error) {
if path == "" {
return listMap, nil
}
var paths = parsePath(path)
if paths[0] == "*" {
if len(paths[1:]) == 0 {
return listMap, nil
}
var results []interface{}
for _, m := range listMap {
value, err := readMap(m, paths[1], paths[2:])
if err != nil {
return nil, err
}
results = append(results, value)
}
return results, nil
}
index, err := strconv.ParseInt(paths[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("Error accessing array: %v", err)
}
if len(paths[1:]) == 0 {
return listMap[index], nil
}
return readMap(listMap[index], paths[1], paths[2:])
} }
func newProperty(cmd *cobra.Command, args []string) error { func newProperty(cmd *cobra.Command, args []string) error {
@@ -292,159 +291,210 @@ func newProperty(cmd *cobra.Command, args []string) error {
} }
func newYaml(args []string) (interface{}, error) { func newYaml(args []string) (interface{}, error) {
var writeCommands yaml.MapSlice var writeCommands, writeCommandsError = readWriteCommands(args, 2, "Must provide <path_to_update> <value>")
if writeScript != "" { if writeCommandsError != nil {
if err := readData(writeScript, &writeCommands); err != nil { return nil, writeCommandsError
return nil, err
}
} else if len(args) < 2 {
return nil, errors.New("Must provide <path_to_update> <value>")
} else {
writeCommands = make(yaml.MapSlice, 1)
writeCommands[0] = yaml.MapItem{Key: args[0], Value: parseValue(args[1])}
} }
var parsedData yaml.MapSlice var dataBucket interface{}
var prependCommand = ""
var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[") var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[")
if isArray { if isArray {
item := yaml.MapItem{Key: "thing", Value: make(yaml.MapSlice, 0)} dataBucket = make([]interface{}, 0)
parsedData = yaml.MapSlice{item}
prependCommand = "thing"
} else { } else {
parsedData = make(yaml.MapSlice, 0) dataBucket = make(yaml.MapSlice, 0)
} }
return updateParsedData(parsedData, writeCommands, prependCommand) for _, entry := range writeCommands {
path := entry.Key.(string)
value := entry.Value
log.Debugf("setting %v to %v", path, value)
var paths = parsePath(path)
dataBucket = updatedChildValue(dataBucket, paths, value)
}
return dataBucket, nil
}
func parseDocumentIndex() (bool, int, error) {
if docIndex == "*" {
return true, -1, nil
}
docIndexInt64, err := strconv.ParseInt(docIndex, 10, 32)
if err != nil {
return false, -1, errors.Wrapf(err, "Document index %v is not a integer or *", docIndex)
}
return false, int(docIndexInt64), nil
}
type updateDataFn func(dataBucket interface{}, currentIndex int) (interface{}, error)
func mapYamlDecoder(updateData updateDataFn, encoder *yaml.Encoder) yamlDecoderFn {
return func(decoder *yaml.Decoder) error {
var dataBucket interface{}
var errorReading error
var errorWriting error
var errorUpdating error
var currentIndex = 0
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
if errorParsingDocIndex != nil {
return errorParsingDocIndex
}
for {
log.Debugf("Read doc %v", currentIndex)
errorReading = decoder.Decode(&dataBucket)
if errorReading == io.EOF {
if !updateAll && currentIndex <= docIndexInt {
return fmt.Errorf("Asked to process document index %v but there are only %v document(s)", docIndex, currentIndex)
}
return nil
} else if errorReading != nil {
return errors.Wrapf(errorReading, "Error reading document at index %v, %v", currentIndex, errorReading)
}
dataBucket, errorUpdating = updateData(dataBucket, currentIndex)
if errorUpdating != nil {
return errors.Wrapf(errorUpdating, "Error updating document at index %v", currentIndex)
}
errorWriting = encoder.Encode(dataBucket)
if errorWriting != nil {
return errors.Wrapf(errorWriting, "Error writing document at index %v, %v", currentIndex, errorWriting)
}
currentIndex = currentIndex + 1
}
}
} }
func writeProperty(cmd *cobra.Command, args []string) error { func writeProperty(cmd *cobra.Command, args []string) error {
updatedData, err := updateYaml(args) var writeCommands, writeCommandsError = readWriteCommands(args, 3, "Must provide <filename> <path_to_update> <value>")
if err != nil { if writeCommandsError != nil {
return err return writeCommandsError
} }
return write(cmd, args[0], updatedData) var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
if errorParsingDocIndex != nil {
return errorParsingDocIndex
}
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
if updateAll || currentIndex == docIndexInt {
log.Debugf("Updating doc %v", currentIndex)
for _, entry := range writeCommands {
path := entry.Key.(string)
value := entry.Value
log.Debugf("setting %v to %v", path, value)
var paths = parsePath(path)
dataBucket = updatedChildValue(dataBucket, paths, value)
}
}
return dataBucket, nil
}
return readAndUpdate(cmd.OutOrStdout(), args[0], updateData)
} }
func write(cmd *cobra.Command, filename string, updatedData interface{}) error { func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) error {
var destination io.Writer
var destinationName string
if writeInplace { if writeInplace {
dataStr, err := yamlToString(updatedData) var tempFile, err = ioutil.TempFile("", "temp")
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filename, []byte(dataStr), 0644) destinationName = tempFile.Name()
destination = tempFile
defer func() {
safelyCloseFile(tempFile)
safelyRenameFile(tempFile.Name(), inputFile)
}()
} else {
var writer = bufio.NewWriter(stdOut)
destination = writer
destinationName = "Stdout"
defer safelyFlush(writer)
} }
dataStr, err := toString(updatedData) var encoder = yaml.NewEncoder(destination)
if err != nil { log.Debugf("Writing to %v from %v", destinationName, inputFile)
return err return readStream(inputFile, mapYamlDecoder(updateData, encoder))
}
cmd.Println(dataStr)
return nil
} }
func deleteProperty(cmd *cobra.Command, args []string) error { func deleteProperty(cmd *cobra.Command, args []string) error {
updatedData, err := deleteYaml(args)
if err != nil {
return err
}
return write(cmd, args[0], updatedData)
}
func deleteYaml(args []string) (interface{}, error) {
var parsedData yaml.MapSlice
var deletePath string
if len(args) < 2 { if len(args) < 2 {
return nil, errors.New("Must provide <filename> <path_to_delete>") return errors.New("Must provide <filename> <path_to_delete>")
}
var deletePath = args[1]
var paths = parsePath(deletePath)
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
if errorParsingDocIndex != nil {
return errorParsingDocIndex
} }
deletePath = args[1] var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
if updateAll || currentIndex == docIndexInt {
if err := readData(args[0], &parsedData); err != nil { log.Debugf("Deleting path in doc %v", currentIndex)
var generalData interface{} return deleteChildValue(dataBucket, paths), nil
if err = readData(args[0], &generalData); err != nil {
return nil, err
} }
item := yaml.MapItem{Key: "thing", Value: generalData} return dataBucket, nil
parsedData = yaml.MapSlice{item}
deletePath = "thing." + deletePath
} }
path := parsePath(deletePath) return readAndUpdate(cmd.OutOrStdout(), args[0], updateData)
return deleteMap(parsedData, path), nil
} }
func mergeProperties(cmd *cobra.Command, args []string) error { func mergeProperties(cmd *cobra.Command, args []string) error {
if len(args) < 2 { if len(args) < 2 {
return errors.New("Must provide at least 2 yaml files") return errors.New("Must provide at least 2 yaml files")
} }
var input = args[0]
updatedData, err := mergeYaml(args) var filesToMerge = args[1:]
if err != nil { var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
return err if errorParsingDocIndex != nil {
return errorParsingDocIndex
} }
return write(cmd, args[0], updatedData)
}
func mergeYaml(args []string) (interface{}, error) { var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
var updatedData map[interface{}]interface{} if updateAll || currentIndex == docIndexInt {
log.Debugf("Merging doc %v", currentIndex)
for _, f := range args { var mergedData map[interface{}]interface{}
var parsedData map[interface{}]interface{} // merge only works for maps, so put everything in a temporary
if err := readData(f, &parsedData); err != nil { // map
return nil, err var mapDataBucket = make(map[interface{}]interface{})
} mapDataBucket["root"] = dataBucket
if err := merge(&updatedData, parsedData, overwriteFlag); err != nil { if err := merge(&mergedData, mapDataBucket, overwriteFlag, appendFlag); err != nil {
return nil, err return nil, err
}
for _, f := range filesToMerge {
var fileToMerge interface{}
if err := readData(f, 0, &fileToMerge); err != nil {
return nil, err
}
mapDataBucket["root"] = fileToMerge
if err := merge(&mergedData, mapDataBucket, overwriteFlag, appendFlag); err != nil {
return nil, err
}
}
return mergedData["root"], nil
} }
return dataBucket, nil
} }
yaml.DefaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
return mapToMapSlice(updatedData), nil defer func() { yaml.DefaultMapType = reflect.TypeOf(yaml.MapSlice{}) }()
return readAndUpdate(cmd.OutOrStdout(), input, updateData)
} }
func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice, prependCommand string) (interface{}, error) { func readWriteCommands(args []string, expectedArgs int, badArgsMessage string) (yaml.MapSlice, error) {
var prefix = ""
if prependCommand != "" {
prefix = prependCommand + "."
}
for _, entry := range writeCommands {
path := prefix + entry.Key.(string)
value := entry.Value
var paths = parsePath(path)
parsedData = writeMap(parsedData, paths, value)
}
if prependCommand != "" {
return readMap(parsedData, prependCommand, make([]string, 0))
}
return parsedData, nil
}
func updateYaml(args []string) (interface{}, error) {
var writeCommands yaml.MapSlice var writeCommands yaml.MapSlice
var prependCommand = ""
if writeScript != "" { if writeScript != "" {
if err := readData(writeScript, &writeCommands); err != nil { if err := readData(writeScript, 0, &writeCommands); err != nil {
return nil, err return nil, err
} }
} else if len(args) < 3 { } else if len(args) < expectedArgs {
return nil, errors.New("Must provide <filename> <path_to_update> <value>") return nil, errors.New(badArgsMessage)
} else { } else {
writeCommands = make(yaml.MapSlice, 1) writeCommands = make(yaml.MapSlice, 1)
writeCommands[0] = yaml.MapItem{Key: args[1], Value: parseValue(args[2])} writeCommands[0] = yaml.MapItem{Key: args[expectedArgs-2], Value: parseValue(args[expectedArgs-1])}
} }
return writeCommands, nil
var parsedData yaml.MapSlice
if err := readData(args[0], &parsedData); err != nil {
var generalData interface{}
if err = readData(args[0], &generalData); err != nil {
return nil, err
}
item := yaml.MapItem{Key: "thing", Value: generalData}
parsedData = yaml.MapSlice{item}
prependCommand = "thing"
}
return updateParsedData(parsedData, writeCommands, prependCommand)
} }
func parseValue(argument string) interface{} { func parseValue(argument string) interface{} {
@@ -487,7 +537,7 @@ func marshalContext(context interface{}) (string, error) {
out, err := yaml.Marshal(context) out, err := yaml.Marshal(context)
if err != nil { if err != nil {
return "", fmt.Errorf("error printing yaml: %v", err) return "", errors.Wrap(err, "error printing yaml")
} }
outStr := string(out) outStr := string(out)
@@ -499,22 +549,81 @@ func marshalContext(context interface{}) (string, error) {
return outStr, nil return outStr, nil
} }
func readData(filename string, parsedData interface{}) error { func safelyRenameFile(from string, to string) {
if renameError := os.Rename(from, to); renameError != nil {
log.Warningf("Error renaming from %v to %v, attemting to copy contents", from, to)
log.Warning(renameError.Error())
// can't do this rename when running in docker to a file targeted in a mounted volume,
// so gracefully degrade to copying the entire contents.
if copyError := copyFileContents(from, to); copyError != nil {
log.Errorf("Failed copying from %v to %v", from, to)
log.Errorf(copyError.Error())
}
}
}
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return err
}
defer safelyCloseFile(in)
out, err := os.Create(dst)
if err != nil {
return err
}
defer safelyCloseFile(out)
if _, err = io.Copy(out, in); err != nil {
return err
}
return out.Sync()
}
func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil {
log.Error("Error flushing writer!")
log.Error(err.Error())
}
}
func safelyCloseFile(file *os.File) {
err := file.Close()
if err != nil {
log.Error("Error closing file!")
log.Error(err.Error())
}
}
type yamlDecoderFn func(*yaml.Decoder) error
func readStream(filename string, yamlDecoder yamlDecoderFn) error {
if filename == "" { if filename == "" {
return errors.New("Must provide filename") return errors.New("Must provide filename")
} }
var rawData []byte var stream io.Reader
var err error
if filename == "-" { if filename == "-" {
rawData, err = ioutil.ReadAll(os.Stdin) stream = bufio.NewReader(os.Stdin)
} else { } else {
rawData, err = ioutil.ReadFile(filename) file, err := os.Open(filename)
if err != nil {
return err
}
defer safelyCloseFile(file)
stream = file
} }
return yamlDecoder(yaml.NewDecoder(stream))
if err != nil { }
return err
} func readData(filename string, indexToRead int, parsedData interface{}) error {
return readStream(filename, func(decoder *yaml.Decoder) error {
return yaml.Unmarshal(rawData, parsedData) for currentIndex := 0; currentIndex < indexToRead; currentIndex++ {
errorSkipping := decoder.Decode(parsedData)
if errorSkipping != nil {
return errors.Wrapf(errorSkipping, "Error processing document at index %v, %v", currentIndex, errorSkipping)
}
}
return decoder.Decode(parsedData)
})
} }

View File

@@ -23,30 +23,6 @@ func TestParseValue(t *testing.T) {
} }
} }
func TestRead(t *testing.T) {
result, _ := read([]string{"examples/sample.yaml", "b.c"})
assertResult(t, 2, result)
}
func TestReadArray(t *testing.T) {
result, _ := read([]string{"examples/sample_array.yaml", "[1]"})
assertResult(t, 2, result)
}
func TestReadString(t *testing.T) {
result, _ := read([]string{"examples/sample_text.yaml"})
assertResult(t, "hi", result)
}
func TestOrder(t *testing.T) {
result, _ := read([]string{"examples/order.yaml"})
formattedResult, _ := yamlToString(result)
assertResult(t,
`version: 3
application: MyApp`,
formattedResult)
}
func TestMultilineString(t *testing.T) { func TestMultilineString(t *testing.T) {
testString := ` testString := `
abcd abcd
@@ -71,37 +47,6 @@ func TestNewYamlArray(t *testing.T) {
formattedResult) formattedResult)
} }
func TestUpdateYaml(t *testing.T) {
result, _ := updateYaml([]string{"examples/sample.yaml", "b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{a Easy! as one two three} {b [{c 3} {d [3 4]} {e [[{name fred} {value 3}] [{name sam} {value 4}]]}]}]",
formattedResult)
}
func TestUpdateYamlArray(t *testing.T) {
result, _ := updateYaml([]string{"examples/sample_array.yaml", "[0]", "3"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[3 2 3]",
formattedResult)
}
func TestUpdateYaml_WithScript(t *testing.T) {
writeScript = "examples/instruction_sample.yaml"
_, _ = updateYaml([]string{"examples/sample.yaml"})
}
func TestUpdateYaml_WithUnknownScript(t *testing.T) {
writeScript = "fake-unknown"
_, err := updateYaml([]string{"examples/sample.yaml"})
if err == nil {
t.Error("Expected error due to unknown file")
}
expectedOutput := `open fake-unknown: no such file or directory`
assertResult(t, expectedOutput, err.Error())
}
func TestNewYaml_WithScript(t *testing.T) { func TestNewYaml_WithScript(t *testing.T) {
writeScript = "examples/instruction_sample.yaml" writeScript = "examples/instruction_sample.yaml"
expectedResult := `b: expectedResult := `b:
@@ -122,11 +67,3 @@ func TestNewYaml_WithUnknownScript(t *testing.T) {
expectedOutput := `open fake-unknown: no such file or directory` expectedOutput := `open fake-unknown: no such file or directory`
assertResult(t, expectedOutput, err.Error()) assertResult(t, expectedOutput, err.Error())
} }
func TestDeleteYaml(t *testing.T) {
result, _ := deleteYaml([]string{"examples/sample.yaml", "b.c"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{a Easy! as one two three} {b [{d [3 4]} {e [[{name fred} {value 3}] [{name sam} {value 4}]]}]}]",
formattedResult)
}