Skip to content

Commit dcb4c36

Browse files
fix: make infracost_breakdown.sh compatible with bash 3.2 (macOS) (#903)
Signed-off-by: Oliver Ladner <[email protected]> Co-authored-by: Oliver Ladner <[email protected]>
1 parent 9aa7be5 commit dcb4c36

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

hooks/infracost_breakdown.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,24 @@ function infracost_breakdown_ {
7070
# -h .totalHourlyCost > 0.1
7171
# --hook-config=.currency == "USD"
7272
first_char=${check:0:1}
73-
last_char=${check: -1}
73+
last_char=${check:$((${#check} - 1)):1}
7474
if [ "$first_char" == "$last_char" ] && {
7575
[ "$first_char" == '"' ] || [ "$first_char" == "'" ]
7676
}; then
77-
check="${check:1:-1}"
77+
check="${check:1:$((${#check} - 2))}"
7878
fi
7979

80-
mapfile -t operations < <(echo "$check" | grep -oE '[!<>=]{1,2}')
80+
# Replace mapfile with while read loop for bash 3.2 compatibility
81+
operations=()
82+
while IFS= read -r line; do
83+
operations+=("$line")
84+
done < <(echo "$check" | grep -oE '[!<>=]{1,2}')
85+
8186
# Get the very last operator, that is used in comparison inside `jq` query.
8287
# From the example below we need to pick the `>` which is in between `add` and `1000`,
8388
# but not the `!=`, which goes earlier in the `jq` expression
8489
# [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
85-
operation=${operations[-1]}
90+
operation=${operations[$((${#operations[@]} - 1))]}
8691

8792
IFS="$operation" read -r -a jq_check <<< "$check"
8893
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"

0 commit comments

Comments
 (0)