Skip to content

Commit d0bcdb3

Browse files
author
Bryce Lowe
authored
Feature/add git root keyword 102 (#103)
* 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
1 parent 0e35287 commit d0bcdb3

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,46 @@ repos:
145145
args: ["--enable require-variable-braces,deprecate-which"]
146146
```
147147

148+
## tflint Caveats
149+
150+
### Using the `--config` argument
151+
152+
With the introduction of `--chdir` into tflint, the `--config` argument is now bound to whatever subdirectory you are
153+
running the check against. For mono-repos this isn't ideal as you may have a central configuration file you'd like to
154+
use. If this matches your use-case, you can specify the placeholder `__GIT_DIR__` value in the `--config` argument
155+
that will evaluate to the root of the repository you are in.
156+
157+
```yaml
158+
repos:
159+
- repo: https://github.com/gruntwork-io/pre-commit
160+
rev: <VERSION>
161+
hooks:
162+
- id: tflint
163+
args:
164+
- "--config=__GIT_DIR__/.tflint.hcl"
165+
```
166+
167+
#### Changing the placeholder value
168+
169+
You can change the value of the placeholder by populating the `PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD` environment variable.
170+
171+
```bash
172+
export PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD=__foo__
173+
174+
cat <<EOF > .pre-commit-config.yaml
175+
---
176+
repos:
177+
- repo: https://github.com/gruntwork-io/pre-commit
178+
rev: v0.1.22
179+
hooks:
180+
- id: terragrunt-hclfmt
181+
- id: tflint
182+
args:
183+
- "--config=__foo__/.tflint.hcl"
184+
EOF
185+
186+
pre-commit run
187+
```
148188

149189
## License
150190

hooks/tflint.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,41 @@ set -e
77
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
88
export PATH=$PATH:/usr/local/bin
99

10-
# Install any plugins defined in .tflint.hcl
11-
tflint --init
10+
# allow customization of the repo root keyword
11+
PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD=${PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD:-__GIT_ROOT__}
12+
13+
process_arg() {
14+
local arg
15+
local repo_root
1216

17+
arg="${1}"
18+
repo_root="$(pwd)"
19+
20+
case "${arg}" in
21+
"--config"*)
22+
echo "${arg//$PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD/$repo_root}"
23+
;;
24+
*)
25+
echo "${arg}"
26+
esac
27+
}
1328

1429
declare -a FILES
1530
declare -a ARGS
1631
while [[ $# -gt 0 ]]
1732
do
1833
case "$1" in
19-
-*) ARGS+=("$1")
34+
-*) ARGS+=("$(process_arg "$1")")
2035
;;
2136
*) FILES+=("$1")
2237
;;
2338
esac
2439
shift
2540
done
2641

42+
# Install any plugins defined in .tflint.hcl
43+
tflint "${ARGS[@]}" --init
44+
2745
for file in "${FILES[@]}"
2846
do
2947
tflint "${ARGS[@]}" --chdir "$(dirname "$file")" --filter "$(basename "$file")"

0 commit comments

Comments
 (0)