-
-
Notifications
You must be signed in to change notification settings - Fork 545
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 terrafmt hook #313
Changes from all commits
90bd2a5
0b8c4a8
99ad370
7d55d35
baffffb
af553c0
d87a65e
1306222
4dac4e9
b4abf14
a6c73bd
5d75ffa
9ce5d44
458fcaf
8e009f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ If you are using `pre-commit-terraform` already or want to support its developme | |
* [`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook. | ||
* [`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook. | ||
* [`jq`](https://github.com/stedolan/jq) required for `infracost_breakdown` hook. | ||
* [`terrafmt`](https://github.com/katbyte/terrafmt) required for `terraform_fmt` hook. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add needed installation instructions both for macOS and Ubuntu in the sections below. |
||
|
||
<details><summary><b>Docker</b></summary><br> | ||
|
||
|
@@ -225,6 +226,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform | |
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` | | ||
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` | | ||
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` | | ||
| `terrafmt` | [terrafmt](https://github.com/katbyte/terrafmt) Format terraform blocks embedded in files. | `terrafmt` | | ||
<!-- markdownlint-enable no-inline-html --> | ||
|
||
Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. | ||
|
@@ -568,6 +570,19 @@ Example: | |
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`). | ||
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource. | ||
|
||
### terrafmt | ||
|
||
1. `terrafmt` supports custom arguments so you can pass [supported flags](https://github.com/katbyte/terrafmt#usage) like `diff` and `fmt` to see what would be formatted and to format the blocks respectively: | ||
|
||
```yaml | ||
- id: terrafmt | ||
args: | ||
- --args=diff | ||
- --args=fmt | ||
``` | ||
|
||
See the `terrafmt --help` command line help for available options | ||
|
||
## Authors | ||
|
||
This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from these awesome contributors: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might worth to pick up what @MaxymVlasov works on in #310 to modernize and standardize common shell coding conventions and approaches across hook scripts in this project. |
||
set -eo pipefail | ||
|
||
main() { | ||
initialize_ | ||
parse_cmdline_ "$@" | ||
terrafmt_ | ||
} | ||
|
||
initialize_() { | ||
# get directory containing this script | ||
local dir | ||
local source | ||
source="${BASH_SOURCE[0]}" | ||
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink | ||
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)" | ||
source="$(readlink "$source")" | ||
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located | ||
[[ $source != /* ]] && source="$dir/$source" | ||
done | ||
_SCRIPT_DIR="$(dirname "$source")" | ||
|
||
# source getopt function | ||
# shellcheck source=lib_getopt | ||
. "$_SCRIPT_DIR/lib_getopt" | ||
} | ||
|
||
parse_cmdline_() { | ||
declare argv | ||
argv=$(getopt -o a: --long args: -- "$@") || return | ||
eval "set -- $argv" | ||
|
||
for argv; do | ||
case $argv in | ||
-a | --args) | ||
shift | ||
ARGS+=("$1") | ||
shift | ||
;; | ||
--) | ||
shift | ||
break | ||
;; | ||
esac | ||
done | ||
} | ||
|
||
terrafmt_() { | ||
find . | grep -E "README.md" | sort | while read -r f; do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
echo "terrafmt: $f" | ||
terrafmt "${ARGS[@]}" "$f" | ||
done | ||
} | ||
|
||
# global arrays | ||
declare -a ARGS=() | ||
|
||
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block must follow common for this file convention/standard for this kind of blocks:
Moreover the
katbyte/terrafmt
repo does not distribute pre-compiled binaries, which means the {zip,tar}ball contains source code rather than the executable file. From what I see inkatbyte/terrafmt
README, the only available option to install the tool at the moment is to build it locally or use Golang builtin mechanism to build and install. Which doesn't seem to be an acceptable approach forpre-commit-terraform
at the moment (@antonbabenko @MaxymVlasov ?).