Skip to content

Commit

Permalink
added warning message when TF_CLI var-file specified
Browse files Browse the repository at this point in the history
  • Loading branch information
milldr committed Dec 27, 2024
1 parent 02bbeb9 commit d9b0d2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/exec/shell_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit d9b0d2d

Please sign in to comment.