diff --git a/utils/update-paths b/utils/update-paths new file mode 100755 index 0000000..894af71 --- /dev/null +++ b/utils/update-paths @@ -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