Shell config file parser (json, yaml)
Project description
niet
Get data from yaml file directly in your shell
Install or Update niet
$ pip install -U niet
Requirements
- Python 2.7+
Usage
With YAML file
Consider the yaml file with the following content:
# /path/to/your/file.yaml
project:
meta:
name: my-project
foo: bar
list-items:
- item1
- item2
- item3
You can retrieve data from this file by using niet like this:
$ niet /path/to/your/file.yaml ".project.meta.name"
my-project
$ niet /path/to/your/file.yaml ".project.foo"
bar
$ niet /path/to/your/file.yaml ".project.list-items"
item1 item2 item3
$ # assign return value to shell variable
$ NAME=$(niet /path/to/your/file.yaml ".project.meta.name")
$ echo $NAME
my-project
With JSON file
Consider the json file with the following content:
{
"project": {
"meta": {
"name": "my-project"
}
},
"foo": "bar",
"list-items": [
"item1",
"item2",
"item3"
]
}
You can retrieve data from this file by using niet like this:
$ niet /path/to/your/file.json "project.meta.name"
my-project
$ niet /path/to/your/file.json "project.foo"
bar
$ niet /path/to/your/file.json "project.list-items"
item1 item2 item3
$ # assign return value to shell variable
$ NAME=$(niet /path/to/your/file.json "project.meta.name")
$ echo $NAME
my-project
Output formats
You can change the output format using the -f or --format optional argument.
By default, niet detect the input format and display complex objects in the same format. If the object is a list or a value, newline output format will be used.
Output formats are:
- ifs
- squote
- dquote
- newline
- yaml
- json
ifs
Ifs output format print all values of a list or a single value in one line. All values are separated by the content of IFS environment variable if defined, space otherwise. This is usefull in a shell for loop, but your content must, of course, don't contain IFS value.
OIFS="$IFS"
IFS="|"
for i in $(niet tests/samples/sample.yaml .project.list-items -f ifs); do
echo $i
done
IFS="$OIFS"
squote
Squotes output format print all values of a list or a single value in one line. All values are quoted with single quotes and are separated by IFS value.
dquote
Dquotes output format print all values of a list or a single value in one line. All values are quoted with a double quotes and are separated by IFS value.
newline
Newline output format print one value of a list or a single value per line. This format is usefull using shell while read loop. eg:
while read value: do
echo $value
done < $(niet --format newline your-file.json project.list-items)
yaml
Yaml output format force output to be in YAML regardless the input file format.
json
Json output format force output to be in JSON regardless the input file format.
Deal with errors
When your JSON file content are not valid niet display an error and exit
with return code 1
You can easily protect your script like this:
PROJECT_NAME=$(niet your-file.yaml project.meta.name)
if [ "$?" = "1" ]; then
echo "Error occur ${PROJECT_NAME}"
else
echo "Project name: ${PROJECT_NAME}"
fi
Examples
You can try niet by using the samples provided with the project sources code.
Sample example:
# tests/samples/sample.yaml
project:
meta:
name: project-sample
tags:
- example
- sample
- for
- testing
- purpose
Retrieve the project name:
$ niet tests/samples/sample.yaml project.meta.name
project-sample
Deal with list of items
$ for el in $(niet tests/samples/sample.yaml project.meta.tags); do echo ${el}; done
example
sample
for
testing
purpose
Tips
You can pass your search with or without quotes like this:
$ niet your-file.yaml project.meta.name
$ niet your-file.yaml "project.meta.name"
Contribute
If you want to contribute to niet please first read the contribution guidelines
Licence
This project is under the MIT License.
See the license file for more details
Hervé Beraud herveberaud.pro@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file niet-1.2.0.tar.gz
.
File metadata
- Download URL: niet-1.2.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8405ec7aa8c55d4f12032aa4113e68954fbd898bb583e7ecca59feb4de87c252 |
|
MD5 | c9bbcbc2c551cca80c4f320577ca2ca3 |
|
BLAKE2b-256 | 06a055cac0e9adfaa99478a113cb71ea8b918344eb6fe4eb9caa97eb68342498 |
File details
Details for the file niet-1.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: niet-1.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01601b7b89a4b9d90fcdad57ca0974d66dd1944ecda33eb0c6be7d16a30869e2 |
|
MD5 | bf07b4b2ef9f93e7b806c8e34f9ebdbb |
|
BLAKE2b-256 | 4b6baaeead082ae82748f647b9b91ebf3330c6a312a0be1fd6766c471ef521b1 |