From c29d8b19a6d15e4ed83d3084c5c6e54b0d6b6f6e Mon Sep 17 00:00:00 2001 From: Leandro Mendes Date: Mon, 21 Aug 2023 15:24:10 +0200 Subject: [PATCH] feat(HACBS-2227): snapshot data model can be used in update expressions 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 --- utils/update-paths | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 utils/update-paths diff --git a/utils/update-paths b/utils/update-paths new file mode 100755 index 00000000..96fcb205 --- /dev/null +++ b/utils/update-paths @@ -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