Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcurtis committed Jun 28, 2024
1 parent 10f04a1 commit b7ab258
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
15 changes: 13 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions cmd/terraform-fmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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))
Expand Down
39 changes: 39 additions & 0 deletions cmd/terraform-validate/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
File renamed without changes.

0 comments on commit b7ab258

Please sign in to comment.