From 813050f0ce6ef3439f03427c4a211cdd6d92e820 Mon Sep 17 00:00:00 2001 From: Matt Seymour Date: Thu, 5 Nov 2020 10:26:52 +0000 Subject: [PATCH] Initial project source * Adds readme * Precommit hooks for the project * Add terraform-fmt hook --- .pre-commit-config.yaml | 8 ++++++++ .pre-commit-hooks.yaml | 8 ++++++++ README.md | 39 ++++++++++++++++++++++++++++++++++++++- terraform-fmt.sh | 5 +++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 .pre-commit-config.yaml create mode 100644 .pre-commit-hooks.yaml create mode 100755 terraform-fmt.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a5b4101 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.3.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..75c6f72 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,8 @@ +- id: terraform-fmt + name: Terraform fmt (Format) + description: Rewrite all Terraform files (.tf) to canonical format. + entry: ./terraform-fmt.sh + files: (\.tf|\.tfvars)$ + # ignore the .terraform directory + exclude: \.terraform\/.*$ + language: script diff --git a/README.md b/README.md index 3501ad1..ae99980 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# pre-commit-terraform-fmt \ No newline at end of file +# pre-commit terraform fmt + +A pre-commit hook to check the formatting of terraform files pre commit. This hook +requires terraform is present on the system path to work. + +It currently does a hard format (changes the state of the files on disk). A future function should +be allowing a `-check` option. + +## Installation + +Add the following to the `.pre-commit-config.yaml` file. + +``` +- repo: https://github.com/envelop-risk/pre-commit-terraform-fmt + rev: v1.0.0 + hooks: + - id: terraform-fmt +``` + +The following hooks, while not required are highly recommended. +``` +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.3.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +``` + +## Running the hooks + +When first installing the hook it is recommended to run the hook on all files. This can be done +with the command. + +``` +pre-commit run -a terraform-fmt +``` diff --git a/terraform-fmt.sh b/terraform-fmt.sh new file mode 100755 index 0000000..21bee62 --- /dev/null +++ b/terraform-fmt.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +for file in "$@"; do + terraform fmt `dirname $file` +done