Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for quoted values in infracost_breakdown --hook-config #269

Merged
merged 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
```
<!-- markdownlint-disable-next-line no-inline-html -->
<details><summary>Output</summary>
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 20 additions & 1 deletion infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,26 @@ function infracost_breakdown_ {
# Next line removes leading spaces, just for fancy output reason.
check=$(echo "$check" | sed 's/^[[:space:]]*//')

operation="$(echo "$check" | grep -oE '[!<>=]+')"
# 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

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)"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

IFS="$operation" read -r -a jq_check <<< "$check"
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
compare_value="${jq_check[1]}${jq_check[2]}"
Expand Down