Created
June 19, 2020 08:28
-
-
Save shiningjason/8ce2a9bba1bdd8eb4d939ff495a66bf6 to your computer and use it in GitHub Desktop.
Read YAML file from Bash script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| parse_yaml() { | |
| local yaml_file=$1 | |
| local prefix=$2 | |
| local indentUnit=${3:-2} | |
| local s="[[:space:]]" | |
| local w="[a-zA-Z0-9_]" | |
| awk ' | |
| BEGIN { | |
| FS = "'$s'" | |
| RS = "\n" | |
| indentCursor = -1 | |
| resultCursor = 0 | |
| } | |
| { | |
| if ($0 !~ /^'$s'*(-'$s')?'$w'+:('$s'.+)?$/) { | |
| print "Invalid YAML syntax in line " NR ": " $0 > "/dev/stderr" | |
| print "exit 1;" | |
| } | |
| isIndentSpace = 1 | |
| indentSpaces = 0 | |
| isArrayItem = 0 | |
| for (i = 1; i <= NF; i++) { | |
| if (isIndentSpace && $i ~ /^$/) { | |
| indentSpaces = indentSpaces + 1 | |
| } else { | |
| if (isIndentSpace) { | |
| if ($i == "-") { | |
| isArrayItem = 1 | |
| key = $(i + 1) | |
| value = $(i + 2) | |
| } else { | |
| key = $i | |
| value = $(i + 1) | |
| } | |
| sub(/:$/, "", key) | |
| } | |
| isIndentSpace = 0 | |
| } | |
| } | |
| indent = indentSpaces / '$indentUnit' | |
| if (indent % 1 != 0 || (indent > indentCursor && indent > indentCursor + 1)) { | |
| print "Incorrect indent in line " NR ": " $0 > "/dev/stderr" | |
| print "exit 1;" | |
| } | |
| prefix = breadcrumbs[indent - 1] != "" ? breadcrumbs[indent - 1] : "'$prefix'" | |
| prefix = prefix != "" ? prefix "_" : prefix | |
| if (isArrayItem) { | |
| arrayIndexes[indent - 1] += 1 | |
| breadcrumbs[indent] = prefix arrayIndexes[indent - 1] | |
| prefix = breadcrumbs[indent] "_" | |
| indent += 1 | |
| } | |
| if (indentCursor > indent) { | |
| arrayIndexes[indent] = 0 | |
| } | |
| breadcrumbs[indent] = prefix key | |
| indentCursor = indent | |
| if (isArrayItem) { | |
| resultIndex = resultIndexes[indent - 2] | |
| if (arrayIndexes[indent - 2] == 1) { | |
| result[resultIndex] = result[resultIndex] "$:1" | |
| } else if (arrayIndexes[indent - 2] > 1) { | |
| result[resultIndex] = result[resultIndex] "," arrayIndexes[indent - 2] | |
| } | |
| resultCursor++ | |
| result[resultCursor] = breadcrumbs[indent - 1] "=" | |
| resultIndexes[indent - 1] = resultCursor | |
| } | |
| prevResultIndex = resultIndexes[indent - 1] | |
| prevResult = result[prevResultIndex] | |
| if (prevResult ~ /^'$w'+=/) { | |
| delimeter = prevResult ~ /^'$w'+=$/ ? "$:" : "," | |
| result[prevResultIndex] = prevResult delimeter key | |
| } | |
| resultCursor++ | |
| result[resultCursor] = breadcrumbs[indent] "=" value | |
| resultIndexes[indent] = resultCursor | |
| } | |
| END { | |
| for (i = 1; i <= resultCursor; i++) print result[i] | |
| } | |
| ' $yaml_file | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # set -e | |
| . parse_yaml.sh | |
| eval $(parse_yaml test.yml "ci") | |
| echo "\$ci_merge=$ci_merge" | |
| echo "\$ci_merge_pre_run=$ci_merge_pre_run" | |
| echo "\$ci_merge_pre_run_1=$ci_merge_pre_run_1" | |
| echo "\$ci_merge_pre_run_1_script=$ci_merge_pre_run_1_script" | |
| echo "\$ci_merge_pre_run_1_arguments=$ci_merge_pre_run_1_arguments" | |
| echo "\$ci_merge_pre_run_1_arguments_report=$ci_merge_pre_run_1_arguments_report" | |
| echo "\$ci_merge_run=$ci_merge_run" | |
| echo "\$ci_merge_run_1=$ci_merge_run_1" | |
| echo "\$ci_merge_run_1_script=$ci_merge_run_1_script" | |
| echo "\$ci_merge_run_1_arguments=$ci_merge_run_1_arguments" | |
| echo "\$ci_merge_run_1_arguments_push=$ci_merge_run_1_arguments_push" | |
| echo "\$ci_merge_run_1_env=$ci_merge_run_1_env" | |
| echo "\$ci_merge_run_1_env_APP_NAME=$ci_merge_run_1_env_APP_NAME" | |
| echo "\$ci_merge_run_1_env_NODE_ENV=$ci_merge_run_1_env_NODE_ENV" | |
| echo "\$ci_merge_run_2=$ci_merge_run_2" | |
| echo "\$ci_merge_run_2_script=$ci_merge_run_2_script" | |
| echo "\$ci_merge_run_2_arguments=$ci_merge_run_2_arguments" | |
| echo "\$ci_merge_run_2_arguments_publish=$ci_merge_run_2_arguments_publish" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| merge: | |
| pre_run: | |
| - script: test | |
| arguments: | |
| report: true | |
| run: | |
| - script: build-node-image | |
| arguments: | |
| push: true | |
| env: | |
| APP_NAME: My App | |
| NODE_ENV: production | |
| - script: build-npm-package | |
| arguments: | |
| publish: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment