|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# __ __ |
| 4 | +# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts |
| 5 | +# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com> |
| 6 | +# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Apache License 2.0 |
| 7 | +# /___/ Please report bugs and contribute back your improvements |
| 8 | +# |
| 9 | +# Version: v3.2.0-SNAPSHOT |
| 10 | +################################### |
| 11 | +set -euo pipefail |
| 12 | +shopt -s inherit_errexit |
| 13 | +unset CDPATH |
| 14 | + |
| 15 | +if ! [[ -v scriptsDir ]]; then |
| 16 | + scriptsDir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)" |
| 17 | + readonly scriptsDir |
| 18 | +fi |
| 19 | + |
| 20 | +if ! [[ -v dir_of_tegonal_scripts ]]; then |
| 21 | + dir_of_tegonal_scripts="$scriptsDir/../src" |
| 22 | + source "$dir_of_tegonal_scripts/setup_tegonal_scripts.sh" "$dir_of_tegonal_scripts" |
| 23 | +fi |
| 24 | + |
| 25 | +function checkParamsAndDefinitionInSync() { |
| 26 | + local foundErrors=0 |
| 27 | + local numOfFiles=0 |
| 28 | + |
| 29 | + while read -r -d $'\0' paramsFile; do |
| 30 | + numOfFiles=$((numOfFiles + 1)) |
| 31 | + local definitionFile="${paramsFile//.params.source/.params-definition.source}" |
| 32 | + if ! [[ -f "$definitionFile" ]]; then |
| 33 | + ((++foundErrors)) |
| 34 | + logError "definition file %s does not exist" "$definitionFile" |
| 35 | + continue |
| 36 | + fi |
| 37 | + local params paramDefinitions |
| 38 | + params=$(grep -E "^(# )?local" "$paramsFile") || die "could not extract params from params file %s" "$paramsFile" |
| 39 | + paramDefinitions=$( |
| 40 | + awk ' |
| 41 | + /^local -ra .*Params=\(/ { in_array=1; next } |
| 42 | + /^\)/ { in_array=0 } |
| 43 | + in_array && $1 !~ /^"/ && NF { print $1 } |
| 44 | + ' "$definitionFile" |
| 45 | + ) || die "could not extract parameter names from parameter definition file %s" "$definitionFile" |
| 46 | + |
| 47 | + for param in $params; do |
| 48 | + if ! [[ $param == "local" || $param == "#" ]]; then |
| 49 | + if ! grep "$param" <<<"$paramDefinitions" >/dev/null; then |
| 50 | + ((++foundErrors)) |
| 51 | + logError "could not find param \033[0;36m%s\033[0m in definition file %s" "$param" "$definitionFile" |
| 52 | + fi |
| 53 | + fi |
| 54 | + done |
| 55 | + for param in $paramDefinitions; do |
| 56 | + if ! grep "$param" <<<"$params" >/dev/null; then |
| 57 | + ((++foundErrors)) |
| 58 | + logError "could not find param \033[0;36m%s\033[0m in source file %s" "$param" "$definitionFile" |
| 59 | + fi |
| 60 | + done |
| 61 | + done \ |
| 62 | + < <( |
| 63 | + # using find here instead of find ... | while so that we can write foundErrors |
| 64 | + # we cannot do this when using the pipe approach as it will use a subshell for while |
| 65 | + find "$dir_of_tegonal_scripts" -name "*.params.source.sh" \ |
| 66 | + -print0 || |
| 67 | + # `while read` will fail because there is no \0 |
| 68 | + true |
| 69 | + ) |
| 70 | + |
| 71 | + if [[ $foundErrors -eq 0 ]]; then |
| 72 | + logSuccess "%s params.source.sh files in sync with their params-definition.source.sh" "$numOfFiles" |
| 73 | + else |
| 74 | + returnDying "%s params.source.sh files are not in sync with their definition, see errors above (%s are in sync)" "$foundErrors" "$((numOfFiles - foundErrors))" |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +${__SOURCED__:+return} |
| 79 | +checkParamsAndDefinitionInSync "$@" |
0 commit comments