From d9b0d2d3a7c3281f8b5c308bc576c0b2e2279f10 Mon Sep 17 00:00:00 2001 From: milldr Date: Fri, 27 Dec 2024 10:06:12 -0500 Subject: [PATCH] added warning message when TF_CLI var-file specified --- internal/exec/shell_utils.go | 20 +++++++++++++------- internal/exec/terraform.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index fbf14a6f9..c3969f119 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -185,13 +185,19 @@ func execTerraformShellCommand( } }() - // Set the Terraform environment variables to reference the var file - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_plan=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_apply=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_refresh=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_import=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_destroy=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_console=-var-file=%s", varFile)) + // Define the Terraform commands that may use var-file configuration + tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} + + // Check for existing var-file arguments in TF_CLI environment variables + for _, cmd := range tfCommands { + envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) + existing := os.Getenv(envVar) + if existing != "" && strings.Contains(existing, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") + } + // Set the Terraform environment variable to reference the var file + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) + } // Set environment variables to indicate the details of the Atmos shell configuration componentEnvList = append(componentEnvList, fmt.Sprintf("ATMOS_STACK=%s", stack)) diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index fcf65bdb3..1910471c3 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -245,10 +245,23 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("TF_APPEND_USER_AGENT=%s", appendUserAgent)) } + // Check for existing var-file arguments in TF_CLI environment variables + tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} + for _, cmd := range tfCommands { + envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) + existing := os.Getenv(envVar) + if existing != "" && strings.Contains(existing, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") + } + } + // Print ENV vars if they are found in the component's stack config if len(info.ComponentEnvList) > 0 { u.LogDebug(atmosConfig, "\nUsing ENV vars:") for _, v := range info.ComponentEnvList { + if strings.Contains(v, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in component environment! This may be overwritten by Atmos") + } u.LogDebug(atmosConfig, v) } }