项目作者: azohra

项目描述 :
Read YAML files with only Bash
高级语言: Shell
项目地址: git://github.com/azohra/yaml.sh.git
创建时间: 2018-11-28T22:21:21Z
项目社区:https://github.com/azohra/yaml.sh

开源协议:MIT License

下载


Yaml.sh

Build Status


Yup. A YAML parser completely in bash. I can’t believe it either.

At the moment, we support a subset of the yaml spec.

Getting Started

Install it:

  1. $ curl -s https://get.yaml.sh | sh

Then query with it:

  1. $ ysh -f my.yml -q "path.to.awesomeness"

Library use

If installed:

  1. YSH_LIB=1;source /usr/local/bin/ysh

If you want the internet as your only dependency:

  1. $ YSH_LIB=1;source /dev/stdin <<< "$(curl -s https://raw.githubusercontent.com/azohra/yaml.sh/v0.2.0/ysh)"

Cook Book

  1. ---
  2. block_no: 0
  3. level_one:
  4. level_two:
  5. key: value
  6. quoted: "value"
  7. simple_lists:
  8. - first
  9. - second item
  10. - "third item"
  11. object_lists:
  12. - {name: one, value: 1}
  13. - {name: "second thing", value: 2}
  14. expanded_lists:
  15. - name: one
  16. value: 1
  17. - name: two
  18. value: 2
  19. - name: "quoted thing"
  20. value: 9000
  21. ---
  22. block_no: 1

Read from a file:

  1. ysh -f input.yaml -Q "block_no"

Re-use an already transpiled file

  1. file=$(ysh -f input.yaml)
  2. ysh -T $file -Q "block_no"

Query piped yaml

  1. cat input.yaml | ysh -Q "block_no"

Query for a value

  1. ysh -f file.yaml -Q "block_no"

Query from a stored sub structure

  1. sub=$(ysh -f file.yaml -s "level_one")
  2. ysh -T $sub -Q "level_two.key"

Query i’th item from a simple list

  1. ysh -f input.yaml -Q "level_one.level_two.simple_lists[1]"

Query i’th item from a complex list

  1. ysh -f input.yaml -Q "level_one.level_two.expanded_lists[1].name"

Print value from each block

  1. #!/bin/bash
  2. YSH_LIB=1; source /usr/local/bin/ysh
  3. file=$(ysh -f input.yaml)
  4. while [ -n "${file}" ]; do
  5. echo "Block Number:" $(ysh -T "${file}" -Q "block_no")
  6. file=$(ysh -T "${file}" -n)
  7. done

Flags

TIP:
Most flags have an upper-case and lower case usage. Upper case
flags denote the expectation of a value (empty otherwise),
where lowercase flags are chainable queries that can also
be passed back in with -T. In most cases queries will end
with an uppercase flag.

-f, --file <file_name>

Read from a file.

-T, --transpiled <file_name>

Read from a pre-transpiled string.

-q, --query <query>

Generic query string. DOES NOT SUPPORT [n] NOTATION

-Q, --query-val <query>

Safe query. Guarentees the return is a value. DOES NOT SUPPORT [n] NOTATION

-s, --sub <query>

Query for a subtree of yaml. Guarentees results are a subtree and no values are returned.

-l, --list <query>

Query for a list.

-L, --list <query>

Query for a list of values. Guarentees results are all values.

-c, --count <query>

Query for a list and count the elements.

-i, --index <i>

Access i’th element from chained list query.

-I, --index-val <i>

Access i’th element from chained list query. Garentees result is a value.

-t, --tops

Return top level keys of structure.

-n, --next

Moves to next block

-h, --help

Show this help dialog.

For more complete usage and examples look at the docs.


Made with ❤️ by the developers at azohra.com