Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle TF_DATA_DIR and Error Logging for !terraform.output #1037

Closed
wants to merge 8 commits into from
Closed
8 changes: 4 additions & 4 deletions internal/exec/terraform_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to describe the component '%s' in the stack '%s': %w", component, stack, err))
}

// Check if the component in the stack is configured with the 'static' remote state backend, in which case get the
Expand All @@ -269,7 +269,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to get remote state backend static type outputs: %w", err))
}

var result any
Expand All @@ -284,7 +284,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to execute terraform output for the component '%s' in the stack '%s': %w", component, stack, err))
}

// Cache the result
Expand Down Expand Up @@ -314,7 +314,7 @@ func getTerraformOutputVariable(

res, err := u.EvaluateYqExpression(atmosConfig, outputs, val)
if err != nil {
u.LogErrorAndExit(fmt.Errorf("error evaluating terrform output '%s' for the component '%s' in the stack '%s':\n%v",
u.LogErrorAndExit(fmt.Errorf("error evaluating terraform output '%s' for the component '%s' in the stack '%s':\n%v",
output,
component,
stack,
Expand Down
9 changes: 8 additions & 1 deletion internal/exec/terraform_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func checkTerraformConfig(atmosConfig schema.AtmosConfiguration) error {
// We delete the file to prevent the Terraform prompt asking to select the default or the
// previously used workspace. This happens when different backends are used for the same component.
func cleanTerraformWorkspace(atmosConfig schema.AtmosConfiguration, componentPath string) {
filePath := filepath.Join(componentPath, ".terraform", "environment")
// If TF_DATA_DIR is set, use the TF_DATA_DIR path
tfDataDir := os.Getenv("TF_DATA_DIR")
// If not set, default to .terraform
if tfDataDir == "" {
tfDataDir = ".terraform"
}

filePath := filepath.Join(componentPath, tfDataDir, "environment")
u.LogDebug(fmt.Sprintf("\nDeleting Terraform environment file:\n'%s'", filePath))
_ = os.Remove(filePath)
milldr marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
Loading