jasperes / bash-yaml Goto Github PK
View Code? Open in Web Editor NEWRead a yaml file and create variables in bash
License: MIT License
Read a yaml file and create variables in bash
License: MIT License
After change travis to github actions, Shellcheck are warning some issues.
https://github.com/jasperes/bash-yaml/runs/1626353860?check_suite_focus=true
test_yamls:
-
key: app
file: test-app.yaml
description: Front end code
-
key: tasks
file: test-long-tasks.yaml
description: Backend end code
After being parsed
test_yamls__key[1]
holds the value task
instead of tasks
There should be two host in spec__host
but instead, there is 1, I'm puttng my bash file with the output which is wrong in the case of db
backupname: mybakcup
spec:
- host: localhost
port: 25060
awsid: AAA
awskey: BBB
dbs:
- dbname: testdb1
username: db1_username
passowrd: db1_password
- dbname: testdb2
username: db2_username
passowrd: db2_password
- host: localhost2
port: 2222
awsid: ZZZZ
awskey: XXXX
dbs:
- dbname: testdb1
username: db1_username
passowrd: db1_password
- dbname: testdb2
username: db2_username
passowrd: db2_password
echo "No of Hosts found ${#spec__host[@]}"
for i in ${!spec__host[@]}; do
echo ${spec__host[$i]} ${spec__port[$i]} ${spec__awsid[$i]}
for i in ${!spec___dbname[@]}; do
echo "No of DB found ${#spec___dbname[@]}"
echo ${spec___dbname[$i]} ${spec___username[$i]} ${spec___passowrd[$i]}
done
done
output
No of Hosts found 1
localhost2 2222 ZZZZ
No of DB found 4
testdb1 db1_username db1_password
testdb2 db2_username db2_password
testdb1 db1_username db1_password
testdb2 db2_username db2_password
[x] Linux
[] OSX
[] BSD
GNU bash, version 5.1.0(1)-release (x86_64-pc-linux-gnu)
Here are the --debug output..
bash test.sh --debug
person_name=("Jonathan")
person_age=("99")
person_email=("[email protected]")
persons__name+=("'Maria'")
persons__age+=("'88'")
persons__email+=("'[email protected]'")
persons__name+=(""João"")
persons__age+=(""77"")
persons__email+=(""[email protected]"")
complex_test_simple_obj_attr=(""value"")
complex_test_simple_obj_other_attr=("other "value"")
complex_test_simple_obj_chaos_list+=("n attr: value1")
complex_test_simple_obj_chaos_list__otherv+=("value1v")
complex_test_simple_obj_chaos_list__attr+=("value2")
complex_test_simple_obj_chaos_list__otherv+=(""value2v"")
complex_test_simple_obj_a_list+=("0")
complex_test_simple_obj_a_list+=("1")
complex_test_simple_obj_a_list+=("2")
complex_test_simple_obj_a_list+=("3")
complex_test_simple_obj_a_list+=("4")
complex_test_simple_obj_a_list+=("5")
complex_test_simple_obj_a_list+=("6")
complex_test_simple_obj_a_list+=("7")
complex_test_simple_obj_a_list+=("8")
complex_test_simple_obj_a_list+=("9")
more_tests_double_dashes=("--ok")
more_tests_dot_start=(".dot")
more_tests_some_propertie=("some-propertie ok!")
more_tests_domain_com=("domain.com ok!")
more_tests_inline_comment=("something")
more_tests_comment_with_hash=("an#hash")
more_tests_hash=("a#hash")
more_tests_single_quotes_hash1=("'a#hash'")
more_tests_single_quotes_hash2=("'a #hash'")
more_tests_single_quotes_hash3=("'#hi'")
more_tests_single_quotes_comment_in_string=("'a string...'")
more_tests_double_quotes_hash1=(""a#hash"")
more_tests_double_quotes_hash2=(""a #hash"")
more_tests_double_quotes_hash3=(""#hi"")
more_tests_double_quotes_comment_in_string=(""a string..."")
more_tests_a_multi_dash_property=("result-is=OK")
more_tests_a_multi_dot_property=("result.is=OK")
more_tests_a_property_that_has_quite_a_number_of_dashes=("result-is=OK")
more_tests_a_property_that_has_dashes_and_dots=("result-is.absolutely=fine.and-perfect")
Error on execute tests!
To avoid a line 9: $2: unbound variable
output when sourcing, change:
local prefix=$2
--> local prefix="${2:-""}"
If you have a ' in a comment it'll record the whole line in the variable
app_name: testcase #testing's fun
[X] Linux
[] OSX
[] BSD
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Hi .
If value ends with "s" character it been cut from the result.
for example : if person_name: Jonathans
the result will be Jonathan without 's' at the end.
--
person:
name: Jonathan
age: 99
email: [email protected]
Im use some array of packages in yml, and when im try to get in loop the sub vars of the array var im get only once the last one in first loop.
packages:
- name: name of package
package: type
ver: 1.0
zip: http://com.zip
- name: name of package2
package: type2
ver: 1.2
zip: http://com2.zip
4.4.20(1)-release
It cannot process https://github.com/roots/trellis/blob/master/group_vars/development/wordpress_sites.yml because the key example.com
cannot be recognized.
Could you please make it work and add a test case using domain name as key?
I have a use case where sometimes I do not want to overwrite already existing variables of the same name. So ideally, I want to have a separate namespace for all the yaml data.
Would it be possible to add an optional prefix to all variables/structs returned? So that in the lower example, the variable would be called e.g. configxy.myvar instead of myvar?
myvar: test
myarray:
[] Linux
4.4.12(1)-release
Thanks for this great function! I'm new to Bash so am not in a place to tweak the code. I was using this to import yaml text that unfortunately includes a $. This is clearly an issue for bash but I was hoping there was an adjustment to add $ or something to the regex to allow this text to come in as a variable. For example:
settings:
user: 'myname'
pwd: 'pa$$word$20' # not real password of course
If I run create_variables config.yml
then echo $settings_pwd
it results in 'pa22336word0'
.
default:
driver: 'driver_name'
server: 'R$word$10'
dsn: 'abcd'
uid: 'GRP19$name!01'
pwd: 'pa$$word$20'
port: 1234
database: 'db.name'
[] Linux
[] OSX
[] BSD
[X] Windows 10
# mac bug fix
AWK_COMMAND=`[ "$(uname)" == "Darwin" ] && echo gawk || echo awk`
program_exists() {
local ret='0'
command -v $1 >/dev/null 2>&1 || { local ret='1'; }
if [ "$ret" -ne 0 ]; then
echo 0
fi
echo 1
}
if [ "$(uname)" == "Darwin" ] && [ $(program_exists gawk) -eq 0 ]; then
echo "Command not found: gawk"
echo " Use Homebrew and then just run: brew install gawk"
exit 1;
fi
...
${AWK_COMMAND} -F"$fs" '{
...
env:
dev:
enable: true
host: 127.0.0.1
port: 7090
pro:
enable: false
host: 192.168.1.126
port: 1090
When parsing a sequence of mappings, the parser is unable to create variables from keys that are not indented at the same level as the first key.
sample:
- item-one:
name: item-one-name
- item-two:
name: item-one-name
While both item-one
and item-two
are considered to be valid yaml, this parser is only able to parse the first variation, and ${sample__name[1]}
is empty.
echo "Item 1: ${sample__name[0]}"
Item 1: item-one-name
echo "Item 2: ${sample__name[1]}"
Item 2:
GNU bash, version 3.2.57(1)-release
If there is a double quote in comment then whole string is included as the value
--
name: test
process:
name: 4K #this "comment" will be included in variable
bash 3.2
Hi,
From the following YAML file:
`servers:
I get the error //yaml.sh: eval: line 42: syntax error near unexpected token `"url: http://test.com"'
I would like to request a new feature or extension where a getValue function to get the value of a Property provided the Property name and YAML file.
Property can be something like Domains_Domain[1]_Name - 'underscore' separated fields' names.
Refer to the YAML file and example of what I am trying to achieve at https://stackoverflow.com/questions/70101607/shell-function-to-get-value-of-a-property-from-yaml-file. I mentioned the same below as well.
DomainName=getValue Domains_Domain[1]_Name /path/my_yaml.yml
AppSpaceName=getValue Domains_Domain[$DomainName]_AppSpaces_AppSpace[1]_Name /path/my_yaml.yml
AppNodeName=getValue Domains_Domain[$DomainName]_AppSpaces_AppSpace[$AppSpaceName]_AppNodes_AppNode[1]_Name /path/my_yaml.yml
AppName=getValue Domains_Domain[$DomainName]_AppSpaces_AppSpace[$AppSpaceName]_AppNodes_AppNode[$AppNodeName]_Applications_Application[1]_Name /path/my_yaml.yml
AppVersion=getValue Domains_Domain[$DomainName]_AppSpaces_AppSpace[$AppSpaceName]_AppNodes_AppNode[$AppNodeName]_Applications_Application[$AppName]_Version /path/my_yaml.yml
---
Action: start
Version: 642
Domains:
Domain:
Name: SanityTest
AppSpaces:
AppSpace:
Name: SanityAppSpace
AppNodes:
AppNode:
Name: SanityAppnode
Applications:
Application:
Name: InstagramTest.application
Version: "1.0"
AppNode:
Name: SanityAppnode_1
Applications:
Application:
Name: InstagramTest.application
Version: "1.0"
List didn't seems to works as expected.
I used test/tesh.sh and complex_test_simple_obj_a_list is empty (created by create_variables).
But parse_yaml report element members
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.