A small single-file, no-dependencies python command line script that consumes a YAML file and displays a single nested value named by a dotted.path
argument.
Initially built to supplement automated command line usage of an Ansible Vault.
- Python v2.7 or v3.0+
- Download
yaml-get.py
to somewhere in your path.$ wget https://raw.github.com/beporter/py-yaml-get/master/yaml-get.py
- Make it executable.
$ chmod a+x yaml-get.py
- The script expects the YAML document to be provided on STDIN.
- The script accepts a single command line argument representing the "dotted path" to the nested value you wish to extract.
Given the sample test.yaml
file (included in this repo):
$ cat test.yaml | ./yaml-get.py debug.enabled
True
$ cat test.yaml | ./yaml-get.py version
42
$ cat test.yaml | ./yaml-get.py directories.source
./src
$ cat test.yaml | ./yaml-get.py dependencies.0
stdlib
$ cat test.yaml | ./yaml-get.py description
This is a great app. It can do many things. You should try it.
In the event that the provided YAML document can not be parsed, or the provided dotted.path can not be located in the file, the script will print an error message to STDERR and exit with a non-zero code. There will be no STDOUT output.
For automated workflows, you may wish to suppress the STDERR output:
$ cat test.yaml | ./yaml-get.py bad.path.here
Error: 'str' object has no attribute 'get'
$ echo $?
2 # Also produces a non-zero exit code.
$ cat test.yaml | ./yaml-get.py bad.path.here 2>/dev/null # Error output suppressed.
$ echo $?
2 # Still produces a non-zero exit code.
Error handling is currently quite crude in the script, and there are many edge cases not specifically addressed.
For example, asking for a non-leaf node currently returns a json-ish string when it should probably error instead:
$ cat test.yaml | ./yaml-get.py debug
{'symbols': False, 'enabled': True, 'includes': ['./debug', './local-only']}
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. Translations are available.
Please use GitHub Issues for listing any known defects or issues.
Please fork this repository, create a new topic branch, and submit a pull request for your work.
Tests for the script are maintained in tests.sh
and make use of the assert.sh
package from @lehmannro, which itself includes a sample inline YAML document.
Tests take the form of:
assert "command to run" "expected output"
# or
assert_raises "command to run" N # Where `N` is the integer exit code expected.
Copyright © 2020 Brian Porter