Yaml query executer is written in ruby.
Add this line to your application's Gemfile:
gem 'yqr'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yqr
By using Hashie gem together, even simpler access becomes possible.
$ cat example1.yaml
---
cat:
- mike
- buchi
dog:
- pochi
- koro
$ yqr --file example1.yaml [dog][0]
pochi
$ cat example2.yaml
---
- name: mike
kind: cat
- name: pochi
kind: dog
- name: buchi
kind: cat
- name: koro
kind: dog
$ yqr ".find{|a| a[kind] == 'dog'}[name]" < example2.yaml
pochi
$ cat example2.yaml | bundle exec yqr ".select{|a| a[kind] == 'cat'}.last[name]"
buchi
$ cat example4.yaml
---
cat:
- name: mike
sex: male
- name: tama
sex: female
# Default output type is yaml
$ yqr --file example4.yaml "[cat].first"
---
:name: mike
:sex: male
# Raw output type (Object.to_s)
$ yqr --file example4.yaml --raw "[cat].first"
{:name=>"mike", :sex=>"male"}
# Json output type
$ yqr --file example4.yaml --json "[cat].first"
{"name":"mike","sex":"male"}
if you install Hashie gem, you can also use query as:
$ yqr --file example4.yaml ".cat.first"
---
:name: mike
:sex: male
$ yqr --file example4.yaml --raw ".cat.first"
#<Hashie::Mash name="mike" sex="male">
git-flow.