Skip to content

Commit

Permalink
feat(HACBS-2227): snapshot data model can be used in update expressions
Browse files Browse the repository at this point in the history
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
theflockers committed Aug 23, 2023
1 parent b846181 commit c29d8b1
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions utils/update-paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/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
\t-p, --paths\tJson String containing fileUpdates paths with optional yq expression(s) enclosed by \"\${{ }}\"
\t\t\tThe \`paths\` json string should be properly escaped/quoted
\t\t\tExample: --paths=\"[{\\\"path\\\":\\\"foo.yaml\\\",\\\"replacements\\\":[{\\\"key\\\":\\\"bar\\\",\\\"replacement\\\":\\\"\\\${{'.components[0].containerImage'}}\\\"}]}]\"\n
\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

newPaths=('{"paths": []}')

IFS=$'\n' # This is required because the JSON string might contain spaces
for path in $(jq -cr '.[]' <<< "${PATHS}"); do
newPath=$(jq '{"path": .path}' <<< "${path}")
newReplacements=('{"replacements": []}')

replacementsLength=$(jq '.replacements | length' <<< "${path}")
for(( i=0; i<replacementsLength; i++ )); do
replacement=$(jq -cr ".replacements[${i}]" <<< "${path}")
# Here we are searching for the enclosed by "${{ }}" yq exp to later execute it.
# The PADDING = 4 means the begining of the expression "${{" +1 (the "'" char)
# to positioning it at the start of the yq expression.
yqExp=$(awk '{
PADDING = 4
match($0, "\\$\\{{2}.*\\}{2}")
EXP = substr($0, RSTART + PADDING, RLENGTH - (PADDING*2)+1)
} {
gsub(/\\/, "", EXP); print EXP
}' <<< "${replacement}")

if [ -n "${yqExp}" ]; then
# execute the yq expression and replace the "replacement string" with the result
yqResult=$(yq "${yqExp}" "${FILE}")
newReplacement=$(sed "s|\${{.*}}|"${yqResult}"|g" <<< "${replacement}")
# appends to the newReplacements array
newReplacements[0]=$(jq -cr ".replacements += [${newReplacement}]" <<< "${newReplacements[0]}")
else
newReplacements[0]=$(jq -cr ".replacements += [${replacement}]" <<< "${newReplacements[0]}")
fi

done

# merge the two hashes and append to the newPaths array
merged=$(jq -cr -s add <<< "${newPath[0]} ${newReplacements[0]}")
newPaths[0]=$(jq -cr ".paths += [ ${merged} ]" <<< "${newPaths[0]}")
done

jq -cr '.paths' <<< "${newPaths[0]}"

0 comments on commit c29d8b1

Please sign in to comment.