Skip to content

Commit

Permalink
Feature/add git root keyword 102 (#103)
Browse files Browse the repository at this point in the history
* feature: add a git root keyword for `--config` argument

When operating in a monorepo the new functionality of tflint (`--chdir`) breaks config file path by changing the current working directory when tflint runs.  This commit patches this gap by allowing a user to provide __GIT_ROOT__ or populate a environment variable with a custom string and when the `--config` argument is is found it replaces that keyword with the repository root (pwd in the case of pre-commit).

* feature: add documentation
  • Loading branch information
brycelowe authored Nov 7, 2023
1 parent 0e35287 commit d0bcdb3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,46 @@ repos:
args: ["--enable require-variable-braces,deprecate-which"]
```

## tflint Caveats

### Using the `--config` argument

With the introduction of `--chdir` into tflint, the `--config` argument is now bound to whatever subdirectory you are
running the check against. For mono-repos this isn't ideal as you may have a central configuration file you'd like to
use. If this matches your use-case, you can specify the placeholder `__GIT_DIR__` value in the `--config` argument
that will evaluate to the root of the repository you are in.

```yaml
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: <VERSION>
hooks:
- id: tflint
args:
- "--config=__GIT_DIR__/.tflint.hcl"
```

#### Changing the placeholder value

You can change the value of the placeholder by populating the `PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD` environment variable.

```bash
export PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD=__foo__
cat <<EOF > .pre-commit-config.yaml
---
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.22
hooks:
- id: terragrunt-hclfmt
- id: tflint
args:
- "--config=__foo__/.tflint.hcl"
EOF
pre-commit run
```

## License

Expand Down
24 changes: 21 additions & 3 deletions hooks/tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,41 @@ set -e
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
export PATH=$PATH:/usr/local/bin

# Install any plugins defined in .tflint.hcl
tflint --init
# allow customization of the repo root keyword
PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD=${PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD:-__GIT_ROOT__}

process_arg() {
local arg
local repo_root

arg="${1}"
repo_root="$(pwd)"

case "${arg}" in
"--config"*)
echo "${arg//$PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD/$repo_root}"
;;
*)
echo "${arg}"
esac
}

declare -a FILES
declare -a ARGS
while [[ $# -gt 0 ]]
do
case "$1" in
-*) ARGS+=("$1")
-*) ARGS+=("$(process_arg "$1")")
;;
*) FILES+=("$1")
;;
esac
shift
done

# Install any plugins defined in .tflint.hcl
tflint "${ARGS[@]}" --init

for file in "${FILES[@]}"
do
tflint "${ARGS[@]}" --chdir "$(dirname "$file")" --filter "$(basename "$file")"
Expand Down

0 comments on commit d0bcdb3

Please sign in to comment.