mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
fix: make infracost_breakdown.sh compatible with bash 3.2 (macOS)
- Replace mapfile with while-read loop for bash 3.2 compatibility
- Fix substring expressions to avoid negative indices:
(${check: -1} => ${check:$((${#check}-1)):1})
- Replace negative array indexing with explicit length calculation
- Maintain identical functionality while supporting macOS default bash
version
Signed-off-by: Oliver Ladner <waste@lugh.ch>
This commit is contained in:
parent
44c7b5dec9
commit
df886fa772
1 changed files with 9 additions and 4 deletions
|
|
@ -70,19 +70,24 @@ function infracost_breakdown_ {
|
||||||
# -h .totalHourlyCost > 0.1
|
# -h .totalHourlyCost > 0.1
|
||||||
# --hook-config=.currency == "USD"
|
# --hook-config=.currency == "USD"
|
||||||
first_char=${check:0:1}
|
first_char=${check:0:1}
|
||||||
last_char=${check: -1}
|
last_char=${check:$((${#check} - 1)):1}
|
||||||
if [ "$first_char" == "$last_char" ] && {
|
if [ "$first_char" == "$last_char" ] && {
|
||||||
[ "$first_char" == '"' ] || [ "$first_char" == "'" ]
|
[ "$first_char" == '"' ] || [ "$first_char" == "'" ]
|
||||||
}; then
|
}; then
|
||||||
check="${check:1:-1}"
|
check="${check:1:$((${#check} - 2))}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mapfile -t operations < <(echo "$check" | grep -oE '[!<>=]{1,2}')
|
# Replace mapfile with while read loop for bash 3.2 compatibility
|
||||||
|
operations=()
|
||||||
|
while IFS= read -r line; do
|
||||||
|
operations+=("$line")
|
||||||
|
done < <(echo "$check" | grep -oE '[!<>=]{1,2}')
|
||||||
|
|
||||||
# Get the very last operator, that is used in comparison inside `jq` query.
|
# Get the very last operator, that is used in comparison inside `jq` query.
|
||||||
# From the example below we need to pick the `>` which is in between `add` and `1000`,
|
# From the example below we need to pick the `>` which is in between `add` and `1000`,
|
||||||
# but not the `!=`, which goes earlier in the `jq` expression
|
# but not the `!=`, which goes earlier in the `jq` expression
|
||||||
# [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
|
# [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
|
||||||
operation=${operations[-1]}
|
operation=${operations[$((${#operations[@]} - 1))]}
|
||||||
|
|
||||||
IFS="$operation" read -r -a jq_check <<< "$check"
|
IFS="$operation" read -r -a jq_check <<< "$check"
|
||||||
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
|
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue