Skip to content

Commit

Permalink
feat(controller): better pattern for terragrunt executables
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMrqes committed Oct 18, 2024
1 parent 24beeb6 commit b364f56
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
1 change: 1 addition & 0 deletions internal/runner/tools/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type BaseExec interface {
Apply(string) error
Show(string, string) ([]byte, error)
TenvName() string
GetExecPath() string
}
18 changes: 4 additions & 14 deletions internal/runner/tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,10 @@ func InstallBinaries(layer *configv1alpha1.TerraformLayer, repo *configv1alpha1.
return nil, err
}
log.Infof("using Terragrunt version %s as wrapper for %s", terragruntVersion, baseExec.TenvName())
if baseExec.TenvName() == "terraform" {
return &tg.Terragrunt{
ExecPath: filepath.Join(binaryPath, "Terragrunt", terragruntVersion, "terragrunt"),
Terraform: baseExec.(*tf.Terraform),
OpenTofu: nil,
}, nil
} else if baseExec.TenvName() == "tofu" {
return &tg.Terragrunt{
ExecPath: filepath.Join(binaryPath, "Terragrunt", terragruntVersion, "terragrunt"),
Terraform: nil,
OpenTofu: baseExec.(*ot.OpenTofu),
}, nil
}

return &tg.Terragrunt{
ExecPath: filepath.Join(binaryPath, "Terragrunt", terragruntVersion, "terragrunt"),
ChildExecPath: baseExec.GetExecPath(),
}, nil
}
return baseExec, nil
}
4 changes: 4 additions & 0 deletions internal/runner/tools/opentofu/opentofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ func (t *OpenTofu) Show(planArtifactPath, mode string) ([]byte, error) {
}
return out, nil
}

func (t *OpenTofu) GetExecPath() string {
return t.ExecPath
}
4 changes: 4 additions & 0 deletions internal/runner/tools/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (t *Terraform) Show(planArtifactPath, mode string) ([]byte, error) {
}
}

func (t *Terraform) GetExecPath() string {
return t.ExecPath
}

func (t *Terraform) silent() {
t.exec.SetStdout(io.Discard)
t.exec.SetStderr(io.Discard)
Expand Down
23 changes: 8 additions & 15 deletions internal/runner/tools/terragrunt/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,24 @@ import (
"errors"
"os/exec"

"github.com/padok-team/burrito/internal/runner/tools/opentofu"
"github.com/padok-team/burrito/internal/runner/tools/terraform"
c "github.com/padok-team/burrito/internal/utils/cmd"
)

type Terragrunt struct {
ExecPath string
WorkingDir string
Terraform *terraform.Terraform
OpenTofu *opentofu.OpenTofu
ExecPath string
WorkingDir string
ChildExecPath string
}

func (t *Terragrunt) TenvName() string {
return "terragrunt"
}

func (t *Terragrunt) getDefaultOptions(command string) ([]string, error) {
var execPath string
if t.Terraform != nil {
execPath = t.Terraform.ExecPath
} else if t.OpenTofu != nil {
execPath = t.OpenTofu.ExecPath
} else {
return nil, errors.New("Cannot find a valid binary to use with Terragrunt")
}
return []string{
command,
"--terragrunt-tfpath",
execPath,
t.ChildExecPath,
"--terragrunt-working-dir",
t.WorkingDir,
"-no-color",
Expand Down Expand Up @@ -110,3 +99,7 @@ func (t *Terragrunt) Show(planArtifactPath, mode string) ([]byte, error) {
}
return output, nil
}

func (t *Terragrunt) GetExecPath() string {
return t.ExecPath
}

0 comments on commit b364f56

Please sign in to comment.