Created
May 18, 2022 15:57
-
-
Save wolviecb/fb409eb8975706b47c562cfd637c8c69 to your computer and use it in GitHub Desktop.
Flatten a yaml file or write a terraform helm_release 'set' block, using yq
This file contains 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
#!/usr/bin/env bash | |
_usage() { | |
echo " | |
${0} Parses a yaml file and returns a list of flattened key/values | |
${0} -f filename | |
${0} -tf filename #Return a terraform set with the key/value sets | |
" | |
exit 1 | |
} | |
if ! yq --version >/dev/null 2>&1 ; then | |
echo "YQ required" | |
exit 1 | |
fi | |
_flatten() { | |
yq eval '.. | select((tag == "!!map" or tag == "!!seq") | not) | (path | join(".")) + "=" + .' "${filename}" | |
} | |
_sets() { | |
while read -r sets; do | |
k="${sets%=*}" | |
v="${sets#*=}" | |
echo "set { | |
name = \"$k\" | |
value = \"$v\" | |
}" | |
done <<<"$(_flatten)" | |
} | |
while getopts f:t flag; do | |
case "${flag}" in | |
f) filename=${OPTARG} | |
if [[ -z "${filename}" ]]; then | |
_usage | |
fi | |
if [[ ! -f "${filename}" ]]; then | |
echo "${filename}: No such file" | |
_usage | |
fi | |
;; | |
t) tf=true | |
;; | |
*) _usage;; | |
esac | |
done | |
if [[ -n ${tf} ]]; then | |
_sets | |
else | |
_flatten | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment