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

Compare commits

..

11 Commits

Author SHA1 Message Date
Mike Farah
cb48ba7173 Rename to yq 2017-12-18 15:29:41 +11:00
Mike Farah
dc4f8a6adb Fixed vendor scripts 2017-12-18 15:29:41 +11:00
Mike Farah
1f6d7a50b2 Clearer README 2017-12-18 15:29:41 +11:00
Kenny Jones
6fc3bdf58f Merge pull request #103 from mikefarah/task/upgrade-to-golang1.9
Task: Upgrade to Golang 1.9
2017-09-27 07:07:16 -04:00
kenjones
87b33e8603 Task: Upgrade to Golang 1.9 2017-09-27 07:05:18 -04:00
kenjones
cc7eb84388 Task: Add docs on keys with dots
Adds documentation on how to use a key lookup indicator for dealing
with keys that have dots when specifying a path as part of create,
read, and write commands.

Resolves: #22
2017-09-25 22:36:49 -04:00
kenjones
9e3f8ebd0a Task: Prepare for development 2017-09-25 09:41:27 -04:00
kenjones
28bcbd321f Release v1.13.1 2017-09-25 09:35:23 -04:00
Kenny Jones
9dd4503b23 Merge pull request #42 from mikefarah/bugfix/append-empty-array
Bugfix: Append when array empty results in null value
2017-09-25 09:34:00 -04:00
kenjones
185d1faadb Bugfix: Append when array empty results in null value
Initialize new array as empty instead of having a single value.

Resolves: #41
2017-09-25 09:31:06 -04:00
kenjones
7b004cb456 Task: Prepare for development 2017-09-24 21:28:28 -04:00
26 changed files with 240 additions and 127 deletions

1
.gitignore vendored
View File

@@ -29,3 +29,4 @@ yaml
vendor/*/ vendor/*/
tmp/ tmp/
cover/ cover/
yq

View File

@@ -1,6 +1,6 @@
language: go language: go
go: go:
- 1.7.x - 1.9.x
script: script:
- scripts/devtools.sh - scripts/devtools.sh
- make local build - make local build

View File

@@ -1,4 +1,4 @@
FROM golang:1.7 FROM golang:1.9
COPY scripts/devtools.sh /opt/devtools.sh COPY scripts/devtools.sh /opt/devtools.sh
@@ -14,6 +14,8 @@ RUN set -ex \
&& apt-get update && apt-get install -y --no-install-recommends \ && apt-get update && apt-get install -y --no-install-recommends \
$buildDeps \ $buildDeps \
python2.7 \ python2.7 \
python-setuptools \
python-wheel \
python-pip \ python-pip \
&& pip install --upgrade \ && pip install --upgrade \
pip \ pip \
@@ -25,4 +27,4 @@ RUN set -ex \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
ENV CGO_ENABLED 0 ENV CGO_ENABLED 0
ENV GOPATH /go:/yaml ENV GOPATH /go:/yq

View File

@@ -30,10 +30,6 @@ help:
clean: clean:
@rm -rf bin build cover *.out @rm -rf bin build cover *.out
veryclean: clean
rm -rf tmp
find vendor/* -maxdepth 0 -ignore_readdir_race -type d -exec rm -rf {} \;
## prefix before other make targets to run in your local dev environment ## prefix before other make targets to run in your local dev environment
local: | quiet local: | quiet
@$(eval DOCKRUN= ) @$(eval DOCKRUN= )
@@ -57,7 +53,7 @@ build: build/dev
.PHONY: build/dev .PHONY: build/dev
build/dev: test *.go build/dev: test *.go
@mkdir -p bin/ @mkdir -p bin/
${DOCKRUN} go build -o bin/yaml --ldflags "$(LDFLAGS)" ${DOCKRUN} go build --ldflags "$(LDFLAGS)"
${DOCKRUN} bash ./scripts/acceptance.sh ${DOCKRUN} bash ./scripts/acceptance.sh
## Compile the project for multiple OS and Architectures. ## Compile the project for multiple OS and Architectures.
@@ -75,7 +71,7 @@ install: build
# Each of the fetch should be an entry within vendor.json; not currently included within project # Each of the fetch should be an entry within vendor.json; not currently included within project
.PHONY: vendor .PHONY: vendor
vendor: tmp/dev_image_id vendor: tmp/dev_image_id
${DOCKRUN} bash ./scripts/vendor.sh ${DOCKRUN} govendor sync
@chmod 664 vendor/vendor.json @chmod 664 vendor/vendor.json
# ---------------------------------------------- # ----------------------------------------------

View File

@@ -1,4 +1,4 @@
export PROJECT = yaml export PROJECT = yq
IMPORT_PATH := github.com/mikefarah/${PROJECT} IMPORT_PATH := github.com/mikefarah/${PROJECT}
export GIT_COMMIT = $(shell git rev-parse --short HEAD) export GIT_COMMIT = $(shell git rev-parse --short HEAD)

View File

@@ -1,12 +1,12 @@
# yaml [![Build Status](https://travis-ci.org/mikefarah/yaml.svg?branch=master)](https://travis-ci.org/mikefarah/yaml) # yq [![Build Status](https://travis-ci.org/mikefarah/yaml.svg?branch=master)](https://travis-ci.org/mikefarah/yaml)
yaml is a lightweight and portable command-line YAML processor 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. The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.
## Install ## Install
[Download latest binary](https://github.com/mikefarah/yaml/releases/latest) or alternatively: [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively:
``` ```
go get github.com/mikefarah/yaml go get github.com/mikefarah/yq
``` ```
## Features ## Features
@@ -23,35 +23,35 @@ go get github.com/mikefarah/yaml
- 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.
## [Usage](http://mikefarah.github.io/yaml/) ## [Usage](http://mikefarah.github.io/yq/)
Check out the [documentation](http://mikefarah.github.io/yaml/) for more detailed and advanced usage. Check out the [documentation](http://mikefarah.github.io/yq/) for more detailed and advanced usage.
``` ```
Usage: Usage:
yaml [flags] yq [flags]
yaml [command] yq [command]
Available Commands: Available Commands:
help Help about any command help Help about any command
merge yaml m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml merge yq m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml
new yaml n [--script/-s script_file] a.b.c newValueForC new yq n [--script/-s script_file] a.b.c newValueForC
read yaml r sample.yaml a.b.c read yq r sample.yaml a.b.c
write yaml w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC write yq w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC
Flags: Flags:
-h, --help help for yaml -h, --help help for yq
-j, --tojson output as json -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
Use "yaml [command] --help" for more information about a command. Use "yq [command] --help" for more information about a command.
``` ```
## Contribute ## Contribute
1. `make vendor` OR run `govendor sync` [link](https://github.com/kardianos/govendor) 1. `make [local] vendor`
2. add unit tests 2. add unit tests
3. apply changes 3. apply changes
4. `make` 4. `make [local] build`
5. profit 5. profit

View File

@@ -454,6 +454,24 @@ func TestWriteCmd_Append(t *testing.T) {
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestWriteCmd_AppendEmptyArray(t *testing.T) {
content := `a: 2
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s b[+] v", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
- v
`
assertResult(t, expectedOutput, result.Output)
}
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")

View File

@@ -71,7 +71,7 @@ func writeArray(context interface{}, paths []string, value interface{}) []interf
case []interface{}: case []interface{}:
array = context.([]interface{}) array = context.([]interface{})
default: default:
array = make([]interface{}, 1) array = make([]interface{}, 0)
} }
if len(paths) == 0 { if len(paths) == 0 {
@@ -196,24 +196,6 @@ func mapToMapSlice(data map[interface{}]interface{}) yaml.MapSlice {
// because the parsing of the yaml was done via a map the order will be inconsistent // because the parsing of the yaml was done via a map the order will be inconsistent
// apply order to allow a consistent output // apply order to allow a consistent output
// Only available in Go 1.8+ sort.SliceStable(mapSlice, func(i, j int) bool { return mapSlice[i].Key.(string) < mapSlice[j].Key.(string) })
// sort.SliceStable(mapSlice, func(i, j int) bool { return mapSlice[i].Key < mapSlice[j].Key })
sort.Sort(sortMap{mapSlice})
return mapSlice return mapSlice
} }
type sortMap struct {
ms yaml.MapSlice
}
func (m sortMap) Len() int {
return len(m.ms)
}
func (m sortMap) Less(i, j int) bool {
return m.ms[i].Key.(string) < m.ms[j].Key.(string)
}
func (m sortMap) Swap(i, j int) {
m.ms[i], m.ms[j] = m.ms[j], m.ms[i]
}

View File

@@ -257,6 +257,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -322,6 +329,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -373,6 +387,20 @@ b.e[0].name: Howdy Partner
<p>You can also pipe the instructions in:</p> <p>You can also pipe the instructions in:</p>
<pre><code class="bash">cat create_instructions.yaml | yaml n -s - <pre><code class="bash">cat create_instructions.yaml | yaml n -s -
</code></pre> </code></pre>
<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>
<pre><code class="yaml">b:
foo.bar: 7
</code></pre>
<pre><code class="bash">yaml r sample.yaml b[foo.bar]
</code></pre>
<pre><code class="bash">yaml w sample.yaml b[foo.bar] 9
</code></pre>
<p>Any valid yaml key can be specified as part of a key lookup.</p>

View File

@@ -22,7 +22,7 @@
}, },
{ {
"location": "/read/", "location": "/read/",
"text": "yaml r \nyaml_file|json_file\n \npath\n\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\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\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\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\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\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\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\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam", "text": "yaml r \nyaml_file|json_file\n \npath\n\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\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\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\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\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\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\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\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam\n\n\n\n\nKeys with dots\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.",
"title": "Read" "title": "Read"
}, },
{ {
@@ -55,9 +55,14 @@
"text": "e.g.: given a sample file of b:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4 then yaml r sample.yaml b.e[*].name will output: - fred\n- sam", "text": "e.g.: given a sample file of b:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4 then yaml r sample.yaml b.e[*].name will output: - fred\n- sam",
"title": "Array Splat" "title": "Array Splat"
}, },
{
"location": "/read/#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.",
"title": "Keys with dots"
},
{ {
"location": "/write/", "location": "/write/",
"text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\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\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml 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\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\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\nyaml w sample.yaml b.d[0] \nnew thing\n\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\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\nyaml w sample.yaml b.d[+] \nbar thing\n\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\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml 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\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\nyaml 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 | yaml w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyaml w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3", "text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\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\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml 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\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\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\nyaml w sample.yaml b.d[0] \nnew thing\n\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\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\nyaml w sample.yaml b.d[+] \nbar thing\n\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\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml 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\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\nyaml 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 | yaml w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyaml w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3\n\n\n\n\nKeys with dots\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.",
"title": "Write/Update" "title": "Write/Update"
}, },
{ {
@@ -95,9 +100,14 @@
"text": "The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags: yaml w -- my.path -3 will output my:\n path: -3", "text": "The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags: yaml w -- my.path -3 will output my:\n path: -3",
"title": "Values starting with a hyphen (or dash)" "title": "Values starting with a hyphen (or dash)"
}, },
{
"location": "/write/#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.",
"title": "Keys with dots"
},
{ {
"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\nyaml n \npath\n \nnew value\n\n\n\n\n\nCreating a simple yaml file\n\n\nyaml 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\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\nyaml 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 | yaml n -s -", "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\nyaml n \npath\n \nnew value\n\n\n\n\n\nCreating a simple yaml file\n\n\nyaml 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\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\nyaml 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 | yaml n -s -\n\n\n\n\nKeys with dots\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.",
"title": "Create" "title": "Create"
}, },
{ {
@@ -110,6 +120,11 @@
"text": "Create scripts follow the same format as the update scripts. Given a script create_instructions.yaml of: b.c: 3\nb.e[0].name: Howdy Partner then yaml n -s create_instructions.yaml will output: b:\n c: 3\n e:\n - name: Howdy Partner You can also pipe the instructions in: cat create_instructions.yaml | yaml n -s -", "text": "Create scripts follow the same format as the update scripts. Given a script create_instructions.yaml of: b.c: 3\nb.e[0].name: Howdy Partner then yaml n -s create_instructions.yaml will output: b:\n c: 3\n e:\n - name: Howdy Partner You can also pipe the instructions in: cat create_instructions.yaml | yaml n -s -",
"title": "Creating using a create script" "title": "Creating using a create script"
}, },
{
"location": "/create/#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.",
"title": "Keys with dots"
},
{ {
"location": "/convert/", "location": "/convert/",
"text": "Yaml to Json\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\nyaml r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\nb\n:{\nc\n:2}}\n\n\n\n\nJson to Yaml\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{\na\n:\nEasy! as one two three\n,\nb\n:{\nc\n:2,\nd\n:[3,4]}}\n\n\n\n\nthen\n\n\nyaml 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\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\nyaml r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\nb\n:{\nc\n:2}}\n\n\n\n\nJson to Yaml\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{\na\n:\nEasy! as one two three\n,\nb\n:{\nc\n:2,\nd\n:[3,4]}}\n\n\n\n\nthen\n\n\nyaml 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",

View File

@@ -261,6 +261,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -378,6 +385,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -480,6 +494,20 @@ e.g.: given a sample file of</p>
<pre><code>- fred <pre><code>- fred
- sam - sam
</code></pre> </code></pre>
<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>
<pre><code class="yaml">b:
foo.bar: 7
</code></pre>
<pre><code class="bash">yaml r sample.yaml b[foo.bar]
</code></pre>
<pre><code class="bash">yaml w sample.yaml b[foo.bar] 9
</code></pre>
<p>Any valid yaml key can be specified as part of a key lookup.</p>

View File

@@ -4,7 +4,7 @@
<url> <url>
<loc>/</loc> <loc>/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -12,7 +12,7 @@
<url> <url>
<loc>/read/</loc> <loc>/read/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -20,7 +20,7 @@
<url> <url>
<loc>/write/</loc> <loc>/write/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -28,7 +28,7 @@
<url> <url>
<loc>/create/</loc> <loc>/create/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -36,7 +36,7 @@
<url> <url>
<loc>/convert/</loc> <loc>/convert/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@@ -44,7 +44,7 @@
<url> <url>
<loc>/merge/</loc> <loc>/merge/</loc>
<lastmod>2017-09-24</lastmod> <lastmod>2017-09-26</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>

View File

@@ -280,6 +280,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -392,6 +399,13 @@
</li> </li>
<li class="md-nav__item">
<a href="#keys-with-dots" title="Keys with dots" class="md-nav__link">
Keys with dots
</a>
</li>
</ul> </ul>
@@ -520,6 +534,20 @@ b.e[0].name: Howdy Partner
<pre><code class="yaml">my: <pre><code class="yaml">my:
path: -3 path: -3
</code></pre> </code></pre>
<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>
<pre><code class="yaml">b:
foo.bar: 7
</code></pre>
<pre><code class="bash">yaml r sample.yaml b[foo.bar]
</code></pre>
<pre><code class="bash">yaml w sample.yaml b[foo.bar] 9
</code></pre>
<p>Any valid yaml key can be specified as part of a key lookup.</p>

View File

@@ -8,7 +8,7 @@ b:
``` ```
then then
```bash ```bash
yaml r -j sample.yaml b.c yq r -j sample.yaml b.c
``` ```
will output will output
@@ -26,7 +26,7 @@ e.g given a json file
``` ```
then then
```bash ```bash
yaml r sample.json yq r sample.json
``` ```
will output will output
```yaml ```yaml

View File

@@ -1,12 +1,12 @@
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.
``` ```
yaml n <path> <new value> yq n <path> <new value>
``` ```
### Creating a simple yaml file ### Creating a simple yaml file
```bash ```bash
yaml n b.c cat yq n b.c cat
``` ```
will output: will output:
```yaml ```yaml
@@ -25,7 +25,7 @@ b.e[0].name: Howdy Partner
then then
```bash ```bash
yaml n -s create_instructions.yaml yq n -s create_instructions.yaml
``` ```
will output: will output:
```yaml ```yaml
@@ -38,5 +38,7 @@ b:
You can also pipe the instructions in: You can also pipe the instructions in:
```bash ```bash
cat create_instructions.yaml | yaml n -s - cat create_instructions.yaml | yq n -s -
``` ```
{!snippets/keys_with_dots.md!}

View File

@@ -1,5 +1,5 @@
# yaml # yq
yaml is a lightweight and portable command-line YAML processor yq is 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. The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.
@@ -7,10 +7,10 @@ The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed
### get the source ### get the source
```bash ```bash
go get github.com/mikefarah/yaml go get github.com/mikefarah/yq
``` ```
[.zip](https://github.com/mikefarah/yaml/zipball/master) or [tar.gz](https://github.com/mikefarah/yaml/tarball/master) [.zip](https://github.com/mikefarah/yq/zipball/master) or [tar.gz](https://github.com/mikefarah/yq/tarball/master)
[View on GitHub](https://github.com/mikefarah/yaml) [View on GitHub](https://github.com/mikefarah/yq)

View File

@@ -2,7 +2,7 @@ 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.
``` ```
yaml m <yaml_file|json_file> <path>... yq m <yaml_file|json_file> <path>...
``` ```
{!snippets/works_with_json.md!} {!snippets/works_with_json.md!}
@@ -20,7 +20,7 @@ c:
``` ```
then then
```bash ```bash
yaml m data1.yaml data2.yaml yq m data1.yaml data2.yaml
``` ```
will output: will output:
```yaml ```yaml
@@ -44,7 +44,7 @@ c:
``` ```
then then
```bash ```bash
yaml m -i data1.yaml data2.yaml yq m -i data1.yaml data2.yaml
``` ```
will update the data1.yaml file so that the value of 'c' is 'test: 1'. will update the data1.yaml file so that the value of 'c' is 'test: 1'.
@@ -62,7 +62,7 @@ c:
``` ```
then then
```bash ```bash
yaml m -x data1.yaml data2.yaml yq m -x data1.yaml data2.yaml
``` ```
will output: will output:
```yaml ```yaml
@@ -88,7 +88,7 @@ d: false
``` ```
then then
```bash ```bash
yaml m -x data1.yaml data3.yaml yq m -x data1.yaml data3.yaml
``` ```
will output: will output:
```yaml ```yaml

View File

@@ -1,5 +1,5 @@
``` ```
yaml r <yaml_file|json_file> <path> yq r <yaml_file|json_file> <path>
``` ```
{!snippets/works_with_json.md!} {!snippets/works_with_json.md!}
@@ -12,14 +12,14 @@ b:
``` ```
then then
```bash ```bash
yaml r sample.yaml b.c yq r sample.yaml b.c
``` ```
will output the value of '2'. will output the value of '2'.
### From Stdin ### From Stdin
Given a sample.yaml file of: Given a sample.yaml file of:
```bash ```bash
cat sample.yaml | yaml r - b.c cat sample.yaml | yq r - b.c
``` ```
will output the value of '2'. will output the value of '2'.
@@ -35,7 +35,7 @@ bob:
``` ```
then then
```bash ```bash
yaml r sample.yaml bob.*.cats yq r sample.yaml bob.*.cats
``` ```
will output will output
```yaml ```yaml
@@ -51,7 +51,7 @@ b.x:
``` ```
then then
```bash ```bash
yaml r sample.yaml \"b.x\".c yq r sample.yaml \"b.x\".c
``` ```
will output the value of '2'. will output the value of '2'.
@@ -68,7 +68,7 @@ b:
``` ```
then then
``` ```
yaml r sample.yaml b.e[1].name yq r sample.yaml b.e[1].name
``` ```
will output 'sam' will output 'sam'
@@ -84,10 +84,12 @@ b:
``` ```
then then
``` ```
yaml r sample.yaml b.e[*].name yq r sample.yaml b.e[*].name
``` ```
will output: will output:
``` ```
- fred - fred
- sam - sam
``` ```
{!snippets/keys_with_dots.md!}

View File

@@ -0,0 +1,17 @@
### Keys with dots
When specifying a key that has a dot use key lookup indicator.
```yaml
b:
foo.bar: 7
```
```bash
yaml r sample.yaml b[foo.bar]
```
```bash
yaml w sample.yaml b[foo.bar] 9
```
Any valid yaml key can be specified as part of a key lookup.

View File

@@ -1,5 +1,5 @@
``` ```
yaml w <yaml_file|json_file> <path> <new value> yq w <yaml_file|json_file> <path> <new value>
``` ```
{!snippets/works_with_json.md!} {!snippets/works_with_json.md!}
@@ -11,7 +11,7 @@ b:
``` ```
then then
```bash ```bash
yaml w sample.yaml b.c cat yq w sample.yaml b.c cat
``` ```
will output: will output:
```yaml ```yaml
@@ -21,7 +21,7 @@ b:
### From STDIN ### From STDIN
```bash ```bash
cat sample.yaml | yaml w - b.c blah cat sample.yaml | yq w - b.c blah
``` ```
### Adding new fields ### Adding new fields
@@ -34,7 +34,7 @@ b:
``` ```
then then
```bash ```bash
yaml w sample.yaml b.d[0] "new thing" yq w sample.yaml b.d[0] "new thing"
``` ```
will output: will output:
```yaml ```yaml
@@ -55,7 +55,7 @@ b:
``` ```
then then
```bash ```bash
yaml w sample.yaml b.d[+] "bar thing" yq w sample.yaml b.d[+] "bar thing"
``` ```
will output: will output:
```yaml ```yaml
@@ -75,7 +75,7 @@ b:
``` ```
then then
```bash ```bash
yaml w -i sample.yaml b.c cat yq w -i sample.yaml b.c cat
``` ```
will update the sample.yaml file so that the value of 'c' is cat. will update the sample.yaml file so that the value of 'c' is cat.
@@ -96,7 +96,7 @@ b.e[0].name: Howdy Partner
then then
```bash ```bash
yaml w -s update_instructions.yaml sample.yaml yq w -s update_instructions.yaml sample.yaml
``` ```
will output: will output:
```yaml ```yaml
@@ -108,14 +108,14 @@ b:
And, of course, you can pipe the instructions in using '-': And, of course, you can pipe the instructions in using '-':
```bash ```bash
cat update_instructions.yaml | yaml w -s - sample.yaml cat update_instructions.yaml | yq w -s - sample.yaml
``` ```
### Values starting with a hyphen (or dash) ### Values starting with a hyphen (or dash)
The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags: The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:
``` ```
yaml w -- my.path -3 yq w -- my.path -3
``` ```
will output will output
@@ -123,3 +123,5 @@ will output
my: my:
path: -3 path: -3
``` ```
{!snippets/keys_with_dots.md!}

View File

@@ -3,9 +3,10 @@
set -e set -e
# acceptance test # acceptance test
X=$(./bin/yaml w ./examples/sample.yaml b.c 3 | ./bin/yaml r - b.c) X=$(./yq w ./examples/sample.yaml b.c 3 | ./yq r - b.c)
if [[ $X != 3 ]]; then if [[ $X != 3 ]]; then
echo "Failed acceptance test: expected 2 but was $X" echo "Failed acceptance test: expected 2 but was $X"
exit 1 exit 1
fi fi
echo "acceptance tests passed"

View File

@@ -1,9 +0,0 @@
#!/bin/bash
set -e
govendor fetch github.com/op/go-logging
govendor fetch github.com/spf13/cobra
govendor fetch gopkg.in/yaml.v2
govendor fetch github.com/imdario/mergo
govendor sync

2
vendor/vendor.json vendored
View File

@@ -39,5 +39,5 @@
"revisionTime": "2017-08-12T16:00:11Z" "revisionTime": "2017-08-12T16:00:11Z"
} }
], ],
"rootPath": "github.com/mikefarah/yaml" "rootPath": "github.com/mikefarah/yq"
} }

View File

@@ -11,12 +11,12 @@ 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.13.0" Version = "1.14.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
// such as "dev" (in development), "beta", "rc1", etc. // such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = "" VersionPrerelease = "dev"
) )
// ProductName is the name of the product // ProductName is the name of the product

View File

@@ -20,7 +20,7 @@ var outputToJSON = false
var overwriteFlag = false var overwriteFlag = false
var verbose = false var verbose = false
var version = false var version = false
var log = logging.MustGetLogger("yaml") var log = logging.MustGetLogger("yq")
func main() { func main() {
cmd := newCommandCLI() cmd := newCommandCLI()
@@ -32,7 +32,7 @@ func main() {
func newCommandCLI() *cobra.Command { func newCommandCLI() *cobra.Command {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "yaml", Use: "yq",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if version { if version {
cmd.Print(GetVersionDisplay()) cmd.Print(GetVersionDisplay())
@@ -74,13 +74,13 @@ func createReadCmd() *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "read [yaml_file] [path]", Use: "read [yaml_file] [path]",
Aliases: []string{"r"}, Aliases: []string{"r"},
Short: "yaml r sample.yaml a.b.c", Short: "yq r sample.yaml a.b.c",
Example: ` Example: `
yaml read things.yaml a.b.c yq read things.yaml a.b.c
yaml r - a.b.c (reads from stdin) yq r - a.b.c (reads from stdin)
yaml r things.yaml a.*.c yq r things.yaml a.*.c
yaml r things.yaml a.array[0].blah yq r things.yaml a.array[0].blah
yaml 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,
@@ -91,15 +91,15 @@ 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: "yaml w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC", Short: "yq w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC",
Example: ` Example: `
yaml write things.yaml a.b.c cat yq write things.yaml a.b.c cat
yaml write --inplace things.yaml a.b.c cat yq write --inplace things.yaml a.b.c cat
yaml w -i things.yaml a.b.c cat yq w -i things.yaml a.b.c cat
yaml w --script update_script.yaml things.yaml yq w --script update_script.yaml things.yaml
yaml w -i -s update_script.yaml things.yaml yq w -i -s update_script.yaml things.yaml
yaml w things.yaml a.b.d[+] foo yq w things.yaml a.b.d[+] foo
yaml w things.yaml a.b.d[+] foo yq w 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.
@@ -125,11 +125,11 @@ 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: "yaml n [--script/-s script_file] a.b.c newValueForC", Short: "yq n [--script/-s script_file] a.b.c newValueForC",
Example: ` Example: `
yaml new a.b.c cat yq new a.b.c cat
yaml n a.b.c cat yq n a.b.c cat
yaml n --script create_script.yaml yq n --script create_script.yaml
`, `,
Long: `Creates a new yaml w.r.t the given path and value. Long: `Creates a new yaml w.r.t the given path and value.
Outputs to STDOUT Outputs to STDOUT
@@ -147,13 +147,13 @@ 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: "yaml m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml", Short: "yq m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml",
Example: ` Example: `
yaml merge things.yaml other.yaml yq merge things.yaml other.yaml
yaml merge --inplace things.yaml other.yaml yq merge --inplace things.yaml other.yaml
yaml m -i things.yaml other.yaml yq m -i things.yaml other.yaml
yaml m --overwrite things.yaml other.yaml yq m --overwrite things.yaml other.yaml
yaml m -i -x things.yaml other.yaml yq m -i -x 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.