diff --git a/README.md b/README.md index 7f4a1f2..9c5ae8f 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,7 @@ This repository contains a collection of Git hooks that can be used to enforce coding standards, run tests, and perform other tasks. This is used across repositories in our organization to ensure consistency and quality in our codebase. Please refer to the README of each repository for specific instructions on how to use these hooks. + +# Conditions +- We expect the code to be formatted before commit. Having an auto format introduces unintenional file changes +- Preserve developer intent as much as we can. diff --git a/terraform/pre-commit.sh b/terraform/pre-commit.sh new file mode 100644 index 0000000..fe82447 --- /dev/null +++ b/terraform/pre-commit.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# Authors: aslamcodes +# Created on: 08/12/2025 +# Last updated on: 08/12/2025 + +set -euo pipefail + +echo "Running Terraform pre-commit checks..." + +# Check if terraform is installed +if ! command -v terraform &> /dev/null; then + echo "Error: terraform command not found. Please install Terraform." + exit 1 +fi + +# Get list of staged .tf files +STAGED_TF_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.tf$' || true) + +if [ -z "$STAGED_TF_FILES" ]; then + echo "No Terraform files staged for commit." + exit 0 +fi + +echo "Checking Terraform files: $STAGED_TF_FILES" + +# Format check +echo "Checking Terraform formatting..." +if ! terraform fmt -check -diff -recursive .; then + echo "Terraform files are not properly formatted." + echo "Run 'terraform fmt -recursive .' to fix formatting issues." + exit 1 +fi + +# Validate syntax +echo "Validating Terraform syntax..." +if ! terraform validate; then + echo "Terraform validation failed." + exit 1 +fi + +echo "All Terraform checks passed!" +exit 0