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 21, 2023
1 parent 85ab98a commit 9369396
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions utils/update-paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/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 container fileUpdates paths
# The `paths` string should be quoted
#
# -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
\t\t\tThe PATHS string should be quoted\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": []}')

replacementsLenght=$(jq '.replacements | length' <<< "${path}")
for(( i=0; i<replacementsLenght; 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

echo "${newPaths[0]}"

0 comments on commit 9369396

Please sign in to comment.