|
1 | 1 | #! /usr/bin/env bash |
2 | 2 | # |
3 | 3 | # installs and runs tflint with tflint-ruleset-azurerm plugin |
| 4 | +# rules: https://github.com/terraform-linters/tflint-ruleset-azurerm/blob/master/docs/rules/ |
4 | 5 |
|
5 | 6 | # ensure strict mode and predictable failure |
6 | 7 | set -euo pipefail |
7 | 8 | trap "echo 'error: Script failed: see failed command above'" ERR |
8 | 9 |
|
9 | 10 | # vars |
| 11 | +# Set local vars from env var, with default fallbacks |
| 12 | +TFLINT_VERSION="${TFLINT_VERSION:-v0.23.1}" |
| 13 | +TFLINT_RULESET_AZURERM_VERSION="${TFLINT_RULESET_AZURERM_VERSION:-v0.7.0}" |
| 14 | +TF_FLAGS=("$TF_WORKING_DIR") |
| 15 | +export TFLINT_LOG=debug |
| 16 | +# use empty array to skip adding disabled rules, eg: "DISABLED_RULES=()" |
10 | 17 | DISABLED_RULES=("azurerm_log_analytics_workspace_invalid_retention_in_days") |
11 | 18 |
|
12 | | -message="Downloading tflint and azurerm plugin" |
| 19 | +# use dynamic flags |
| 20 | +if [ ${#DISABLED_RULES[@]} -gt 0 ]; then |
| 21 | + echo "${#DISABLED_RULES[@]} DISABLED_RULES were defined: [${DISABLED_RULES[*]}]." |
| 22 | + |
| 23 | + # repeat flag multiple times |
| 24 | + for rule in "${DISABLED_RULES[@]}"; do |
| 25 | + echo "Adding [$rule] to flags" |
| 26 | + TF_FLAGS+=(--disable-rule="$rule") |
| 27 | + done |
| 28 | + |
| 29 | +else |
| 30 | + echo "DISABLED_RULES were not defined. Skipping." |
| 31 | +fi |
| 32 | + |
| 33 | +message="Downloading tflint ($TFLINT_VERSION) and azurerm plugin ($TFLINT_RULESET_AZURERM_VERSION)" |
13 | 34 | echo "STARTED: $message..." |
14 | 35 |
|
15 | 36 | # download tflint |
16 | | -curl -L "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" -o tflint.zip && unzip tflint.zip && rm tflint.zip |
| 37 | +curl -L "https://github.com/terraform-linters/tflint/releases/download/$TFLINT_VERSION/tflint_linux_amd64.zip" -o tflint.zip && unzip tflint.zip && rm tflint.zip |
17 | 38 |
|
18 | 39 | # download tflint-ruleset-azurerm plugin |
19 | | -curl -L "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint-ruleset-azurerm/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" -o tflint-ruleset-azurerm_linux_amd64.zip && unzip tflint-ruleset-azurerm_linux_amd64.zip && rm tflint-ruleset-azurerm_linux_amd64.zip |
| 40 | +curl -L "https://github.com/terraform-linters/tflint-ruleset-azurerm/releases/download/$TFLINT_RULESET_AZURERM_VERSION/tflint-ruleset-azurerm_linux_amd64.zip" -o tflint-ruleset-azurerm_linux_amd64.zip && unzip tflint-ruleset-azurerm_linux_amd64.zip && rm tflint-ruleset-azurerm_linux_amd64.zip |
20 | 41 |
|
21 | 42 | # move tflint-ruleset-azurerm plugin to correct path |
22 | 43 | install -D -m 777 tflint-ruleset-azurerm ./.tflint.d/plugins/tflint-ruleset-azurerm |
|
35 | 56 | cat .tflint.hcl |
36 | 57 |
|
37 | 58 | # run tflint |
38 | | -# expand array for disabled rules |
39 | | -TFLINT_LOG=debug ./tflint "$TF_WORKING_DIR" --disable-rule="${DISABLED_RULES[*]}" |
| 59 | +echo "Running tflint with the following flags: [${TF_FLAGS[*]}]" |
| 60 | +./tflint "${TF_FLAGS[@]}" |
0 commit comments