From 3922c0a8fb115dc5f45eebf13f28171076c3ad3b Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 23 Feb 2024 17:01:24 +0500 Subject: [PATCH] add terraform validation to linting --- package.json | 2 +- terraform_validate.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100755 terraform_validate.sh diff --git a/package.json b/package.json index bab18d3..2570de4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "test": "bun test", "fmt": "bun x prettier -w **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt **/*.tf .sample/main.tf", "fmt:ci": "bun x prettier --check **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt -check **/*.tf .sample/main.tf", - "lint": "bun run lint.ts", + "lint": "bun run lint.ts && ./terraform_validate.sh", "update-version": "./update-version.sh" }, "devDependencies": { diff --git a/terraform_validate.sh b/terraform_validate.sh new file mode 100755 index 0000000..5776ebb --- /dev/null +++ b/terraform_validate.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Function to run terraform init and validate in a directory +run_terraform() { + local dir="$1" + echo "Running terraform init and validate in $dir" + cd "$dir" || exit + terraform init + terraform validatecd + cd - || exit +} + +# Main script +main() { + # Get the current directory + current_dir=$(pwd) + + # Find all subdirectories containing a main.tf file + subdirs=$(find "$current_dir" -type f -name "main.tf" -exec dirname {} \;) + + # Run terraform init and validate in each subdirectory + for dir in $subdirs; do + run_terraform "$dir" + done +} + +# Run the main script +main