From b7ab258fe0e35f99f67154cffaaca88fbd05e9f0 Mon Sep 17 00:00:00 2001 From: Brett Curtis Date: Fri, 28 Jun 2024 17:22:26 -0400 Subject: [PATCH] Refactor --- .pre-commit-hooks.yaml | 15 ++++++- cmd/terraform-fmt/main.go | 5 +-- cmd/terraform-validate/main.go | 39 +++++++++++++++++++ .../outputs.go => internal/outputs/main.go | 0 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 cmd/terraform-validate/main.go rename pkg/common/helpers/outputs.go => internal/outputs/main.go (100%) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2446551..2884068 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,5 +1,16 @@ - id: terraform-fmt - name: Terraform Format + description: The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. entry: terraform-fmt + exclude: \.terraform/.*$ + files: (\.tf|\.tfvars)$ + language: golang + name: Terraform Format + require_serial: true + +- id: terraform-validate + description: The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc. + entry: terraform-validate + exclude: \.terraform/.*$ + files: (\.tf|\.tfvars)$ language: golang - files: \.tf$ + name: Terraform Validate diff --git a/cmd/terraform-fmt/main.go b/cmd/terraform-fmt/main.go index edad207..7dd0b25 100644 --- a/cmd/terraform-fmt/main.go +++ b/cmd/terraform-fmt/main.go @@ -6,7 +6,7 @@ import ( "os/exec" "strings" - outputs "github.com/osinfra-io/pre-commit-hooks/pkg/common/helpers" + "github.com/osinfra-io/pre-commit-hooks/internal/outputs" ) func checkTerraformInstalled() bool { @@ -17,7 +17,6 @@ func checkTerraformInstalled() bool { func main() { if !checkTerraformInstalled() { fmt.Println("Terraform is not installed or not in PATH.") - // Handle the error, e.g., exit the program or inform the user. os.Exit(1) } @@ -41,7 +40,7 @@ func main() { // Split the unformattedFiles string into a slice of file names fileNames := strings.Split(unformattedFiles, "\n") for _, file := range fileNames { - fmt.Println(" " + outputs.EmojiColorText(outputs.Diamond, (file), outputs.Yellow)) + fmt.Println(outputs.Yellow, " - " + file) } fmt.Println(outputs.EmojiColorText(outputs.Working, "Formatting files with terraform fmt...", outputs.Purple)) diff --git a/cmd/terraform-validate/main.go b/cmd/terraform-validate/main.go new file mode 100644 index 0000000..91efa4d --- /dev/null +++ b/cmd/terraform-validate/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + + "github.com/osinfra-io/pre-commit-hooks/internal/outputs" +) + +func checkTerraformInstalled() bool { + _, err := exec.LookPath("terraform") + return err == nil +} + +func main() { + if !checkTerraformInstalled() { + fmt.Println("Terraform is not installed or not in PATH.") + os.Exit(1) + } + + fmt.Println(outputs.EmojiColorText(outputs.Running, "Running terraform validate...", outputs.Purple)) + + // Run terraform validate + cmd := exec.Command("terraform", "validate") + _, err := cmd.CombinedOutput() + if err != nil { + // Check if the error is an ExitError + if _, ok := err.(*exec.ExitError); ok { + // Handle specific exit codes if necessary + fmt.Printf(outputs.EmojiColorText(outputs.Error, "Terraform validate failed: %v\n", outputs.Red), err) + } else { + fmt.Printf(outputs.EmojiColorText(outputs.Error, "Error running terraform validate: %v\n", outputs.Red), err) + } + os.Exit(1) + } + + fmt.Println(outputs.EmojiColorText(outputs.ThumbsUp, "Terraform validate completed successfully.", outputs.Green)) +} diff --git a/pkg/common/helpers/outputs.go b/internal/outputs/main.go similarity index 100% rename from pkg/common/helpers/outputs.go rename to internal/outputs/main.go