Skip to content

Commit

Permalink
Add configuration of default terraform executable
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 committed Oct 5, 2023
1 parent e907490 commit f3158e0
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion modules/terraform/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package terraform

import (
"fmt"
"os/exec"

"github.com/gruntwork-io/terratest/modules/collections"
"github.com/gruntwork-io/terratest/modules/retry"
Expand Down Expand Up @@ -30,10 +31,20 @@ var commandsWithParallelism = []string{
"destroy-all",
}

const (
// TofuDefaultPath command to run tofu
TofuDefaultPath = "tofu"

// TerraformDefaultPath to run terraform
TerraformDefaultPath = "terraform"
)

var defaultExecutable = defaultTerraformExecutable()

// GetCommonOptions extracts commons terraform options
func GetCommonOptions(options *Options, args ...string) (*Options, []string) {
if options.TerraformBinary == "" {
options.TerraformBinary = "tofu"
options.TerraformBinary = defaultExecutable
}

if options.TerraformBinary == "terragrunt" {
Expand Down Expand Up @@ -112,3 +123,17 @@ func GetExitCodeForTerraformCommandE(t testing.TestingT, additionalOptions *Opti
}
return DefaultErrorExitCode, getExitCodeErr
}

func defaultTerraformExecutable() string {
cmd := exec.Command(TerraformDefaultPath, "-version")
cmd.Stdin = nil
cmd.Stdout = nil
cmd.Stderr = nil

if err := cmd.Run(); err == nil {
return TerraformDefaultPath
}

// fallback to Tofu if terraform is not available
return TofuDefaultPath
}

0 comments on commit f3158e0

Please sign in to comment.