From dd2d6b7095a4150f72dc92d2015da099f72e93d0 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 26 Oct 2021 18:43:55 +0300 Subject: [PATCH 1/4] feat: Add support for quoted values in `infracost_breakdown` `--hook-config` --- infracost_breakdown.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/infracost_breakdown.sh b/infracost_breakdown.sh index c088b7733..5dac1d30a 100755 --- a/infracost_breakdown.sh +++ b/infracost_breakdown.sh @@ -105,6 +105,20 @@ function infracost_breakdown_ { # Next line removes leading spaces, just for fancy output reason. check=$(echo "$check" | sed 's/^[[:space:]]*//') + # Drop quotes in hook args section. From: + # -h ".totalHourlyCost > 0.1" + # --hook-config='.currency == "USD"' + # To: + # -h .totalHourlyCost > 0.1 + # --hook-config=.currency == "USD" + first_char=${check:0:1} + last_char=${check: -1} + if [ "$first_char" == "$last_char" ] && { + [ "$first_char" == '"' ] || [ "$first_char" == "'" ] + }; then + check="${check:1:-1}" + fi + operation="$(echo "$check" | grep -oE '[!<>=]+')" IFS="$operation" read -r -a jq_check <<< "$check" real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")" From caea8de3a38fc915fd170fd190e0db53b1726771 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 26 Oct 2021 19:37:14 +0300 Subject: [PATCH 2/4] Implement review suggestions --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4ff527ad1..17bb66de0 100644 --- a/README.md +++ b/README.md @@ -243,10 +243,10 @@ Unlike most other hooks, this hook triggers once if there are any changed files - id: infracost_breakdown args: - --args=--path=./env/dev - - --hook-config=.totalHourlyCost|tonumber > 0.1 - - --hook-config=.totalHourlyCost|tonumber > 1 - - --hook-config=.projects[].diff.totalMonthlyCost|tonumber != 10000 - - --hook-config=.currency == "USD" + - --hook-config='.totalHourlyCost|tonumber > 0.1' + - --hook-config='.totalHourlyCost|tonumber > 1' + - --hook-config='.projects[].diff.totalMonthlyCost|tonumber != 10000' + - --hook-config='.currency == "USD"' ```
Output @@ -369,8 +369,8 @@ Example: ```yaml - id: terraform_providers_lock args: - - '--args=-platform=windows_amd64' - - '--args=-platform=darwin_amd64' + - --args=-platform=windows_amd64 + - --args=-platform=darwin_amd64 ``` 4. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository: From d649f0fd0c18dd849afba38bbc6bc6235dacc747 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 26 Oct 2021 19:37:37 +0300 Subject: [PATCH 3/4] feat: Support jq complex queries that include comparison --- infracost_breakdown.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/infracost_breakdown.sh b/infracost_breakdown.sh index 5dac1d30a..a98a8a6cf 100755 --- a/infracost_breakdown.sh +++ b/infracost_breakdown.sh @@ -119,7 +119,12 @@ function infracost_breakdown_ { check="${check:1:-1}" fi - operation="$(echo "$check" | grep -oE '[!<>=]+')" + operations="$(echo "$check" | grep -oE '[!<>=]+')" + # Get the last operation, that is user comparation, not inside jq query: + # [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000 + # shellcheck disable=SC2086 # Enable word splitting. + operation="$(echo $operations | rev | cut -d' ' -f1 | rev)" + IFS="$operation" read -r -a jq_check <<< "$check" real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")" compare_value="${jq_check[1]}${jq_check[2]}" From a85bc763d9a1757c451371da2653c850258ca834 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Wed, 27 Oct 2021 14:16:57 +0300 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: George L. Yermulnik --- infracost_breakdown.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/infracost_breakdown.sh b/infracost_breakdown.sh index a98a8a6cf..ac0fdf911 100755 --- a/infracost_breakdown.sh +++ b/infracost_breakdown.sh @@ -119,11 +119,12 @@ function infracost_breakdown_ { check="${check:1:-1}" fi - operations="$(echo "$check" | grep -oE '[!<>=]+')" - # Get the last operation, that is user comparation, not inside jq query: + operations=($(echo "$check" | grep -oE '[!<>=]{1,2}')) + # 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`, + # but not the `!=`, which goes earlier in the `jq` expression # [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000 - # shellcheck disable=SC2086 # Enable word splitting. - operation="$(echo $operations | rev | cut -d' ' -f1 | rev)" + operation=${operations[-1]} IFS="$operation" read -r -a jq_check <<< "$check" real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"