Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Leandro Mendes <[email protected]>
  • Loading branch information
theflockers committed Aug 24, 2023
1 parent c29d8b1 commit e50cc83
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions utils/update-paths
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
#
# script: update-paths
#
# description: This script searches for "${{ }}" enclosed yq expression in fileUpdates paths then
# 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 "${{ }}"
# -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'}}\"}]}]"
# 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"
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 -- "$@")
Expand Down Expand Up @@ -46,43 +46,34 @@ if [ -z "${PATHS}" ] || [ -z "${FILE}" ]; then
exit
fi

newPaths=('{"paths": []}')
newPaths='{"paths": []}'
pathsLength=$(jq -cr '. |length' <<< "${PATHS}")
for(( p=0; p<pathsLength; p++)); do

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

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}")
for(( r=0; r<replacementsLength; r++ )); do

EXP = substr($0, RSTART + PADDING, RLENGTH - (PADDING*2)+1)
} {
gsub(/\\/, "", EXP); print EXP
}' <<< "${replacement}")
replacement=$(jq -cr ".replacements[${r}]" <<< "${path}")
if grep -q '{{.*}}' <<< "${replacement}"; then
# get the yq expression and use xargs to remove the trailling whitespaces
yqExp=$(sed 's|.*{{\(.*\)}}.*|\1|' <<< "${replacement}"| xargs)

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}")
newReplacement=$(sed "s|{{.*}}|"${yqResult}"|g" <<< "${replacement}")

# appends to the newReplacements array
newReplacements[0]=$(jq -cr ".replacements += [${newReplacement}]" <<< "${newReplacements[0]}")
newReplacements=$(jq -cr ".replacements += [${newReplacement}]" <<< "${newReplacements}")
else
newReplacements[0]=$(jq -cr ".replacements += [${replacement}]" <<< "${newReplacements[0]}")
newReplacements=$(jq -cr ".replacements += [${replacement}]" <<< "${newReplacements}")
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]}")
merged=$(jq -cr -s add <<< "${newPath} ${newReplacements}")
newPaths=$(jq -cr ".paths += [ ${merged} ]" <<< "${newPaths}")
done

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

0 comments on commit e50cc83

Please sign in to comment.