-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HACBS-2227): snapshot data model can be used in update expressio…
…ns (#54) This PR adds the update-paths script to enable updating yq expression in fileUpdates paths strings with the result of these expressions Signed-off-by: Leandro Mendes <[email protected]>
- Loading branch information
1 parent
b846181
commit a918b3a
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/bin/bash | ||
# | ||
# script: update-paths | ||
# | ||
# description: This script searches for "{{ }}" enclosed yq expression in fileUpdates paths then | ||
# executes and replaces it with its result | ||
# | ||
# parameters: | ||
# | ||
# -p, --paths Json String containing fileUpdates paths with optional yq expression(s) enclosed by "{{ }}" | ||
# The `paths` json string should be properly quoted | ||
# Example: --paths='[{"path":"foo.yaml","replacements":[{"key":"bar","replacement":"{{ .components[0]| .containerImage }}"}]}]' | ||
# | ||
# -f, --file: Path to the mapping_snapshots.json file | ||
# | ||
print_help(){ | ||
echo -e "$0 [ -p, --paths ] PATHS [ -f, --file ] FILE\n" | ||
echo -e "\t-p, --paths\tJson String containing fileUpdates paths with optional yq expression(s) enclosed by \"\${{ }}\"" | ||
echo -e "\t\t\tThe \`paths\` json string should be properly escaped/quoted" | ||
echo -e "\t\t\tExample: --paths='[{\"path\":\"foo.yaml\",\"replacements\":[{\"key\":\"bar\",\"replacement\":\"{{ .components[0]| .containerImage }}\"}]}]'" | ||
echo -e "\t-f, --file\tPath to mapping_snapshots.json file\n" | ||
} | ||
|
||
OPTIONS=$(getopt -l "paths:,file:" -o "p:f:" -a -- "$@") | ||
eval set -- "$OPTIONS" | ||
while true; do | ||
case "$1" in | ||
-p|--paths) | ||
shift | ||
PATHS=$1 | ||
;; | ||
-f|--file) | ||
shift | ||
FILE="$1" | ||
;; | ||
--) | ||
shift | ||
break | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
if [ -z "${PATHS}" ] || [ -z "${FILE}" ]; then | ||
print_help | ||
exit | ||
fi | ||
|
||
pathsLength=$(jq -cr '. |length' <<< "${PATHS}") | ||
for(( p=0; p<pathsLength; p++)); do | ||
|
||
path=$(jq ".[${p}]" <<< "${PATHS}") | ||
replacementsLength=$(jq '.replacements | length' <<< "${path}") | ||
for(( r=0; r<replacementsLength; r++ )); do | ||
replacement=$(jq -cr ".replacements[${r}].replacement" <<< "${path}") | ||
if grep -q '{{.*}}' <<< "${replacement}"; then | ||
yqExp=$(sed 's|.*{{\(.*\)}}.*|\1|' <<< "${replacement}") | ||
|
||
# execute the yq expression and replace the "replacement string" with the result | ||
yqResult=$(yq -oy -r "${yqExp}" "${FILE}") | ||
newReplacement=$(sed "s|{{.*}}|"${yqResult}"|g" <<< "${replacement}") | ||
|
||
newReplacementJson=$(jq -R <<< ${newReplacement}) | ||
PATHS=$(jq -cr ".[${p}].replacements[$r].replacement = ${newReplacementJson}" <<< "${PATHS}") | ||
fi | ||
done | ||
done | ||
echo "${PATHS}" |